加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 基于varchar列在Gridview DataBound中显示CheckBox

发布时间:2020-12-15 05:39:20 所属栏目:百科 来源:网络整理
导读:SQL Server表结构: ChapName varchar(200)Status varchar(1) 需求: 我想在Visual Studio 2010的ASP.NET网格视图中显示复选框 如果status列的值为T,则检查并取消选中. 但它只显示文本框. 我试过 asp:templatefield和 asp:itemtemplate但如果我尝试绑定此
SQL Server表结构:
ChapName        varchar(200)
Status          varchar(1)

需求:

>我想在Visual Studio 2010的ASP.NET网格视图中显示复选框
如果status列的值为T,则检查并取消选中.
但它只显示文本框.
>我试过< asp:templatefield>和< asp:itemtemplate>但如果我尝试绑定此复选框,则会抛出错误.
>因为我是这个领域的初学者,所以需要任何示例代码.

我试过的代码:

protected void GridView1_RowDataBound(object sender,GridViewRowEventArgs e)
{
            CheckBox c = (CheckBox)GridView1.FindControl("ChkStatus");
            TextBox TB = (TextBox)GridView1.FindControl("Status");

        //Response.Write(TB.Text);
            if (TB.Text == "T")
            {
                c.Checked = true;
            }
            else
            {
                c.Checked = false;
            }
    }

我得到的错误

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web
request. Please review the stack trace for more information about the
error and where it originated in the code.

Exception Details:
System.NullReferenceException: Object reference not set to an instance
of an object.

Aspx标记:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
              DataKeyNames="Comp,QTypeCode" DataSourceID="SDS_QType_Edit" 
              BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" 
              BorderWidth="1px" 
              CellPadding="4" ForeColor="Black" GridLines="Vertical" 
              AllowPaging="True" AllowSorting="True" 
              onselectedindexchanged="GridView1_SelectedIndexChanged" 
              onrowdatabound="GridView1_RowDataBound">
    <AlternatingRowStyle BackColor="White" />
    <Columns>
        <asp:CommandField ShowEditButton="True" ShowSelectButton="True" />
        <asp:BoundField DataField="QTypeCode" HeaderText="QTypeCode" 
                        SortExpression="QTypeCode" InsertVisible="False" 
                        ReadOnly="True" />
        <asp:BoundField DataField="Descr" HeaderText="Descr" SortExpression="Descr" />
        <asp:CheckBoxField DataField="AnsReq" HeaderText="AnsReq" ReadOnly="True" 
                           SortExpression="AnsReq" />
        <asp:CheckBoxField DataField="OptionPrint" HeaderText="OptionPrint" 
                           ReadOnly="True" SortExpression="OptionPrint" />
        <asp:BoundField DataField="Status" HeaderText="Status" 
                        SortExpression="Status" />
        <asp:BoundField DataField="TransDate" HeaderText="TransDate" 
                        SortExpression="TransDate" />
        <asp:BoundField DataField="UserName" HeaderText="UserName" 
                        SortExpression="UserName" />
        <asp:TemplateField HeaderText="Check Box" >
            <ItemTemplate>
                <asp:CheckBox ID ="ChkStatus" Text="text" runat="server" />
            </ItemTemplate>
        </asp:TemplateField>
     </Columns>
     <EditRowStyle Wrap="False" />
     <EmptyDataRowStyle Wrap="False" />
     <FooterStyle BackColor="#CCCC99" />
     <HeaderStyle Wrap="False" BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
     <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
     <RowStyle Wrap="False" BackColor="#F7F7DE" />
     <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" 
                       Wrap="False" />
     <SortedAscendingCellStyle BackColor="#FBFBF2" />
     <SortedAscendingHeaderStyle BackColor="#848384" />
     <SortedDescendingCellStyle BackColor="#EAEAD3" />
     <SortedDescendingHeaderStyle BackColor="#575357" />
</asp:GridView>

解决方法

假设您在asmx上定义了如下网格
<asp:GridView ID="GridView1" runat="server" 
    onrowdatabound="GridView1_RowDataBound" AutoGenerateColumns="False">
    <Columns>
        <asp:BoundField DataField="ChapName" HeaderText="ChapName" />
        <asp:TemplateField HeaderText="Status" Visible ="false">
            <ItemTemplate>
                <asp:Label ID="Status" runat="server" Text='<%# Bind("Status") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Check Box" >
        <ItemTemplate>
            <asp:CheckBox ID ="ChkStatus" Text="text" runat="server" />
        </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

在Row Data Bound事件上,您可以找到如下控件

protected void GridView1_RowDataBound(object sender,GridViewRowEventArgs e)
{

    CheckBox c = e.Row.FindControl("ChkStatus") as CheckBox;
    Label lbl = e.Row.FindControl("Status") as Label;
    if (c!= null && lbl != null )
    {
        c.Checked = (lbl.Text == "T");
    }

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读