网格视图页脚行中的单选按钮

本文关键字:单选按钮 视图 网格 | 更新日期: 2023-09-27 18:27:53

error- Object reference not set to an instance of an object. 

错误指向GridView页脚行中存在的RadioButtonList。 对象引用未设置为对象的实例。说明:执行当前 Web 请求期间发生未处理异常。请查看堆栈跟踪,了解有关错误及其在代码中起源位置的详细信息。

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

源错误:

Line 69: 
Line 70:         //for RadioButtonLiist Control
Line 71:         gender = ((RadioButtonList)  (this.GridView1.FooterRow.FindControl("RadioButtonList2"))).SelectedValue;
Line 72: 
Line 73:         //for DropDownList                                                                                                                      

法典-

    string name, city, gender;
    //id = int.Parse(((TextBox)this.GridView1.FooterRow.FindControl("TextBox1"))).Text);
    name = ((TextBox)(this.GridView1.FooterRow.FindControl("TextBox5"))).Text;
    //for RadioButtonLiist Control
    gender = ((RadioButtonList)(this.GridView1.FooterRow.FindControl("RadioButtonList2"))).SelectedValue;

    city = ((TextBox)(this.GridView1.FooterRow.FindControl("TextBox4"))).Text;
    cmd = new SqlCommand("insert into emp values('" + name + "','" + gender + "','" + city + "')", cn);
    cmd.ExecuteNonQuery();
    disp();

代码隐藏-

  <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing" 
        onrowupdating="GridView1_RowUpdating" ShowFooter="True" 
        onrowcancelingedit="GridView1_RowCancelingEdit">
        <Columns>
            <asp:CommandField ShowEditButton="True" />
            <asp:CommandField ShowDeleteButton="True" />
            <asp:TemplateField HeaderText="Id">
                <EditItemTemplate>
                    <asp:Label ID="Label2" runat="server" Text='<%# Bind("id") %>'></asp:Label>
                </EditItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                </FooterTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("id") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>

            <asp:TemplateField HeaderText="name">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox2" runat="server" Text='<%# bind("name") %>'></asp:TextBox>
                </EditItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
                </FooterTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label3" runat="server" Text='<%# bind("name") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>

            <asp:TemplateField HeaderText="gender">
                <EditItemTemplate>
                    <br />
                    <asp:DropDownList ID="DropDownList1" SelectedValue='<%# bind("gender") %>' runat="server">
                        <asp:ListItem>male</asp:ListItem>
                        <asp:ListItem>female</asp:ListItem>
                    </asp:DropDownList>
                </EditItemTemplate>
                <FooterTemplate>
                    <asp:RadioButtonList ID="RadiaButtonList2" runat="server" Height="18px" 
                        Width="84px">
                        <asp:ListItem>male</asp:ListItem>
                        <asp:ListItem>female</asp:ListItem>
                    </asp:RadioButtonList>
                </FooterTemplate>
                <ItemTemplate>
                    <br />
                    <asp:Label ID="Label4" runat="server" Text='<%# bind("gender") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>

            <asp:TemplateField HeaderText="city">
                <EditItemTemplate>
                    <asp:RadioButtonList ID="RadioButtonList1" runat="server">
                        <asp:ListItem>lucknow</asp:ListItem>
                        <asp:ListItem>kanpur</asp:ListItem>
                    </asp:RadioButtonList>
                </EditItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
                </FooterTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label5" runat="server" Text='<%# bind("city") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Add new item">
                <FooterTemplate>
                    <asp:LinkButton ID="lbInsert" runat="server" onclick="LinkButton1_Click">Insert</asp:LinkButton>
                </FooterTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

网格视图页脚行中的单选按钮

这是因为在下面的代码中,你拼错了 RadioButtonList2 :-

<asp:RadioButtonList ID="RadiaButtonList2" runat="server" Height="18px" 
                        Width="84px">
                        <asp:ListItem>male</asp:ListItem>
                        <asp:ListItem>female</asp:ListItem>
                    </asp:RadioButtonList>

因此,它无法找到该控件,并且您正在尝试调用 Null 引用上的属性!