链接到radiobutton值,数据没有绑定,asp.net c#

时间:2011-01-21 09:07:51

标签: c# asp.net radiobuttonlist

嗨我需要稍微修改一下我的代码。我有一个带有单选按钮列表和textarea的页面。当用户选择单选按钮时,将填充textarea。

另外,当用户选择单选按钮时,url将在URL中保留一个扩展名,以显示他们选择的选择索引号。 (即?选择= 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

他们可以通过这种方式复制网址并将其作为链接引用到其他网站中。或者把它放在他们的最爱。

问题是,如果你抓住网址并打开一个新的浏览器,页面就不会相应地传递值和数据绑定。页面上不显示单选按钮或内容。 必须是我认为的回发逻辑???

什么在起作用:

  1. 当我启动网站时,会出现单选按钮并设置索引0
  2. 当我选择单选按钮时,显示正确的数据,并在浏览器中显示链接到单选按钮值的网址(即http://test.com/test.aspx?selected=2
  3. 如果我在同一浏览器中剪切并粘贴指针网址,则会呈现正确的数据
  4. 什么不起作用(处理错误的PostBack的一切):

    1.当我启动网站时,即使单选按钮设置为0索引且可见,也不会在textarea中显示数据。 2.如果我将指针URL剪切并粘贴到新浏览器中,则不会显示文本区域和单选按钮。

        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);
    
        }
    
    
    }  
    

2 个答案:

答案 0 :(得分:1)

在此行之前Page_Load事件的代码中

RadioButtonList1.SelectedIndex = selected;

你应该绑定RadioButtonList1。绑定RadioButtonList后,您可以设置SelectedIndex

答案 1 :(得分:0)

我的SqlDataSource1_Selecting方法就是问题所在。我使用了另一种方法,我的代码也运行了。