RadioButtonList由后面的代码选择后回发问题。 [英] RadioButtonList Postback issue when selected by code behind.

查看:92
本文介绍了RadioButtonList由后面的代码选择后回发问题。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我目前面临的一个相当有趣的问题。我的页面上有一个单选按钮列表,其中有三个选项是,否和可能。它将AutoPostBack设置为true以触发SelectedIndexChanged事件。



< asp:RadioButtonList ID =rblTakeHolidayrunat =serverAutoPostBack =True
onselectedindexchanged =rblTakeHoliday_SelectedIndexChanged
RepeatDirection =Horizo​​ntal>
< asp:ListItem Value =是>是< / asp:ListItem>
< asp:ListItem Value =No> No< / asp:ListItem>
< asp:ListItem Value =Maybe>可能< / asp:ListItem>
< / asp:RadioButtonList>





用户从RadioButtonList中选择一个选项,并根据选择填充四个网格视图使用数据和用户可以从这些网格视图中选择一些行并提交此信息。到目前为止,它工作正常。



如果用户稍后再次访问同一页并想要将之前选择的假日偏好更改为新的内容,我想显示他之前的假期选择。所以我从数据库中取出他以前的选择,并在后面的代码中选择一个单选按钮(例如用户最初选择Maybe作为选项):



 rblTakeHoliday.Items [0] .Selected = false; 
rblTakeHoliday.Items [1] .Selected = false;
rblTakeHoliday.Items [2] .Selected = true;





现在用户正在播放页面上的这三个单选按钮。当用户选择是或否单选按钮时,将触发SelectedIndexChanged事件,但它不会触发Maybe单选按钮,因为它在浏览器中使用checked =checked呈现,因为我将其设置为Selected =truein代码背后。因此,例如,如果用户选择是选项,则会触发SelectedIndexChanged事件。如果用户返回可能选项,则不会触发SelectedIndexChaged事件。这有什么工作吗?我想让单选按钮列表在任何单选按钮选择上触发其SelectedIndexChanged事件。



我知道这是问题,因为在后面的代码中设置了单选按钮。

解决方案

hi ,



需要更多详细信息,说明您在页面后面设置值的状态,这个页面是:加载?如果有的话试试这个



if(!IsPostBack){//设置您的收音机按钮列表数据}



另一种解决方法是你设置数据的方式,你应该使用



 / /假设在数据库值MayBe被保存。将其分配给变量try to set 
String myValue =MayBe;
rblTakeHoliday.SelectedIndex =
rblTakeHoliday.Items.IndexOf(rblTakeHoliday.Items.FindByValue(myValue));





希望这会有所帮助。


显然,这是我在应用程序中使用的第三方AJAX RAD控件(Telerik Controls和这些控件的AjaxSettings部分)的问题。我不得不在面板中移动RadioButtonList并使用该面板ID作为Telerik的AjaxSettings的控件。



我创建了一个测试页面来测试它是否在没有AJAX的情况下发生,最终能够解决问题。


< pre lang =xml>我有类似的经验,其中radiobuttonlist会在一个选择上触发,但在我重新选择列表中的原始选择时则不会。这是一个奇怪的解决方案,但我不得不这样做。

< pre lang = c# >
protected void rblTakeHoliday_SelectedIndexChanged(object sender,EventArgs e)
{
foreach(radio.Items中的ListItem项)
{
if(item.Selected)
item.Selected = true;
}
}
< / pre >


This is rather interesting issue I am facing at the moment. I have a radio button list on my page with three options "Yes", "No" and "Maybe". It has AutoPostBack set to true to fire SelectedIndexChanged event.

<asp:RadioButtonList ID="rblTakeHoliday" runat="server" AutoPostBack="True"
                    onselectedindexchanged="rblTakeHoliday_SelectedIndexChanged"
                    RepeatDirection="Horizontal">
                    <asp:ListItem Value="Yes">Yes</asp:ListItem>
                    <asp:ListItem Value="No">No</asp:ListItem>
           <asp:ListItem Value="Maybe">Maybe</asp:ListItem>
                </asp:RadioButtonList>



User selects one of the option from RadioButtonList and based on selection four gridviews are populated with data and user can select some rows from those gridviews and submit this information. Up to this point it works fine.

If user later come to same page again and want to change holiday preference from whatever selected before to something new I want to display his previous holiday selection. So I fetch his previous selection from database and make one of the radio button selected in code behind as follow (For example user selected "Maybe" as an option initially):

rblTakeHoliday.Items[0].Selected = false;
rblTakeHoliday.Items[1].Selected = false;
rblTakeHoliday.Items[2].Selected = true;



Now user is playing with those three radio buttons on the page. The SelectedIndexChanged event will fire when user selects "Yes" or "No" radio button but it will not fire for "Maybe" radio button as it rendered with checked="checked" in browser because I set it as Selected= "true" in code behind. So for example If user selects option "Yes" the SelectedIndexChanged event will fire. If user go back to "Maybe" option the SelectedIndexChaged event will not get fired. Is there any work around to this ? I want to make radio button list fire its SelectedIndexChanged event on any radio button selection.

I know this is the issue because of setting radio button selected in code behind.

解决方案

hi,

Need more details on which state you are setting the values in page behind, is this Page:Load? if so try this

if(!IsPostBack){//SET YOUR RADIO BUTTON LIST DATA}

Another workaround is the way you are setting data, you should use

// Suppose at databse value "MayBe" is saved. Assign it into a variable the try to set
String myValue="MayBe";
rblTakeHoliday.SelectedIndex=
rblTakeHoliday.Items.IndexOf(rblTakeHoliday.Items.FindByValue(myValue));



Hope this will help.


Apparently it turn out to be an issue with third party AJAX RAD controls ( Telerik Controls and AjaxSettings section of those controls )I am using in the application. I had to move RadioButtonList inside panel and use that panel ID as control of AjaxSettings for Telerik.

I created a test page to test if it happens without AJAX or not and finally able to nail the problem.


I had a similar experience where the radiobuttonlist would fire on one selection but not when I reselected the orginal selection in the list.  This is a wierd fix, but I had to do this.

<pre lang="c#">
protected void rblTakeHoliday_SelectedIndexChanged(object sender, EventArgs e)
{
    foreach (ListItem item in radio.Items)
    {
        if (item.Selected)
           item.Selected = true;
    }
}
</pre>


这篇关于RadioButtonList由后面的代码选择后回发问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆