在Asp.net';对象引用未设置为对象的实例';在更改网格视图中的单选按钮列表时选择了error

本文关键字:单选按钮 视图 列表 error 选择 网格 对象引用 net 设置 实例 Asp | 更新日期: 2023-09-27 18:00:09

我的设计页面在项目模板中有一个RadioButtonList,它有一个事件OnSelectedIndexChanged。如果此事件被触发,我会得到错误

object reference not set to an instance of an object

也就是说,即使我已经设置了值,我也会在单选按钮列表中获得null值。

<asp:GridView ID="gdvHomeGenlist" runat="server" AutoGenerateColumns="False"
    BackColor="White" BorderColor="#3366CC" BorderStyle="None"
    BorderWidth="1px" CellPadding="4" Height="12px"
    ShowFooter="True" Width="335px"
    OnRowCommand="gdvHomeGenlist_RowCommand">
  <Columns>
    <asp:TemplateField HeaderText="Attendance">
      <ControlStyle Width="200px" />
      <HeaderStyle Font-Bold="True" Font-Size="8pt" Width="10px" />
      <ItemTemplate>             
        <asp:RadioButtonList ID="rdbattnd" runat="server"
            RepeatDirection="Horizontal"
            OnSelectedIndexChanged="rdbattnd_SelectedIndexChanged"
            AutoPostBack="True">
          <asp:ListItem  Value="1">Attended</asp:ListItem>
          <asp:ListItem Value="2">Not attended</asp:ListItem>
        </asp:RadioButtonList>
        <asp:TextBox ID="txtbxHmListGenRsnForNotAttdSpcify" runat="server" ></asp:TextBox>
      </ItemTemplate>
      <ItemStyle Font-Size="8pt" />
    </asp:TemplateField> 
  </Columns>
  <RowStyle BackColor="#EFF3FB" />
  <EditRowStyle BackColor="#2461BF" />
  <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
  <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
  <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
  <AlternatingRowStyle BackColor="White" />
</asp:GridView>

代码背后:

protected void rdbattnd_SelectedIndexChanged(object sender, EventArgs e)
{
    RadioButtonList rdbAttendance =
      (RadioButtonList)gdvHomeGenlist.FindControl("rdbattnd");    ///fetching null   
    /// error object reference is not set to an
    /// instance of an object
    if (rdbAttendance.SelectedValue == "1")
    {
            // some action
    }
}

在Asp.net';对象引用未设置为对象的实例';在更改网格视图中的单选按钮列表时选择了error

试试这个:

protected void rdbattnd_SelectedIndexChanged(object sender, EventArgs e)
{
    var rdbAttendance = ((RadioButtonList)sender);
    //if (rdbAttendance.SelectedValue == "1")of an object       
    //{
    //}
}