Reputation: 38703
I have a doubt
What is the Different between these below type of labels text declarations ?
<asp:Label ID="lbl2" **Text="Name"** runat="server"></asp:Label>
and
<asp:Label ID="lbl2" runat="server"**>Name</**asp:Label>
I have straightly providing the text in text property Text="Name"
and providing the text on center of the label field > Name </
I have some more labels on my gridview controls , I want to get the label text value while edit the grid, I am using find control to get the label values
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
Label xx = GridView1.Rows[e.NewEditIndex].FindControl("lbl2") as Label;
Label yy = GridView1.Rows[e.NewEditIndex].FindControl("lbl2") as Label;
txtName.Text = xx.Text;
txtAge.Text = yy.Text;
}
Here is my gridview code
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lbl1" Text='<%# Eval("StudentName") %>' runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
It's working fine, if I put the Eval value in label Text property , But if i providing the eval values in center of the label, like ( <asp:Label ID="lbl1" runat="server"><%# Eval("StudentName") %></asp:Label>
) then find control didn't return the value ,It's returning empty ("") . Why ?
Edit:
But if I set the label text in outside of gridview , both of way's are working good(lblid.text give correct if set the text in that two way ) ! only i got the problem in label inside of gridview !
Upvotes: 8
Views: 4646
Reputation: 38703
Finally I found a answer. ( correct me if I am wrong)
My code behind have two class
public partial class Home : System.Web.UI.Page
Manually all page controls are defined by my Home class
So the ordinary label gave correct and both type of text value
But my below line was create new label by using system.Ui.Page
class ,
Label yy = GridView1.Rows[e.NewEditIndex].FindControl("lbl2") as Label;
now this find-control get only the label properties value Text="xx"
is property of the label so it's get the values , but Name this value is not stored in the any label properties , So we can't get it .
(it's my guess only )
and also i guess another one reason (It's just a joke )
this client side
<label>Text</label>
control have open and closed tag , But server sideLabel
control have not any open and closed tag, So this reason for i can't get the label value
Upvotes: 1
Reputation: 383
asp:Label controls are rendered as spans, not as html label elements. Use Text property if you need to access the value on the server side. Use id.innerHTML if you need to retrieve the value on the client side using javascript.
Upvotes: 1
Reputation: 15413
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
Text
property having the value String.Empty
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
Upvotes: 6
Reputation: 373
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 :)
Upvotes: 2
Reputation:
<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
Upvotes: 2