[英]Binding a button ItemTemplate to check in which row has been clicked
I have a gridview which has this look: 我有一个具有以下外观的gridview:
The Edit is the classic Edit/Update/Cancel buttonColumn. Edit是经典的Edit / Update / Cancel buttonColumn。 The Upgrade is an ItemTemplate with a button integrated.
升级是集成了按钮的ItemTemplate。 After clicking one of the Upgrade/Downgrade buttons, the gridview gets this look:
单击“升级/降级”按钮之一后,gridview的外观如下:
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). 现在,到目前为止,我已经实现了发送“保存”按钮事件,以便仅获取checkboxList之一(与“保存”按钮相关联的一个)的值。 However, I would like to associate each Block of CheckboxList/Save button to a single Upgrade/Downgrade button.
但是,我想将CheckboxList / Save按钮的每个Block关联到单个Upgrade / Downgrade按钮。 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:
第二个版本的Save buttonClick如下:
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: 因此,在@Andrei关于使用RowCommand事件的提示和找到的教程之后: 在这里和这里(这在c#和VB中),我将代码修改如下:
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 使用本教程了解Container.DataItemIndex
Also, you have to add athe RowCommand event to Gridview like this:
同样,您必须向Gridview添加一个RowCommand事件,如下所示:
<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! 看起来很简单!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.