Gridview页脚行中的单选按钮

时间:2014-10-27 07:12:20

标签: c# asp.net gridview ado.net

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

错误指向RadioButtonList,它位于GridView的页脚行中。你调用的对象是空的。 描述:执行当前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>

1 个答案:

答案 0 :(得分:0)

这是因为在以下代码中,您拼写错误 RadioButtonList2 : -

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

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

相关问题