Difference between asp.net label text elements and ordnary text

what is the difference between this label text type ?

<asp:Label ID="lbl2" **Text="Name"** runat="server"></asp:Label>

will create a Label control which Text property will have the value "Name"

<asp:Label ID="lbl2"  runat="server"**>Name</**asp:Label>

will create a Label control

  • with Text property having the value String.Empty
  • with a Literal child control which Text property value will be "Name"

be aware that using both behaviors (setting Text property and having content) at the same time might lead to unexpected behavior : see http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.label.text.aspx

Note : Setting the Text property will clear any other controls contained in the Label control.

So I think the problem is that when you write :

<asp:Label ID="lbl2"  runat="server"**><%#  Eval("StudentName") %></**asp:Label>

then

Label xx = GridView1.Rows[e.NewEditIndex].FindControl("lbl2") as Label;
txtName.Text = xx.Text;

You are trying to access value of a child Literal control which has not yet been DataBound

Not sure it would work or make a difference, but you may try :

Label xx = GridView1.Rows[e.NewEditIndex].FindControl("lbl2") as Label;
xx.Controls[0].DataBind();
txtName.Text = xx.Text;

Anyway, by now you should have figured that you'd better use the Text property of your Label and not the implicit Text Literal


the first to labels will give you spans. no much difference it's more what you prefer out put in the html:

< span id="MainContent_lbl2" >Name< /span >

< span id="MainContent_Label1" >Name< /span >

when you write your Eval outside the label you still need to add the ' ' (single quotation) try it and if it didn't work try the " " double qoutation. I hope I could give you some insight on your question :)


<asp:Label ID="lbl2" **Text="Name"** runat="server"></asp:Label>

I am not sure but this will render the asp label as <label> and the text part will be rendered as the InnerText of this label where other one

<asp:Label ID="lbl2" runat="server"**>Name</**asp:Label> this will render the Name as the InnerHtml of the <label> tab