ASP.NET - access hidden field at code behind

Asked By Reena Jain on 24-Jun-09 11:03 AM
At Degin time

<asp:DataList runat="server" ID="chkAuction">

     <ItemTemplate>
     <td valign="top">
           <asp:CheckBox runat="server" ID="chkSelect" />
           <asp:HiddenField runat="server" ID="hdnSelect" Value='<%#  dataBinder.Eval(Container, "DataItem.SubCategoryID") %>' />

   </td>

  <td valign="top">
    <asp:Label ID="lblSelecet" runat="server" Text='<%#  DataBinder.Eval(Container, "DataItem.SubCategoryName") %>'></asp:Label>
 </td>
  </ItemTemplate>
</asp:DataList>

At Code Behind

 protected string GetSelectedSubCategory()
    {
        string catID = "";
        foreach (DataListItem li in chkSale.Items)
        {
            if (((CheckBox)li.FindControl("chkSelect")).Checked == true)
            {
                //catID += ((HiddenField)li.FindControl("hdnSelect")).Value.ToString() + ";";
              
            }
        }
}

But I'm not able to access the hidden field's value, just return me blank if I take the display text then it gives me, but I require hidden field's value, binding coding is done on page load and it working properly
It's also not working it return me the hdnSelect.Value is blank, why its not working, ITs binding propery in hdnSelect.value when I'm debug it, it goes at that point, means there is not prob with binding. rather it is not picked up the value just return blank. my prob is that I can not use html control in it.

Please help in this issue, thank in advance

Solution

Murali Mohan replied to Reena Jain on 24-Jun-09 11:25 AM

Dear reena,

change the code

"<asp:HiddenField runat="server" ID="hdnSelect" Value='<%#  dataBinder.Eval(Container, "DataItem.SubCategoryID") %>' />
"

to

<asp:HiddenField runat="server" ID="hdnSelect" Value='<%#  DataBinder.Eval(Container, "DataItem.SubCategoryID") %>' />

 

then check its once.

main problem is dataBinder it should be DataBinder


Solution

Murali Mohan replied to Reena Jain on 24-Jun-09 11:32 AM

I am providing the working code please into sample application.

in Aspx page:

<asp:DataList runat="server" ID="chkAuction">

<ItemTemplate>

<td valign="top">

<asp:CheckBox runat="server" ID="chkSelect" />

<asp:HiddenField runat="server" ID="hdnSelect" Value='<%# DataBinder.Eval(Container, "DataItem.SubCategoryID") %>' />

</td>

<td valign="top">

<asp:Label ID="lblSelecet" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.SubCategoryName") %>'></asp:Label>

</td>

</ItemTemplate>

</asp:DataList>

<br />

<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />

CodeBehind:

protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

Fill();

}

private void Fill()

{

DataTable dt = new DataTable();

dt.Columns.Add(new DataColumn("SubCategoryID"));

dt.Columns.Add(new DataColumn("SubCategoryName"));

for (int index = 0; index < 3; index++)

{

DataRow dr = dt.NewRow();

dr[0] = index;

dr[1] = "Item" + index;

dt.Rows.Add(dr);

}

chkAuction.DataSource = dt;

chkAuction.DataBind();

}


protected void Button1_Click(object sender, EventArgs e)

{

StringBuilder selectedValues = new StringBuilder();

foreach (DataListItem var in chkAuction.Items)

{

CheckBox chkSelect = (CheckBox)var.FindControl("chkSelect");

HiddenField hdnSelect = (HiddenField)var.FindControl("hdnSelect");

if (chkSelect != null && chkSelect.Checked && hdnSelect != null)

{

selectedValues.AppendLine(hdnSelect.Value);

}

}

}

Thanks A lot

Reena Jain replied to Murali Mohan on 27-Jun-09 02:56 AM
It' s working now. The prob is that I'd applied the javascript coding on it, so its not giving me the hidden field value, when I comment the javascript coding, its working fine.

Thanks being helpful
new problem
Reena Jain replied to Murali Mohan on 27-Jun-09 03:02 AM
hi, Now I've a new problem in the datalist.
How to unchecked the datalist's checkbox, because if I save the data and go again that form with another entry the previous checkboxes which I'd checked that time, show again. I want all the checkboxs in datalist should be unchecked after saving or updating the data. so if I go again in that form then it found clear to me lick textbox and simple checkbox

Please provide me the solution as soon as possible.

Thanks
Reena Jain
Solution
Murali Mohan replied to Reena Jain on 29-Jun-09 06:48 AM
Hi reena,

Take one Extra column for Checkbox like isChecked in dataset.
bind the value to checkbox.
when u r moving one page to another page or sub mitting the values you need to stored checked values to dataset.
after binding the values it is automatically check /uncheck the control based on IsChecked column.