简体   繁体   中英

linking to a radiobutton value, data is not binding, asp.net c#

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. (ie ?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 (ie 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);

    }


}  

In your code at Page_Load event before this line

RadioButtonList1.SelectedIndex = selected;

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM