Problem with Datalist data

S

savvy

I'm displaying the data through a DataList. In which i've a field, Job
Description which shows all the data from "JobDescription" column in
the database.

<%# DataBinder.Eval(Container.DataItem, "JobDescription")%>

But Initially i just want only some part of the some particular amount
data to be displayed (say 100 words or some 2000 characters) not the
whole data.Is there any way to trim this function to above requirement
using strings or do I need to do some manupulation in the database?
How can I do this?
Thanks in Advance
 
E

Eliyahu Goldin

You can handle either DataItemBound or PreRender event. In the event you
have access to the data that will be rendered. You can do with the data
whatever you want: trim, format, change style etc.

Eliyahu
 
S

savvy

Thanks for your replies
It helped me alot
I'm still a little bit confused , if u can give me any sort of sample
code for the above problem it will be great help to me
Thanks Once Again in Advance
 
G

Guest

Another method is to write a function and call it during the databinding..
code will be like below

<%# TrimText(DataBinder.Eval(Container.DataItem, "JobDescription")) %>

This code behind function would look like

protected bool TrimText(string val)
{
string TrimmedVal = // add your logic to trim val here
return(TrimmedVal);
}
 
S

savvy

Thanks for all your help
I used the above idea and implemented the following function as given
below. And its working perfectly

string Truncate(string input, int characterLimit) {

string output = input;

// Check if the string is longer than the allowed amount
// otherwise do nothing

if (output.Length > characterLimit && characterLimit > 0) {
// cut the string down to the maximum number of characters
output = output.Substring(0,characterLimit);

// Check if the character right after the truncate point was a
space

// if not, we are in the middle of a word and need to remove the
rest of it
if (input.Substring(output.Length,1) != " ") {
int LastSpace = output.LastIndexOf(" ");

// if we found a space then, cut back to that space
if (LastSpace != -1) {
output = output.Substring(0,LastSpace);
}
}
// Finally, add the "..."
output += "...";
}
return output;
}
Thanks to everyone
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top