2011-01-20 61 views
0

我有一个单选按钮页面和一个基于您的选择动态填充数据的textarea。单选按钮充当文章标题的列表,并在选择时看到文章的内容。修改radiobuttonlist的page_load方法

在我的页面加载方法中,我想让用户能够在浏览器中看到一个指向他们的价值的URL。这样他们可以链接到另一个来源的文章。

目前,我有方法可以让我链接到该按钮的选择,如果我手动键入下面的示例网址:

http://localhost/test/Articles_test.aspx?selected=1 
http://localhost/test/Articles_test.aspx?selected=2 

我想修改此,这样的URL出现在浏览器当进行单选按钮选择时。另外,如果未指定值参数,则在页面加载时默认为“0”索引。

protected void Page_Load(object sender, EventArgs e) 
{  
    if (!IsPostBack) 
    { 
     int selected; 

     if (int.TryParse(Request.QueryString["selected"], out selected)) 
      RadioButtonList1.SelectedIndex = selected; 
      RadioButtonList1.DataBind();   
    } 
} 



protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e) 
{ 

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

} 

回答

1

设置您的单选按钮列表以回复更改。然后,在处理程序中,做一个重定向到相应的URL:

protected void Page_Load(object sender, EventArgs e) 
{  
    int selected; 

    if (int.TryParse(Request.QueryString["selected"], out selected)) 
     RadioButtonList1.SelectedIndex = selected; 
     RadioButtonList1.DataBind();   
} 
protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e) 
{ 

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

感谢您的输入chris。任何代码片段? – 2011-01-20 19:22:04