【问题标题】:selectedindexchanged event not firing in radiobuttonlistselectedindexchanged 事件未在单选按钮列表中触发
【发布时间】:2012-07-06 05:25:02
【问题描述】:

我在单选按钮列表上应用 onselectedindexchangedevent 但是当我点击 单选按钮,单选按钮没有选择运动,它选择,然后 取消选择。我也设置了 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;




    }

【问题讨论】:

    标签: .net radiobuttonlist


    【解决方案1】:

    把服务器端代码改成这样:

    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”

    【讨论】:

    • 我做到了,但仍然没有触发事件
    • 我添加了完整的服务器端代码和页面代码,它对我有用。
    猜你喜欢
    • 1970-01-01
    • 2013-03-29
    • 1970-01-01
    • 2011-03-17
    • 1970-01-01
    • 2016-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多