方法不会正确地为 selectindex 赋值,asp.net radiobuttonlist

发布于 2024-10-13 20:10:53 字数 2342 浏览 6 评论 0原文

我有一个带有单选按钮列表和文本区域的页面。数据根据用户的选择动态显示在文本区域内。我还设置了 OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged" 来创建一个允许用户引用其文章(单选按钮选择)的 URL。

除了剪切和粘贴创建的网址(即 http://test.com/test.aspx ?selected=3) 进入新浏览器。代码不断将 radiobuttonlist1.selectedindex 分配给 -1。

所以这就是我在调试模式情况1中看到的内容

,当我将网址剪切并粘贴到新浏览器时http://test.com/test.aspx?selected=1,在page_load方法代码末尾RadioButtonList1.SelectedIndex等于= -1。由于某种原因,它没有正确分配 selectindex。

情况 2 当我在启动的网页中选择一个单选按钮时,它会跳过 page_load 代码,因为回发为 true。然后在 RadioButtonList1_SelectedIndexChanged 中创建一个 url。然后运行页面加载方法并在最后保留正确的 RadioButtonList1.SelectedIndex 值。

情况 3 当我在启动的网页中选择一个指向 的链接时http://test.com/test.aspx?selected=2,回发为 false,因此它循环遍历 page_load 代码并在最后成功保存正确的 RadioButtonList1.SelectedIndex 值。

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

    }


}

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

    }


}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花伊自在美 2024-10-20 20:10:53

您需要反转调用以首先对单选按钮列表进行数据绑定,然后设置选定的索引。

例如,您可以重组为以下内容。如果需要数据绑定,可以放在我注释的地方。

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;

            }
        }
    }
}

注意:我强烈建议进一步清理这个问题,如果用户传递的“selectedindex”大于数据,您将在上述代码中得到异常。

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.

初熏 2024-10-20 20:10:53

我的会话参数没有在 SqlDataSource1_Selecting 上获取正确的值。我删除了代码并在 aspx 中硬编码了会话参数,以使我的代码正常工作。感谢大家的意见!我很高兴这件事结束了。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文