开发者

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. fo开发者_运维百科r 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);

    }


}


You need to reverse the calls to first databind the radiobuttonlist, THEN set the selected index.

For example you could restructure to the following. If you need to data bind, you can put it where I have the comment.

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        //Always bind the list to here, if needed

        if (Request.QueryString["selected"] != null)
        {
            int selected;
            if (int.TryParse(Request.QueryString["selected"], out selected))
            {   
                RadioButtonList1.SelectedIndex = selected;

            }
        }
    }
}

NOTE: I strongly recommend cleaning this up a bit further, if the user passes a "selectedindex" that is larger then the data you will get an exception with the above code.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜