修改radiobuttonlist的page_load方法
我有一个带有单选按钮和一个文本区域的页面,可以根据您的选择动态填充数据。单选按钮充当文章标题列表,选择后您会看到文章的内容。
在我的页面加载方法中,我希望允许用户能够在浏览器中看到指向他们所拥有的值的 URL。这样他们就可以链接到另一个来源中的文章。
目前,如果我手动键入以下示例 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);
}
I have a page with radio buttons and a textarea that populates data dynamically based on your selection. The radio buttons act as a list of article titles and on selection you see the content of the article.
Within my pageload method, I want to allow users to be able to see a URL in their browser that points to value they've. That way they can link to the article within another source.
Currently, the method I have allows me to link to the button selection if I manually type in the following example URLs:
http://localhost/test/Articles_test.aspx?selected=1
http://localhost/test/Articles_test.aspx?selected=2
I'd like to modify this so that the URL appears in the browser when a radio button selection is made. Plus, on page load defaults to the "0" index if no value parameter was specified.
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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
设置您的单选按钮列表以在更改时回发。然后,在处理程序中,重定向到适当的 URL:
Set your radiobutton list to post back on change. Then, in the handler, do a redirect to the appropriate URL: