Reputation:
i have a datalist and inside it i bind a value in a label like this
<asp:Label ID="hf1" runat="server" Visible="false"><%# Eval("DeptId")%></asp:Label>
how can i get the value of label in datalist EditCommand?
Upvotes: 2
Views: 848
Reputation: 1391
You should use style(css) to hide that element.So that it can be rendered in code behind file where you want to find the value of Hidden field. Just use
<asp:Label ID="hf1" runat="server" style="display:none"><%# Eval("DeptId")%></asp:Label>
instead of
<asp:Label ID="hf1" runat="server" Visible="false"><%# Eval("DeptId")%></asp:Label>
then everything will work fine for you. Thanks Gourav
Upvotes: 0
Reputation: 4469
With the Label you can also get the controls in EditCommand event as below:
protected void dlData_EditCommand(object source, DataListCommandEventArgs e)
{
Label hf1 = (Label)e.Item.FindControl("hf1");//Any control you can find here
}
Also if you need to filter data item only in that case just put a condition:
protected void dlData_EditCommand(object source, DataListCommandEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item)
{
Label hf1 = (Label)e.Item.FindControl("hf1");
}
}
Upvotes: 0
Reputation: 94643
Use HiddenField
instead of Label with Visible="false"
. You may obtain the reference of control in DataList via FindControl(id)
method.
Markup:
<asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Eval("DeptId") %>' />
Code in EditCommand:
HiddenField h=e.Item.FindControl("HiddenField1") as HiddenField;
Upvotes: 2
Reputation: 21127
Visible="False"
will not render any content to the page. Use a <asp:hidden>
control instead:
<asp:hidden ID="hf1" runat="server" Text='<%# Eval("DeptId")%>' />
Upvotes: 0