Author |
Topic |
bluestar
Posting Yak Master
133 Posts |
Posted - 2008-10-10 : 12:02:50
|
HelloI am using a tooltip in one of my column in datagrid.I want to show only 1st 20 characters and rest of the text to be shown in tooltip .for this i need to write a function.here is my code 1. <asp:TemplateField HeaderText="CommentText"> 2. <ItemTemplate> 3. <asp:Labelid="lbl"text='<%#tooltip(System.Convert.ToString(Eval("CommentText"))) %>' runat="server" ToolTip='<%#Eval("CommentText") %>'></asp:Label> 4. </ItemTemplate> 5. </asp:TemplateField> 1. public string tooltip(String input)//function is to display only 1st 20characters 2. { 3. char[] delimiterChars = { ' ' }; 4. string[] words = input.Split(delimiterChars); 5. 6. string output = ""; 7. //rebuilds string without new word 8. for (int x = 0; x != 20; x++) 9. { 10. if (x == 0) 11. { 12. output = words[x].ToString(); 13. } 14. else 15. { 16. output = output+" "+words[x]; 17. } 18. } 19. 20. return output; 21. }I am not sure whether my function is correct or not.I am getting errorIndex was outside the bounds of the array.Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 1. Exception Details: System.IndexOutOfRangeException: Index was outside the bounds of the array. 2. 3. Source Error: 4. 5. Line 31: else 6. Line 32: { 7. Line 33: output = output+" "+words[x]; 8. Line 34: } 9. Line 35: }please helpThank You |
|
afrika
Master Smack Fu Yak Hacker
2706 Posts |
Posted - 2008-10-10 : 12:34:08
|
chanage this8. for (int x = 0; x != 20; x++)to this8. for (int x = 0; x >= 20; x++) |
|
|
bluestar
Posting Yak Master
133 Posts |
Posted - 2008-10-10 : 12:38:36
|
thanks for the replyI did >=20,the error is gone but then its not showing any result in the grid view.That column is empty. |
|
|
afrika
Master Smack Fu Yak Hacker
2706 Posts |
Posted - 2008-10-10 : 12:46:50
|
Ok a few more errorsAdd this to your code6. int x ;7. x = 0;8. for ( x = 0; x >= 20; x++) |
|
|
afrika
Master Smack Fu Yak Hacker
2706 Posts |
Posted - 2008-10-10 : 12:48:34
|
Sorry, the code show be more like this6. int x ;7. x = 1;8. for ( x = 1; x >= 20; x++)The above code, will loop 21 times, and I guess what you want is 20 ? |
|
|
bluestar
Posting Yak Master
133 Posts |
Posted - 2008-10-10 : 13:21:03
|
Its still not working. I want 1st 20 characters to be displayed.Please do reply thanks |
|
|
afrika
Master Smack Fu Yak Hacker
2706 Posts |
Posted - 2008-10-10 : 13:26:20
|
There is nothing being passed to your variables, belowAlso could you please explain what you are trying to achieve ?1. public string tooltip(String input)//function is to display only 1st 20characters2. { 3. char[] delimiterChars = { ' ' };// this is obviously empty4. string[] words = input.Split(delimiterChars);5. 6. string output = ""; |
|
|
bluestar
Posting Yak Master
133 Posts |
Posted - 2008-10-10 : 13:30:40
|
okay what I want is the column name CommentText has many sentences.I am using gridview in my asp.net with C# form.If i have long text to be displayed in that columns in gridview it is showing complete text.Waht i want is it should show only few characters that is only 20 characters in the gridview display and in tooltip it should show complete text.please do replyThank You |
|
|
afrika
Master Smack Fu Yak Hacker
2706 Posts |
Posted - 2008-10-10 : 13:34:09
|
I honestly dont understand your explanation. But from your program and the little am understanding, it appears you want to split your sentence where ever you have a space in the array called delimiterChars ??? |
|
|
afrika
Master Smack Fu Yak Hacker
2706 Posts |
Posted - 2008-10-10 : 13:36:01
|
Please clarify, what do you want to split ?Whereever you have a space as depicted in your split function ? Highlighted in red above ? |
|
|
bluestar
Posting Yak Master
133 Posts |
Posted - 2008-10-10 : 13:36:15
|
in short I want my output variable to return 1st 20 character of the text. |
|
|
afrika
Master Smack Fu Yak Hacker
2706 Posts |
Posted - 2008-10-10 : 13:50:42
|
try this 3. string delimiterChars = { ' ' };4. string[] words = delimiterChars.Split(',');5. int x = words.GetUpperBound(0);6. string output = '';7. //rebuilds string without new word8. for ( x = 0; x != 20; x++)9. {10. if (x == 0)11. {12. output = words[x].ToString();13. }14. else15. {16. output = output.ToString()+' '+ words[x].ToString();17. }18. }19. 20. return output;21. } |
|
|
afrika
Master Smack Fu Yak Hacker
2706 Posts |
Posted - 2008-10-10 : 13:54:00
|
Sorry I forgot to add2. string delimiterChars ;3. delimiterChars = Label1.Text;// Assign the value of your control, Whatever you name it, label or etc to catch the value4. string[] words = delimiterChars.Split(' ');5. int x = words.GetUpperBound(0);6. string output = '';7. //rebuilds string without new word8. for ( x = 0; x != 20; x++)9. {10. if (x == 0)11. {12. output = words[x].ToString();13. }14. else15. {16. output = output.ToString()+' '+ words[x].ToString();17. }18. }19. 20. return output;21. } |
|
|
afrika
Master Smack Fu Yak Hacker
2706 Posts |
Posted - 2008-10-10 : 14:06:23
|
I asked you to try the code above, you also are changing the codes thereby making it difficult for me and yourself.I corrected this line for youfor ( x = 0; x != 20; x++) and you changed it back,What you changed it back to means, that if x = 0 or is not 20, which will give you an endless loop.I cant help you, if you are making it difficult for yourself. |
|
|
afrika
Master Smack Fu Yak Hacker
2706 Posts |
Posted - 2008-10-10 : 14:07:09
|
now you have deleted it ? |
|
|
bluestar
Posting Yak Master
133 Posts |
Posted - 2008-10-10 : 14:08:14
|
its giving me this error Compilation ErrorDescription: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.Compiler Error Message: CS0019: Operator '+' cannot be applied to operands of type 'method group' and 'char'Source Error:Line 40: else {Line 41: Line 42: output = output.ToString +' '+ words[x].ToString ;Line 43: Line 44: thank you |
|
|
afrika
Master Smack Fu Yak Hacker
2706 Posts |
Posted - 2008-10-10 : 14:09:56
|
quote: Originally posted by bluestar its giving me this error Compilation ErrorDescription: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.Compiler Error Message: CS0019: Operator '+' cannot be applied to operands of type 'method group' and 'char'Source Error:Line 40: else {Line 41: Line 42: output = output.ToString +' '+ words[x].ToString ;Line 43: Line 44: thank you
thats not the code, i gave you. You are still changing it.Am sorry, I cant help you if you keep changing it.Sorry |
|
|
bluestar
Posting Yak Master
133 Posts |
Posted - 2008-10-10 : 14:10:29
|
i deleted my post because I pasted the wrong code.by mistake .the old code which I modified.now I am using exactly your code.thanks for your help.but still i am getting one error. |
|
|
afrika
Master Smack Fu Yak Hacker
2706 Posts |
Posted - 2008-10-10 : 14:11:18
|
quote: Originally posted by bluestar i deleted my post because I pasted the wrong code.by mistake .the old code which I modified.now I am using exactly your code.thanks for your help.but still i am getting one error.
thats DEFINITELY not the code i gave you |
|
|
bluestar
Posting Yak Master
133 Posts |
Posted - 2008-10-10 : 14:17:18
|
well I am struggling with this from yesterday.okay can I ask you something about the following.delimiterChars = Label1.Text;// Assign the value of your control, Whatever you name it, label or etc to catch the valueI am displaying it in gridview label ,<asp:TemplateField HeaderText="CommentText"> <ItemTemplate> <asp:Label id="lbl" text ='<%# tooltip(System.Convert.ToString(Eval("CommentText"))) %>' runat="server" ToolTip='<%#Eval("CommentText") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> so I just want to confirm I have to write delimiterChars = lb1.TextordelimiterChars = gridview1.lb1.Text |
|
|
afrika
Master Smack Fu Yak Hacker
2706 Posts |
Posted - 2008-10-10 : 14:22:00
|
Am assuming, the value of <%# tooltip(System.Convert.ToString(Eval("CommentText"))) %> is assigned to the label. control called lbl and not lb1 as you put above.If this is the case thendelimiterChars = lbl.Text;is correct. Also, i would advice you to follow STRICTLY the codes, i gave you above, as c# is not as forgiving as vb.net and is a strongly typed language.In your code, you removed the delimiters (), which i put for youLine 40: else {Line 41: Line 42: output = output.ToString +' '+ words[x].ToString ;Line 43: Line 44: is very wrong.Stick to what i gave you.About struggling, I believe everyone in this forum and every programmer out there once struggled. I learnt a lot from this forum and others GoodluckEhi |
|
|
Next Page
|