selectedindexchanged事件未在radiobuttonlist中触发

时间:2012-07-05 17:37:35

标签: .net radiobuttonlist

我在radiobuttonlist上申请onselectedindexchangedevent,但是当我点击时       radiobuttton,radiobutton不选择运动,它选择,然后       取消选择。我还设置了postback = true。但它没有触发..

**.aspx** 

  <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
                <Columns>
                <asp:TemplateField>
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
                    <asp:RadioButtonList ID="RadioButtonList1" runat="server"   
AutoPostBack="true"RepeatDirection="Horizontal"OnSelectedIndexChanged="clicked"> 

                <asp:ListItem Value="agree" Selected="True" ></asp:ListItem>
                   <asp:ListItem Value="agree"></asp:ListItem>
                    </asp:RadioButtonList>

                </ItemTemplate>

                </asp:TemplateField>

                </Columns>
                </asp:GridView>


**.aspx.cs**



 public void clicked(object sender, EventArgs arg)
    {

        test t = new test();
        questiondal d = new questiondal();

        GridViewRow row= (( RadioButtonList  )sender).NamingContainer as GridViewRow;
  RadioButtonList list= (RadioButtonList )row.FindControl("Radio");
list.SelectedIndexChanged();
 Label4.Text= list.SelectedValue;




    }

1 个答案:

答案 0 :(得分:0)

将服务器端代码更改为以下内容:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        GridView1.DataSource = new List<string> {"test"};
        GridView1.DataBind();
    }      
}
public void clicked(object sender, EventArgs arg)
{
    RadioButtonList list = (RadioButtonList)sender;     
    var t = list.SelectedValue;
    Label1.Text = t;
}  

并为不同的列表项设置不同的值,如下所示:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>                    
                <asp:RadioButtonList ID="RadioButtonList1" runat="server" RepeatDirection="Horizontal"
                    OnSelectedIndexChanged="clicked" AutoPostBack="true" >
                    <asp:ListItem Value="agree1" Text="a"></asp:ListItem>
                    <asp:ListItem Value="agree2" Text="b"></asp:ListItem>
                    <asp:ListItem Value="agree3" Text="c"></asp:ListItem>
                </asp:RadioButtonList>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

因此,如果用户点击第一项,Label4.Text将是“agree1”,如果用户点击第二项,那么它将是“agree2”