0

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

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

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

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

什么工作:

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

什么不起作用(处理虚假回发的一切):

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

    }


}  
4

2 回答 2

1

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

RadioButtonList1.SelectedIndex = selected;

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

于 2011-01-21T10:31:28.813 回答
0

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

于 2011-02-01T17:21:23.097 回答