【发布时间】:2014-01-28 06:15:27
【问题描述】:
如何访问特定选定和选中行的 gridview 单元格。下面是代码
<asp:GridView ID="GridView1" runat="server" AutoGenerateSelectButton="True">
<Columns>
<asp:TemplateField HeaderText="IsApproved">
<ItemTemplate>
<asp:CheckBox ID="chkApproved" runat="server" CommandName="Approve" />
</ItemTemplate></asp:TemplateField></Columns></asp:GridView>
<asp:RadioButtonList ID="RadioButtonList1" runat="server" RepeatDirection="Horizontal">
<asp:ListItem Text="Approve" Value="1"></asp:ListItem>
<asp:ListItem Text="Reject" Value="2"></asp:ListItem>
</asp:RadioButtonList>
gridview 的数据源由Page_Load 上的强类型数据集设置。现在在 btnSubmit 上,如果选中,我想将所选行插入到我的数据库表中
protected void btnSubmit_Click(object sender, EventArgs e)
{
if (RadioButtonList1.SelectedValue == "1")
{
foreach (GridViewRow row in GridView1.Rows)
{
var chk = row.FindControl("chkApproved") as CheckBox;
if (chk.Checked)
{
DataSet1TableAdapters.tbl_ApproveTableAdapter ta = new DataSet1TableAdapters.tbl_ApproveTableAdapter();
DataSet1.tbl_ApproveDataTable dt = new DataSet1.tbl_ApproveDataTable();
ta.Insert()// here I've to specify the cells of GV
}
}
}
}
<asp:Button ID="btnSubmit" runat="server" OnClick="btnSubmit_Click" Text="Submit" />
这里有两个问题
i) 如果在复选框中选中该行,则访问 gridview 单元格
ii) 在我的 GV 选择中,它自动设置为 AutoPostback,我想禁用它,因为它正在清除选定的复选框。 .
【问题讨论】:
-
您获取选中复选框的代码运行良好。您能告诉我们您在 GridView 中绑定了哪些事件吗?我认为 ItemCommand 会被绑定?