【发布时间】:2013-07-19 02:14:12
【问题描述】:
我有一个带有复选框的网格视图。当有人选择复选框时,我想获取所有选中的隐藏值并保存到数据库中。下面是我的网格。
<asp:GridView ID="grdBillableAssetDetails" AutoGenerateColumns="False" runat="server" Width="100%" DataKeyNames="ListId">
<HeaderStyle CssClass="GridHeader" />
<RowStyle BackColor="White" ForeColor="#284775" CssClass="GridRow" Height="25px" />
<Columns>
<asp:TemplateField HeaderText="Check AssetSubType" SortExpression="CheckAssetSubType">
<ItemStyle HorizontalAlign="Center" Width="100px" />
<ItemTemplate>
<asp:CheckBox ID="chkAssetSubType" runat="server" Checked='<%#Eval("Select").ToString() == "1" ? true:false%>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ListId" HeaderText="ListId" Visible="false">
<HeaderStyle HorizontalAlign="Center" Width="150px" />
<ItemStyle HorizontalAlign="center" Width="150px" />
</asp:BoundField>
<asp:BoundField DataField="AssetType" HeaderText="AssetType">
<HeaderStyle HorizontalAlign="Center" Width="150px" />
<ItemStyle HorizontalAlign="center" Width="150px" />
</asp:BoundField>
<asp:BoundField DataField="AssetSubType" HeaderText="AssetSubType">
<HeaderStyle HorizontalAlign="Center" Width="150px" />
<ItemStyle HorizontalAlign="center" Width="150px" />
</asp:BoundField>
</Columns>
</asp:GridView>
下面是我想要将商店列表值和 CompanyId 放入数据库的代码。
protected void btnSave_ServerClick(object sender, EventArgs e)
{
try
{
int companyId = Convert.ToInt32(Request.QueryString["EntityId"]);
List<int> listListId = new List<int>();
foreach (GridViewRow gvrow in grdBillableAssetDetails.Rows)
{
CheckBox chk = (CheckBox)gvrow.FindControl("chkAssetSubType");
if (chk.Checked)
{
int ListId = (int)grdBillableAssetDetails.DataKeys[gvrow.RowIndex].Value;
listListId.Add(ListId);
}
}
DataAccess.SaveListTypes(companyId, listListId);
hdnPageStatus.Value = "0";
}
catch (Exception ex)
{
hdnErrMsg.Value = ex.Message;
}
}
谁能帮帮我。
【问题讨论】:
-
我想获取选中的行隐藏值以及所有已选中的值存储到数据库中。
-
请在您尝试获取所有检查值的地方发布您的代码隐藏(C# 或 VB.NET)。
标签: c# asp.net gridview webforms