簡體   English   中英

從Gridview獲取所有檢查的隱藏值

[英]Get all checked hidden values from Gridview

我有一個帶有復選框的gridview。 當有人選擇復選框時,我想獲取所有選中的隱藏值並保存到數據庫中。以下是我的網格。

<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;
    }
}

有什么可以幫助我的嗎?

下面是一個示例vb.NET

For j = 0 To gridCallfile.Rows.Count - 2<br>
    If Convert.ToBoolean (grid.Rows(j).Cells(5).Value) Then<br>
    'your code here
end If

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM