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

发布于 2024-10-12 20:43:33 字数 2376 浏览 5 评论 0原文

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

此外,当用户进行单选按钮选择时,URL 中将包含一个扩展名,以显示他们选择了哪个选择索引号。 (即?selected=0)

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

这样他们就可以复制该网址并在其他网站中将其作为链接引用。或将其放入他们的收藏夹中。

问题是,如果您获取 URL 并打开新浏览器,页面不会相应地传递值和数据绑定。页面上没有显示单选按钮或内容。 我认为一定是回发逻辑???

工作原理:

  1. 当我启动网站时,单选按钮出现,并且索引 0 被设置,
  2. 当我选择单选按钮时,正确的数据显示,并且链接到单选按钮值的 URL 显示在浏览器中(即 http://test.com/test.aspx?selected=2)
  3. 如果我在同一浏览器中剪切并粘贴指针网址,则正确的数据是呈现出

不起作用的内容(所有处理错误回发的内容):

1.当我启动网站时,即使单选按钮设置为 0 索引并且可见,文本区域内也不会出现任何数据。 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);

    }


}  

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. (i.e. ?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

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. 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
  2. when i select radio buttons the correct data displays and urls linking to radio button values display in browser (i.e. 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):

1.when i launch website no data within the textarea apprears even though the radio button is set to 0 index and is visiable.
2. if i cut and paste pointer url into a new browser, text area and radio buttons do not display.

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

    }


}  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

做个ˇ局外人 2024-10-19 20:43:33

之前的 Page_Load 事件中,

在您的代码中,此行RadioButtonList1.SelectedIndex = selected;

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

In your code at Page_Load event before this line

RadioButtonList1.SelectedIndex = selected;

you should bind RadioButtonList1. after binding RadioButtonList you can set SelectedIndex.

一袭水袖舞倾城 2024-10-19 20:43:33

我的 SqlDataSource1_Selecting 方法是问题所在。我使用了另一种方法并且我的代码有效。

my SqlDataSource1_Selecting method was the issue. i used another approach and my code worked.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文