5

SQL Server 表结构:

ChapName        varchar(200)
Status          varchar(1)

要求

  • 如果状态列的值为 T,我想在 Visual Studio 2010 的 ASP.NET 网格视图中显示复选框,否则选中它并取消选中。但它只显示文本框。
  • 我已经尝试过<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;
            }
    }

我得到的错误

你调用的对象是空的。

说明:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

异常详细信息:
System.NullReferenceException:对象引用未设置为对象的实例。

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>
4

1 回答 1

3

假设您在 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");
    }

}
于 2013-05-01T17:06:33.057 回答