Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在代码隐藏中为网格中的复选框指定值_C#_Asp.net - Fatal编程技术网

C# 在代码隐藏中为网格中的复选框指定值

C# 在代码隐藏中为网格中的复选框指定值,c#,asp.net,C#,Asp.net,我在asp.net中有以下网格 <asp:GridView ID="grdDWlocations" CssClass="table table-hover table-striped" runat="server" GridLines="None" ShowHeaderWhenEmpty="True" EmptyDataText="No data found..." AutoGenerateColumns="False"> <Columns>

我在asp.net中有以下网格

<asp:GridView ID="grdDWlocations" CssClass="table table-hover table-striped" runat="server" GridLines="None" ShowHeaderWhenEmpty="True"
    EmptyDataText="No data found..." AutoGenerateColumns="False">
    <Columns>
        <asp:TemplateField HeaderText="" Visible="true">
            <HeaderTemplate>
                <asp:CheckBox ID="allDWlocchk" runat="server" Checked="true" Width="10px" onclick="CheckAllgrdReqDW(this)"></asp:CheckBox>
            </HeaderTemplate>
            <ItemTemplate>
                <asp:CheckBox ID="chk_DWlocReq" runat="server" Checked="true" Width="5px" OnCheckedChanged="chk_Req_CheckedChangedDW_Click" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Code">
            <ItemTemplate>
            <asp:Label ID="lbl_DWCode" runat="server" Text='<%# Bind("Ml_loc_cd") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Description">
            <ItemTemplate>
                <asp:Label ID="lbl_DWDescription" runat="server" Text='<%# Bind("Ml_loc_desc") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
但以上一项无效,如何才能正确执行?

使用此项:

    foreach(GridViewRow row in GridView1.Rows) {
        if(row.RowType == DataControlRowType.DataRow) {
            CheckBox myCheckBoxID = row.FindControl("myCheckBoxID") as CheckBox;
        }
        myCheckBoxID.Checked = true;
    }
或 如果您正在处理RowDataBound事件,则如下所示:

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if(e.Row.RowType == DataControlRowType.DataRow)
        {
            CheckBox myCheckBoxID = e.Row.FindControl("myCheckBoxID") as CheckBox;
        }
        myCheckBoxID.Checked = true;
    }
使用以下命令:

    foreach(GridViewRow row in GridView1.Rows) {
        if(row.RowType == DataControlRowType.DataRow) {
            CheckBox myCheckBoxID = row.FindControl("myCheckBoxID") as CheckBox;
        }
        myCheckBoxID.Checked = true;
    }
或 如果您正在处理RowDataBound事件,则如下所示:

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if(e.Row.RowType == DataControlRowType.DataRow)
        {
            CheckBox myCheckBoxID = e.Row.FindControl("myCheckBoxID") as CheckBox;
        }
        myCheckBoxID.Checked = true;
    }
请尝试以下操作:

CheckBox checkbox1 = dgvr.FindControl("chk_DWlocReq") as CheckBox;     
checkbox1.Checked = true;   
请尝试以下操作:

CheckBox checkbox1 = dgvr.FindControl("chk_DWlocReq") as CheckBox;     
checkbox1.Checked = true;   

我认为您的代码必须更改为:

foreach (GridViewRow dgvr in grdDWlocations.Rows)
{
    ((CheckBox)dgvr.FindControl("chk_DWlocReq")).Checked=true;
}

我认为您的代码必须更改为:

foreach (GridViewRow dgvr in grdDWlocations.Rows)
{
    ((CheckBox)dgvr.FindControl("chk_DWlocReq")).Checked=true;
}
我使用了以下代码:

ds1.SelectCommand = String.Format("exec myStoredproc");
dv1 = (DataView)ds1.Select(DataSourceSelectArguments.Empty);

    for (int i = 0; i < GridView1.Rows.Count; i++)
                {
                    if (dv1.Table.Rows[i]["ReceiveNotification"].ToString() == "1")
                    {
                        ((CheckBox)GridView1.Rows[i].FindControl("GV_chkNotification")).Checked = true;
                    }
                    else
                    {
                        ((CheckBox)GridView1.Rows[i].FindControl("GV_chkNotification")).Checked = false;
                    }
    
                }
我使用了以下代码:

ds1.SelectCommand = String.Format("exec myStoredproc");
dv1 = (DataView)ds1.Select(DataSourceSelectArguments.Empty);

    for (int i = 0; i < GridView1.Rows.Count; i++)
                {
                    if (dv1.Table.Rows[i]["ReceiveNotification"].ToString() == "1")
                    {
                        ((CheckBox)GridView1.Rows[i].FindControl("GV_chkNotification")).Checked = true;
                    }
                    else
                    {
                        ((CheckBox)GridView1.Rows[i].FindControl("GV_chkNotification")).Checked = false;
                    }
    
                }