1

I seem to be stuck with tunnel vision only seeing how to update a database based off the check/uncheck event of a checkbox. What I am wanting to do is hold the event from firing until a button is pressed. How could I achieve such?

HTML

<td valign="top" style="text-align: left; width: 200px;">
<asp:GridView runat="server" ID="datagridTest" AutoGenerateColumns="false" GridLines="Both" >
    <Columns>
    <asp:BoundField DataField="thirteen" HeaderText="You" />
    <asp:BoundField DataField="seven" HeaderText="Me" />
    <asp:TemplateField>
        <ItemTemplate>
            <asp:Label runat="server" Text='<%#Eval("josemen1212") %>' ID="red" Visible="false"></asp:Label>
         </ItemTemplate>
        <ItemTemplate>
            <asp:CheckBox ID="checker" runat="server" AutoPostBack="true" OnCheckedChanged="Checker_Click" Checked='<%# Convert.ToBoolean(Eval("green")) %>' />
        </ItemTemplate>
    </asp:TemplateField>
    </Columns>
</asp:GridView>
</br>
<asp:Button runat="server" ID="btnClickMe" CssClass="Buttons" Text="Click" OnClick="btnClickMe_Click" />
</td>

C#

private void btnClickMe_Click(object sender, EventArgs e)
{
  //Here is where I want the update to run
}

protected void Checker_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in dgRD.Rows)
{
    if (row.RowType == DataControlRowType.DataRow)
    {
        CheckBox chk = (CheckBox)row.FindControl("checker");
        if (chk.Checked)
        {
            string ID = ((Label)row.FindControl("josemen1212")).Text;
            //Run Sql statement to update db
        }
        else
        {
            string ID = ((Label)row.FindControl("josemen1212")).Text;
            //Run sql statement to update db
        }
    }
}
}
7
  • You just need to move your code from Checker_Click to btnClickMe_Click right? Or is there something I'm overlooking here? Commented Jan 26, 2016 at 15:49
  • Just remove the code in your Checker_Click event and put it in your btnClickMe_Click event. Then remove the call to Checker_Click from the checkbox itself. Commented Jan 26, 2016 at 15:50
  • @Gilgamesh & sr28 - do I have a need for the Checker_click method anymore? Commented Jan 26, 2016 at 15:59
  • no, you don't need the Checker_click method anymore Commented Jan 26, 2016 at 16:02
  • May I ask what is the use of string ID in your if (chk.Checked) block? and why the string ID is identical? That is, both obtained from ((Label)row.FindControl("josemen1212")).Text; does that make any difference? Commented Jan 26, 2016 at 16:02

1 Answer 1

1

Move the code from the Checker_Click method to the btnClickMe_Click event? Something like this:

 private void btnClickMe_Click(object sender, EventArgs e)
    {
      foreach (GridViewRow row in dgRD.Rows)
    {
        if (row.RowType == DataControlRowType.DataRow)
        {
            CheckBox chk = (CheckBox)row.FindControl("checker");
            if (chk.Checked)
            {
                string ID = ((Label)row.FindControl("josemen1212")).Text;
                //Run Sql statement to update db
            }
            else
            {
                string ID = ((Label)row.FindControl("josemen1212")).Text;
                //Run sql statement to update db
            }
        }
    }
    }
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.