format for url to point to radio button selection

2019-08-27 18:51发布

Possible Duplicate:
linking to a radio button selection, asp.net c#

i have a page with a textarea and radio buttons. the text area is populated with data based on the radio button selection. i want the radio button selection to appear in the url so that a user can link to the radio button selection.

i'm hoping that all i need to do i modify my querystring to include radio button value. here's the data caputered by fidler when i make a radio button selection.

 __EVENTTARGET  ctl00$MainContent$RadioButtonList1$6
__EVENTARGUMENT 
__LASTFOCUS 
__VIEWSTATE /+++PC9wPg0KPHA+/....
__EVENTVALIDATION   /wEWCwKY7d6oAQLh8vmTCALk7M7lDQK+6NunDwK/6OenDwK86OenDwK86OunDwK86POnDwK96NenDwK96NunDwKxh73KA3Q+PMuKU/JUCKsF1aiY2DNLu7/pFFni/Qtz+7FXy35g
ctl00$MainContent$RadioButtonList1  41

i'm hoping my url simply needs to look something like this to point to the radio button value but and all i need is the appropriate syntax:

http://www.test.com/test.aspx?ctl00$MainContent$RadioButtonList1$41

---code behind ---

protected void Page_Load(object sender, EventArgs e)
    {


        if (Page.IsPostBack == false)
        {


            RadioButtonList1.SelectedIndex = 0;
            RadioButtonList1.DataBind();

        }

        else
        {

            string strRedirect;

            strRedirect = "frm_Articles.aspx?Article_PK=" + RadioButtonList1.SelectedValue.ToString();

            Response.Redirect(strRedirect);


        }
    }  



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

     }


    }



}

1条回答
神经病院院长
2楼-- · 2019-08-27 19:08

One thing to note is that you have misconstructed your url. It should be:

http://www.test.com/test.aspx?ctl00$MainContent$RadioButtonList1=41

(note the third to last character is an =.

Apart from that it would depend on how your page works in terms of picking up the selected radio button. My advice would be to just try it and see what happens. However, I would suspect that if you are doing it based on an event firing on page load (ie some kind of postback behaviour) then it won't work.

If this is the case then you will just want to do something on page load to check if that value exists in the url and set it before you load up the text. If you end up going down this route you might want to consider using a more well defined and user friendly querystring parameter. In particular the id you have there is built based on its position in the control hierarchy. If you were to redesign your HTML structure the ID might well change.

Additionally...

With the code you've provided where you set the selectedindex of your radiobutton list you need to find out what value you need before you set it and set it to 0 if you have no better value (though it looks like you are setting to 0 before binding whcih seems superfluous). Something like this may work (not compiled or tested so apologies for typos - it is designed to give you the general idea more than to be finalised code).

if (Page.IsPostBack == false)
{
    RadioButtonList1.DataBind();
    //Check if you have a value to set.
    if (Request.Querystring(RadioButtonList1.ClientID)!=null)
    {
        //Get the value
        string setValue = Request.Querystring(RadioButtonList1.ClientID)
        //Find the right radio option to select
        foreach(ListItem item in RadioButtonList1.Items)
        {
            if (item.Value == setValue)
            {
                item.Selected = true;
                break;
            }
        }
    }

}

I assume it will default to the selected index being 0 if nothing is set as selected otherwise.

Anyway, this code is meant as a starting point to work from. There may be other ways to do it and some may even be better.

查看更多
登录 后发表回答