指向单选按钮选择的网址格式 [英] format for url to point to radio button selection

查看:56
本文介绍了指向单选按钮选择的网址格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
链接到单选按钮选择,asp.net c#

我有一个带有文本区域和单选按钮的页面.文本区域将根据单选按钮的选择填充数据.我希望单选按钮选择出现在url中,以便用户可以链接到单选按钮选择.

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.

我希望我需要做的所有事情都可以修改我的查询字符串以包含单选按钮值.这是我选择单选按钮时,fidler烦恼的数据.

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

--- ---

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

     }


    }



}

推荐答案

要注意的一件事是您构造了错误的URL.应该是:

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.

如果是这种情况,那么您只想在页面加载时执行一些操作,以检查该值是否存在于url中并在加载文本之前进行设置.如果您最终选择了这条路线,则可能需要考虑使用定义更明确且用户友好的querystring参数.特别是,您在那里拥有的ID是根据其在控件层次结构中的位置构建的.如果您要重新设计HTML结构,则ID可能会发生变化.

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.

此外...

使用您提供的代码设置单选按钮列表的selectedindex,您需要先找到所需的值,然后再将其设置为0(如果没有更好的值,则将其设置为0)(尽管看起来像是在绑定之前将其设置为0似乎是多余的).这样的事情可能会起作用(未经编译或测试,因此对错别字表示歉意-它旨在为您提供一般概念,而不是最终代码).

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

}

如果没有设置任何其他设置,我假定它将默认为选定索引为0.

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.

这篇关于指向单选按钮选择的网址格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆