0

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

object reference not set to an instance of an object

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

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

1 回答 1

0

尝试这个:

protected void rdbattnd_SelectedIndexChanged(object sender, EventArgs e)
{
    var rdbAttendance = ((RadioButtonList)sender);

    //if (rdbAttendance.SelectedValue == "1")of an object       
    //{
    //}
}
于 2013-02-19T11:35:48.410 回答