ItemDataBound text modification question

G

Guest

I've got a repeater that's bound to a datareader and I'd like to
conditionally modify the text that is being outputted. Previously, I've done
this by assigning a name to attribute to the surrounding tablecell in which
the <%# %> tags reside and I could use the findcontrol to grab a handle to
the control by name and do any necessary modifications. I'm wondering if I
can get around this? Is it necessary to assign a name to a surrounding cell?
What if I'm not using a table? I tried to use e.item.controls(0), but this
does not seem to correspond with all available controls that get databound (I
guess it excludes non web-server controls like simple client side tables),
but there seems to be only one element of type "DataBoundLiteralControl." I
was guessing that this represented the text that was between the databinding
tags, and hoped that I could modify the text property to add some text in,
but it's a read only property. To sum it up, two questions:

1, what is the DataBoundLiteralControl actually used for? What does it
represent?

2, Is there a way to do what I'd like to do? To modify the text that is
bound, without using findcontrol? (The control has no name, it's just the
contents of the <%# %> tags.

Thanks...
 
S

Steven Cheng[MSFT]

Hi Benr,

Welcome to ASPNET newsgroup.
As for the problem you met in dealing with serverside databinding in
asp.net, here are some of my understanding and suggestions:

1. When we call xxcontrol.databind(); the ASP.NET will loop through all
the <%# %> blocks in that control's template and invoke them. For templated
databound control( such as datagrid, datalist, repeater..), it will loop
through all the item in datasource and executing the invocation of the <%#
%> blocks repeatly. And as for the "DataBoundLiteralControl" you
mentioned, it's the underlying internal control asp.net runtime used to
hold the databinding values generated by each
<%# %> block(this behavior is for templated databound controls like
repeater, datalist... , not for simple server controls like textbox,
label, dropdownlist....) because each templated databound control need to
add a certain control instance rather than a simple string value into its
sub controls collection.

2. For modifying the value in databinding process, we have the following
means:

1) We can use some servercontrols in the databound control's template and
assign the databiding expression to those control's property, then we can
reference those control's refernce in ItemDataBound event and modify their
corresponding propeties, just like:

<ItemTemplate>
<asp:TextBox id="txtName" runat="server" Text="<%# xxxxx %>" />
</ItemTemplate>

in ItemDataBound , we call

TextBox = e.Item.FindControl("txtName") as TextBox

to get the TextBox's reference and do our customization.

2) If we don't want to add additioal sub control to wrapper the databinding
text , one approach is to define some helper functions which do the
customization , and we can embed the function call within the <%# %> block
like:

<ItemTemplate>
<asp:TextBox id="txtName" runat="server" Text="<%#
MyFunction(DataBinder.Eval(...))%>" />
</ItemTemplate>

MyFunction can be a public/protected method of the codebebhind page class
or we can use a certain public class's static method.

Hope helps. Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
G

Guest

Hey Steven,

Thanks for your response, it was exactly what I was looking for.

Sincerely,

-Ben
 

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