databinding asp label

  • Thread starter Thread starter Jeronimo Bertran
  • Start date Start date
J

Jeronimo Bertran

I am currently using databing to show on an asp:label the result of
appending two database fields in the following way:


<asp:label id=textAcknowledgedBy runat="server" Text='<%# DataBinder.Eval
(dataSetEventResponse1, "Tables[spEventDetails].DefaultView.[0].UserName")
+ " from " + DataBinder.Eval(dataSetEventResponse1, "Tables
[spEventDetails].DefaultView.[0].ComputerName") %>'></asp:label>


This works fine, but now I need to make it a bit more complex and check if
the UserName field is null and in case it is, display the name of the
current user (which is obtained from a session variable).

How can I achieve this?

Thanks,

Jeronimo Bertran
 
Hi Jeronimo,

I dont think there is any direct way to give conditions for a Label..

One workaound would be like....

Write a function which takes 2 parameters and do all your validations and
checks here for the label control.
And let this method populate the label control...

Hope this helps...

Need any help, do post a msg back..

Happy Coding
 
You must set it in the Page_Load () event, something like:

private void Page_Load(object sender, System.EventArgs e)

if (! IsPostBack) {

textAcknowledgedBy.Text = ....

} else {

....

}



Inline databinding are only for the simple cases.

S. L.
 
Jeronimo,

Instead of putting databind expression in the <asp:label...>, handle
ItemDataBound event. In the event handler you can implement any logic you
wish by using normal programming in C# or VB.

Eliyahu
 
Thanks for all your suggestions,

Hi Jeronimo,

I think the two suggestions
1. Using a helper funtion (defined in page class) in your <%# %>
databinding template

2. Programly set the lable's Text in codebehind such as
lblName.Text = .....

are both ok. How do you think of them? If you have any further concerns or
questions on this, please feel free to post here. 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.)
 
Back
Top