1

I have a grid view on a webform that has a hidden field holding the id, how do I capture the data from the id field to use in my update statement? This is what my syntax looks like...
HTML

    <asp:GridView runat="server" ID="gridview2" AutoGenerateColumns="false" >
<Columns>
<asp:BoundField DataField="weekofyear" HeaderText="Week" />
<asp:BoundField DataField="supportname" HeaderText="Name" />
<asp:BoundField DataField="supid" HeaderText="SupportIDName" Visible="false"/>
<asp:TemplateField>
    <ItemTemplate>
        <asp:CheckBox ID="TicketCompleted" runat="server" AutoPostBack="true" OnCheckedChanged="TicketCompletedCompleted_Click" Checked='<%# Convert.ToBoolean(Eval("TicketCompleted")) %>' />
    </ItemTemplate>
</asp:TemplateField>
</Columns>

C#

protected void TicketCompleted_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in dgRD.Rows)
{
    if (row.RowType == DataControlRowType.DataRow)
    {
        //Below captures the checkbox value (yes or no)
        CheckBox chk = (CheckBox)row.FindControl("TicketCompleted");                    
        if (chk.Checked)
        {
            //Here is where I will run the update
        }
    }
}
2
  • change hidden field to a ItemTemplate and add label and bind your id to label; in code behind find that label and get value Commented Jan 22, 2016 at 18:44
  • @techspider. I follow you until finding the value from the ItemTemplate and using C# to get the value? Commented Jan 22, 2016 at 18:48

1 Answer 1

3

HTML:

<asp:BoundField DataField="weekofyear" HeaderText="Week" />
<asp:BoundField DataField="supportname" HeaderText="Name"  />
<%--<asp:BoundField DataField="supid" HeaderText="SupportIDName" Visible="false"/>--%>
<asp:TemplateField>
     <ItemTemplate>
          <asp:Label runat="server" Text='<%#Eval("supid") %>' ID="supid" Visible="false"></asp:Label>
     </ItemTemplate>
     <ItemTemplate>
           <asp:CheckBox ID="TicketCompleted" runat="server" AutoPostBack="true" OnCheckedChanged="TicketCompletedCompleted_Click" Checked='<%# Convert.ToBoolean(Eval("TicketCompleted")) %>' />
     </ItemTemplate>
</asp:TemplateField>

C#

CheckBox chk = (CheckBox)row.FindControl("TicketCompleted");
if (chk.Checked)
{
    string ID = ((Label)row.FindControl("supid")).Text;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.