简体   繁体   English

链接到单选按钮值,数据未绑定,asp.net c#

[英]linking to a radiobutton value, data is not binding, asp.net c#

hi i need to modify my code a little bit. 嗨,我需要修改我的代码一点。 i have a page with a radio button list and a textarea. 我有一个带有单选按钮列表和文本区域的页面。 the textarea is populated when a users makes a radio button selection. 用户选择单选按钮时,将填充文本区域。

also, when a user makes a radio button selection the url will hold an extention in the url to show which selection index number they have selection. 同样,当用户选择单选按钮时,URL将在URL中保留一个扩展名以显示他们具有选择的选择索引号。 (ie ?selected=0) (即?selected = 0)

http://test.com/frm_Articles.aspx?selected=0 http://test.com/frm_Articles.aspx?selected=1 http://test.com/frm_Articles.aspx?selected=2 http://test.com/frm_Articles.aspx?selected=0 http://test.com/frm_Articles.aspx?selected=1 http://test.com/frm_Articles.aspx?selected=2

that way they can copy the url and reference it in other websites as a link. 这样,他们可以复制该网址,并在其他网站中将其作为链接引用。 or place it in their favorites. 或将其放置在他们的收藏夹中。

the problem is, if you grab the url and open a new browser, the page does not pass the value and databind accordingly. 问题是,如果您获取URL并打开新的浏览器,则该页面不会传递该值,并且不会相应地进行数据绑定。 no radio buttons or content appear on the page. 页面上没有单选按钮或内容。 must be the postback logic i think??? 一定是我认为的回发逻辑???

what's working: 工作原理:

  1. when i launch the website the radio buttons appear and index 0 is set 当我启动网站时,出现单选按钮并设置索引0
  2. when i select radio buttons the correct data displays and urls linking to radio button values display in browser (ie http://test.com/test.aspx?selected=2 ) 当我选择单选按钮时,将显示正确的数据,并在浏览器中显示链接到单选按钮值的URL(即http://test.com/test.aspx?selected=2
  3. if i cut and paste pointer urls within the same browser then correct data is rendered 如果我在同一浏览器中剪切并粘贴了指针网址,则呈现正确的数据

what doesn't work (everything that deal with an false PostBack): 什么不起作用(处理错误的PostBack的所有内容):

1.when i launch website no data within the textarea apprears even though the radio button is set to 0 index and is visiable. 1.当我启动网站时,即使单选按钮设置为0索引并且可见,textarea内也没有数据。 2. if i cut and paste pointer url into a new browser, text area and radio buttons do not display. 2.如果我将指针网址剪切并粘贴到新的浏览器中,则不会显示文本区域和单选按钮。

    protected void Page_Load(object sender, EventArgs e)
    {


            if (Page.IsPostBack == false)
            {

                int selected;


                if (Request.QueryString["selected"] != null)
                {


                    if (int.TryParse(Request.QueryString["selected"], out selected))
                    {   


                        RadioButtonList1.SelectedIndex = selected;
                        RadioButtonList1.DataBind();


                    }



                }
                else
                {

                    int firstart = 0;      

                    RadioButtonList1.SelectedIndex = firstart;
                    RadioButtonList1.DataBind();   

                }

            }


    }


    protected void SqlDataSource2_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
    {

  //    

    }
    protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
    {
        try{
        e.Command.Parameters["@URL_FK"].Value =  Session["URL_PK"];


        }
     catch (Exception ex)
     {

     }


    }


    protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
    {


           string strRedirect;
           strRedirect = "test.aspx?selected=" + RadioButtonList1.SelectedIndex;  
           Response.Redirect(strRedirect);

    }


}  

In your code at Page_Load event before this line 在您的代码中此行之前的Page_Load事件中

RadioButtonList1.SelectedIndex = selected;

you should bind RadioButtonList1. 您应该绑定RadioButtonList1。 after binding RadioButtonList you can set SelectedIndex . 绑定RadioButtonList之后,可以设置SelectedIndex

my SqlDataSource1_Selecting method was the issue. 我的SqlDataSource1_Selecting方法就是问题所在。 i used another approach and my code worked. 我使用了另一种方法,我的代码工作了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM