RadiobuttonList在ASP.NET中,所选索引始终设置为0,C# [英] RadiobuttonList Selected index always set to 0 in ASP.NET,C#

查看:93
本文介绍了RadiobuttonList在ASP.NET中,所选索引始终设置为0,C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在ASP.NET和C#中创建了一个Web应用程序。

我有4个文本框用于Choice1,Choice 2,Choice3和Choice4。 />
和,带有4个项目的Radiobuttonlist,A,B,C,D

我想如果用户在radiobuttonlist中选择选项A,那么它的值应该等于选择1文本框,如果用户在radiobuttonlist中选择选项B,然后它的值应该等于选择2文本框等等。

i尝试if if循环为此,但无论我选择什么选项,radiobuttonlist选择索引始终保持为0。

如何获得radiobuttonlist的选定索引?

Hi ,
I am creating a web application in ASP.NET and C#.
I have 4 textboxes for Choice1, Choice 2, Choice3 and Choice4.
And, Radiobuttonlist with 4 items, A,B,C,D
I want if user select option A in radiobuttonlist, then its value should be equal to choice 1 textbox, if user select Option B in radiobuttonlist, then its value should be equal to Choice 2 textbox and so on..
i tried if else loop for this, but whatever option i select, radiobuttonlist selected index always remains 0.
How to get selected index of radiobuttonlist??

推荐答案







设计页面中的代码...



Hi,


Code in design page...

<asp:radiobuttonlist id="RadioButtonList1" runat="server" autopostback="True" xmlns:asp="#unknown">
            onselectedindexchanged="RadioButtonList1_SelectedIndexChanged">
        <asp:listitem>Choice1</asp:listitem>
        <asp:listitem>choice2</asp:listitem>
        <asp:listitem>choice3</asp:listitem>
        <asp:listitem>choice4</asp:listitem>
        </asp:radiobuttonlist>





我在用户更改索引时引发服务器事件。



检索事件代码来自募集活动的selectedindex





I raise server event on index changed by user.

event code for retrieving selectedindex from raised event

protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
{
    int i = RadioButtonList1.SelectedIndex;
}





如果您更改了索引,则会检查此事件是否会检查索引值。 />
刚才我工作了。对我来说效果很好。



如果对你的代码发布任何疑问,它可以帮你解决问题



我希望你能理解我说的话。





全部最好。



If you changed index this event'll be raise thn check for index values.
Just now I worked on it.it works fine for me.

If any doubts just post with your code it can helps to solve your problem

I hope you understand what I said.


All the best.


你好akkanshagupta,

我提供广泛的解释,如果仍然有任何疑问,请给我一个查询。

设计:

Hi akkanshagupta,
I am providing Broad Explanation,if still any doubt send me a query.
Design:
<table align="center" class="style1">
            <tr>
                <td class="style2">
                    <asp:radiobuttonlist id="RadioButtonList1" runat="server" autopostback="True" xmlns:asp="#unknown">
                        onselectedindexchanged="RadioButtonList1_SelectedIndexChanged" 
                        RepeatDirection="Horizontal">
                        <asp:listitem>India
                        <asp:listitem>Australia
                        <asp:listitem>U.S.A
                        <asp:listitem>Germany
                    
                </td>
                <td>
                     </td>
            </tr>
            <tr>
                <td class="style2">
                    Srinivas From:</td>
                <td>
                    <asp:textbox id="TextBox1" runat="server" xmlns:asp="#unknown">
                </td>
            </tr>
            <tr>
                <td class="style2">
                    Jhon From:</td>
                <td>
                    <asp:textbox id="TextBox2" runat="server" xmlns:asp="#unknown">
                </td>
            </tr>
            <tr>
                <td class="style2">
                    Obama From</td>
                <td>
                    <asp:textbox id="TextBox3" runat="server" xmlns:asp="#unknown">
                </td>
            </tr>
            <tr>
                <td class="style2">
                    Hitlor From:</td>
                <td>
                    <asp:textbox id="TextBox4" runat="server" xmlns:asp="#unknown">
                </td>
            </tr>
        </table>





Code Behind:



Code Behind:

protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
  {
      TextBox1.Text = "";
      TextBox2.Text = "";
      TextBox3.Text = "";
      TextBox4.Text = "";
      if (RadioButtonList1.SelectedIndex == 0)
      {
          TextBox1.Text = RadioButtonList1.SelectedItem.Text;
      }
      else if (RadioButtonList1.SelectedIndex == 1)
      {
          TextBox2.Text = RadioButtonList1.SelectedItem.Text;
      }
      else if (RadioButtonList1.SelectedIndex == 2)
      {
          TextBox3.Text = RadioButtonList1.SelectedItem.Text;
      }
      else
          TextBox4.Text = RadioButtonList1.SelectedItem.Text;
  }


看来你正在填充页面加载的RadioButtonList -

如果所以 - 确保你用一个

If / Then / Postback块包围你的RadioButtonList的人口:

如果不是Page.IsPostBack那么

'填充你的RBL

结束如果



例如:



if( !IsPostBack)

{

loadradiobuttonlist();

}
It seems that you're populating the RadioButtonList on the page load -
If so - make sure you surround your population of the RadioButtonList with an
If/Then/Postback block:
if not Page.IsPostBack then
' populate your RBL
end if

eg:

if (!IsPostBack)
{
loadradiobuttonlist();
}


这篇关于RadiobuttonList在ASP.NET中,所选索引始终设置为0,C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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