Search code examples
c#asp.netradiobuttonlist

method won't assign value to selectindex correctly, asp.net radiobuttonlist


I have a page with a radiobuttonlist and textarea. data is displayed dynamically within the textarea based on the user's selection. i also set up a OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged" to create a url that will allow users to reference their article (radio button selection).

everything works except for cutting and pasting the created url (i.e. http://test.com/test.aspx?selected=3) into a new browser. the code keeps assigning the radiobuttonlist1.selectedindex to -1.

so here's what i'm seeing in debug mode

Case 1 when i cut and past the url in to a new browser http://test.com/test.aspx?selected=1, at the end of the page_load method code RadioButtonList1.SelectedIndex is equal to = -1. for some reason it's' not assigning the selectindex correctly.

Case 2 when i select a radio button within the web page i launched, it skips the page_load code because post back is true. then creates a url within the RadioButtonList1_SelectedIndexChanged. then runs through the on page load method and hold the correct RadioButtonList1.SelectedIndex value at the end.

Case 3 when i select a link within the webpage launched that is using pointing to http://test.com/test.aspx?selected=2, postback is false so it loops though the page_load code and successfully hold the correct RadioButtonList1.SelectedIndex value at the end.

protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
               {

                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;


                }

            }



        } 



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

    }


}

Solution

  • My session parameter was not taking in the correct value on SqlDataSource1_Selecting. i removed the code which and hardcoded the session parameter in the aspx to get my code working correctly. thanks for everyone's input! i'm glad this one is over.