始终取消选中gridview中复选框的状态 [英] The state of the checkbox in the gridview is always unchecked

查看:75
本文介绍了始终取消选中gridview中复选框的状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在gridviewdgZiel中有以下带有复选框chkZiel的HTML页面:

I have the following HTML Page with the checkbox "chkZiel" in the gridview "dgZiel":

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="CC_SimpleTargetReport.aspx.cs" Inherits="CCMatrixDB_WEB.REPORT.CC_SimpleTargetReport" %>

<%@ Register Assembly="Microsoft.ReportViewer.WebForms, Version=12.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" Namespace="Microsoft.Reporting.WebForms" TagPrefix="rsweb" %>

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
    <script src="Scripts/jquery-1.10.2.min.js"></script>
    <script src="Scripts/JavaSript_scroll.js"></script>
    <asp:Table ID="Table1" runat="server" Width="100%">
        <asp:TableRow ID="TableRow1" runat="server">
            <asp:TableCell ID="TableCell1" runat="server" HorizontalAlign="Left">
                <asp:TextBox ID="tbCOL1" runat="server" BorderWidth="0" BorderStyle="None" Text="TARGET" Width="100"></asp:TextBox>
            </asp:TableCell>
            <asp:TableCell ID="TableCell2" runat="server" HorizontalAlign="Left">
                <asp:TextBox ID="tbCOL2" runat="server" BorderWidth="0" BorderStyle="None" Text="COMPONENT" Width="100"></asp:TextBox>
            </asp:TableCell>
        </asp:TableRow>
        <asp:TableRow ID="TableRow2" runat="server">
            <asp:TableCell ID="TableCell3" runat="server" HorizontalAlign="Left">
                <DIV style=" border-style: double; OVERFLOW: auto; WIDTH: 550px; HEIGHT: 168px"> 
                    <asp:GridView ID="dgZiel" runat="server" BackColor="White" 

                        CellPadding="4" GridLines="Horizontal" AutoGenerateColumns="false" ShowHeader="False">
                        <Columns>
                            <asp:TemplateField ItemStyle-HorizontalAlign="Center" >
                                <ItemTemplate>
                                    <asp:CheckBox ID="chkZiel" runat="server"  Width="40px" />
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:BoundField DataField="Name" ItemStyle-Width="100%" />
                        </Columns>
                    </asp:GridView>
                </div>
            </asp:TableCell>
        </asp:TableRow>
		<asp:TableRow ID="TableRow3" runat="server">
            <asp:TableCell ID="TableCell5" runat="server" HorizontalAlign="Left" ColumnSpan="2" VerticalAlign="Bottom" Height="45px">
                <asp:Button ID="Button" runat="server" Text="GENERATE REPORT" Width="142px" onclick="Button_Click" />
            </asp:TableCell>
        </asp:TableRow>
    </asp:Table>
</asp:Content>





以下代码检查所有gridview行的复选框的状态:





The following code checks the state of the checkbox for all gridview rows:

    protected void Button_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < dgZiel.Rows.Count; i++)
        {
            CheckBox chkZiel = (CheckBox) dgZiel.Rows[i].FindControl("chkZiel");
            if (chkZiel.Checked == true)
            {
                string name = dgZiel.Rows[i].Cells[1].Text;
            }
        }
}





但是所有复选框的状态都是未选中的(chkZiel.Checked - > false)等于是否选中复选框。

有什么问题?



什么我试过了:



我尝试了不同的c#代码,但是复选框的状态始终未经检查。



But the state of all checkboxes are uncheced (chkZiel.Checked -> false) equal if the checkbox is checked or not.
What is wrong?

What I have tried:

I tried different c# codes, but the state of the checkbox stays always on unchecked.

推荐答案

非常感谢你。我忘记了:

if(!this.IsPostBack)
Page_Load()函数中的
语句。
Thank you wery much. I forget the:
if (!this.IsPostBack)
statement in the Page_Load() function.


这通常会发生如果你没有在Not Ispostack块中绑定GridView。如果您在页面加载时绑定网格,请确保将其包装在Not Ispostback中。例如:



This will typically happen if you are not binding your GridView within Not Ispostack block. If you are binding your Grid on page load then make sure to wrap it within Not Ispostback. For example:

protected void Page_Load(object sender, EventArgs e){
        if (!IsPostBack)
        {
            // Populate your grid here and call DataBind()
           
        }
}





这将确保CheckBox的状态不会重置在回发上。



This will ensure that the state of your CheckBox will not reset on postbacks.


这篇关于始终取消选中gridview中复选框的状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆