修改radiobuttonlist的page_load方法

发布于 2024-10-13 05:12:51 字数 952 浏览 5 评论 0原文

我有一个带有单选按钮和一个文本区域的页面,可以根据您的选择动态填充数据。单选按钮充当文章标题列表,选择后您会看到文章的内容。

在我的页面加载方法中,我希望允许用户能够在浏览器中看到指向他们所拥有的值的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

流星番茄 2024-10-20 05:12:51

设置您的单选按钮列表以在更改时回发。然后,在处理程序中,重定向到适当的 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);
}

Set your radiobutton list to post back on change. Then, in the handler, do a redirect to the appropriate 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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文