首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >selectedindexchanged事件未在单选按钮列表中触发

selectedindexchanged事件未在单选按钮列表中触发
EN

Stack Overflow用户
提问于 2012-07-06 01:37:35
回答 1查看 3K关注 0票数 0

我在单选按钮列表上应用了onselectedindexchangedevent,但当我单击单选按钮时,单选按钮没有选择移动,它选择了,然后取消选择.I也设置了postback=true.but它不触发。

代码语言:javascript
复制
**.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;




    }
EN

回答 1

Stack Overflow用户

发布于 2012-07-06 03:13:04

将服务器端代码更改为如下所示:

代码语言:javascript
复制
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;
}  

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

代码语言:javascript
复制
<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”。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11349591

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档