简体   繁体   中英

Binding a button ItemTemplate to check in which row has been clicked

I have a gridview which has this look:

第一眼

The Edit is the classic Edit/Update/Cancel buttonColumn. The Upgrade is an ItemTemplate with a button integrated. After clicking one of the Upgrade/Downgrade buttons, the gridview gets this look:

按下按钮后

Now, I have achieved so far to send the Save button event,in order to get the value of only one of the checkboxList (the one asocciated with the Save button). However, I would like to associate each Block of CheckboxList/Save button to a single Upgrade/Downgrade button. So, if I click the button on the first row, I need to disable the block on the second row and vice-versa. What i have right now is the following:

In the Upgrade/Downgrade buttonClick:

protected void Button2_Click(object sender, EventArgs e)
    {
        GridView1.Columns[2].Visible = true;
        button2Clicked = true;
        Session["buttonClicked"] = button2Clicked;
    }

In the Save buttonClick:

 protected void Button3_Click(object sender, EventArgs e)
    {

        CheckBoxList chb = new CheckBoxList();
        Button bt3 = (Button)sender;

            chb = (CheckBoxList)bt3.FindControl("Checkbox1");

            if(chb.SelectedValue=="Upgrade")
                Response.Write("Upgrade");
            else if (chb.SelectedValue == "Downgrade")
                Response.Write("Downgrade");
                   else
                        Response.Write("Not Allowed!");


    }

A second version of the Save buttonClick is the following:

 protected void Button3_Click(object sender, EventArgs e)
    {

        CheckBoxList chb = new CheckBoxList();
        Button bt3 = (Button)sender;

            chb = (CheckBoxList)bt3.FindControl("Checkbox1");
            if ((bool)Session["buttonClicked"])
            {
                if (chb.SelectedValue == "Upgrade")
                    Response.Write("Upgrade");
                else if (chb.SelectedValue == "Downgrade")
                    Response.Write("Downgrade");
                else
                    Response.Write("Not Allowed!");
            }
            else { Response.Write("Wrong Button clicked!"); }


    }

It seems like the Up/Downgrade button click is not stored anywhere. Is it some event that I am missing?

So, After @Andrei hint about using RowCommand Event and the tutorial found: here and here(This is in both c# and VB) I modified my code as following:

First, i modified the button like this:

 <asp:Button ID="Button2" runat="server" Text="Upgrade/Downgrade" OnClick="Button2_Click" CommandName="Command" CommandArgument="<%# Container.DataItemIndex %>" />

Use this tutorial to understand Container.DataItemIndex

Also, you have to add athe RowCommand event to Gridview like this:

     <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
     onrowcancelingedit="GridView1_RowCancelingEdit"  OnRowCommand="GridView1_RowCommand"
    onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating" 
    onrowdatabound="GridView1_RowDataBound" EnableModelValidation="True" BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" ForeColor="Black" GridLines="Vertical">
              <AlternatingRowStyle BackColor="#CCCCCC" />

And in code behind, here are the modifications:

 protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {


            if (e.CommandName == "Command")
            {
                //Determine the RowIndex of the Row whose Button was clicked.
                int rowIndex = Convert.ToInt32(e.CommandArgument);

                //Reference the GridView Row.
                GridViewRow row = GridView1.Rows[rowIndex];

                //Fetch value of CheckboxList.
                CheckBoxList chb = (row.FindControl("Checkbox1") as CheckBoxList);
                chb.Enabled = true;
            }

    }

It is as simple as it looks!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM