我有一个具有这种外观的gridview:
编辑是经典的编辑/更新/取消按钮列。 升级是一个ItemTemplate,集成了一个按钮。单击其中一个升级/降级按钮后,gridview将显示以下内容:
现在,我已经实现了发送Save按钮事件,以便只获取checkboxList中的一个(使用Save按钮取消的那个)的值。但是,我想将每个CheckboxList / Save按钮块关联到一个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按钮单击的第二个版本如下:
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!"); }
}
似乎没有将Up / Downgrade按钮单击存储在任何位置。这是我遗失的一些事件吗?
答案 0 :(得分:1)
所以, 在@Andrei提示使用RowCommand事件并发现教程之后: here和here(This is in both c# and VB)我修改了我的代码如下:
首先,我修改了这样的按钮:
<asp:Button ID="Button2" runat="server" Text="Upgrade/Downgrade" OnClick="Button2_Click" CommandName="Command" CommandArgument="<%# Container.DataItemIndex %>" />
Use this tutorial to understand Container.DataItemIndex
此外,您必须将RowCommand事件添加到Gridview,如下所示:
<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;
}
}
它看起来很简单!