[英]Binding a button ItemTemplate to check in which row has been clicked
我有一个具有以下外观的gridview:
Edit是经典的Edit / Update / Cancel buttonColumn。 升级是集成了按钮的ItemTemplate。 单击“升级/降级”按钮之一后,gridview的外观如下:
现在,到目前为止,我已经实现了发送“保存”按钮事件,以便仅获取checkboxList之一(与“保存”按钮相关联的一个)的值。 但是,我想将CheckboxList / Save按钮的每个Block关联到单个Upgrade / Downgrade按钮。 因此,如果单击第一行上的按钮,则需要禁用第二行上的块,反之亦然。 我现在所拥有的是以下内容:
在升级/降级按钮中单击:
protected void Button2_Click(object sender, EventArgs e)
{
GridView1.Columns[2].Visible = true;
button2Clicked = true;
Session["buttonClicked"] = button2Clicked;
}
在保存按钮中单击:
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!");
}
第二个版本的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!"); }
}
似乎“上/降级”按钮单击未存储在任何地方。 我有什么事想念吗?
因此,在@Andrei关于使用RowCommand事件的提示和找到的教程之后: 在这里和这里(这在c#和VB中),我将代码修改如下:
首先,我这样修改按钮:
<asp:Button ID="Button2" runat="server" Text="Upgrade/Downgrade" OnClick="Button2_Click" CommandName="Command" CommandArgument="<%# Container.DataItemIndex %>" />
使用本教程了解Container.DataItemIndex
同样,您必须向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" />
在后面的代码中,有一些修改:
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;
}
}
看起来很简单!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.