Question

I have 2 issues. On an aspx page, I have 3 RadioButtonLists (aka RBL)that gets each of their data from a database table. Upon selection on any RBL, a postback occurs to the same page and the value of that specific RBL gets entered into the querystring, that a ListView is looking for, to filter out products based on the querystring. The List View works fine when it reads the querystring an properly filters the list down.

Issue 1 - On postback, the value that you selected from any of the 3 RBL's, doesn't get selected when the page loads. So all my RBL's don't have any value, even if I have a default value set on the page. How do I get my page to reload with the values selected in the RBL's that you've chosen?

Issue 2 - If you make a selection on any of the other 2 RBL's, instead of updating the querystring, it wipes out the first value when it posts back again. So if you pick something in RBL-1, it posts back with that specific field updated in the querystring, but then if you pick something in RBL-2, it posts back but only the value from RBL-2, and wipes out the value that you just picked from RBL-1. How can I get my page to load, while keeping any of your prior selections in the querystring?

ASPX code:
        <p>Normally Open or Closed:<asp:RadioButtonList ID="RadioButtonList1" runat="server" EnableViewState="true" 
                AutoPostBack="true" DataSourceID="NormalitySDS" DataTextField="Op"
                DataValueField="Op" onselectedindexchanged="RadioButtonAllLists_SelectedIndexChanged">
            </asp:RadioButtonList>
            <asp:SqlDataSource ID="NormalitySDS" runat="server" 
                ConnectionString="<%$ 005 %>" 
                SelectCommand="SELECT DISTINCT [Op] FROM [Matrix] ORDER BY [Op]">
            </asp:SqlDataSource>
        </p>
        <p>Sizes:<asp:RadioButtonList ID="RadioButtonList2" runat="server" EnableViewState="true" 
                AutoPostBack="True" DataSourceID="SizesSDS" DataTextField="SIZE" RepeatColumns="2" 
                DataValueField="SIZE" onselectedindexchanged="RadioButtonAllLists_SelectedIndexChanged">
            </asp:RadioButtonList>
            <asp:SqlDataSource ID="SizesSDS" runat="server" 
                ConnectionString="<%$ 005 %>" 
                SelectCommand="SELECT DISTINCT [SIZE] FROM [Matrix] ORDER BY [SIZE]">
            </asp:SqlDataSource>
        </p>
        <p>Body:<asp:RadioButtonList ID="RadioButtonList3" runat="server" EnableViewState="true" 
                AutoPostBack="True" DataSourceID="BodySDS" DataTextField="Body" 
                DataValueField="Body" OnSelectedIndexChanged="RadioButtonAllLists_SelectedIndexChanged">
            </asp:RadioButtonList>
            <asp:SqlDataSource ID="BodySDS" runat="server" 
                ConnectionString="<%$ 005 %>" 
                SelectCommand="SELECT DISTINCT [Body] FROM [Matrix] ORDER BY [Body]">
            </asp:SqlDataSource>
      <SelectParameters>
                    <asp:QueryStringParameter DefaultValue="NC" Name="Op" QueryStringField="Op" Type="String" />
                    <asp:QueryStringParameter DefaultValue="0.25" Name="Sz" QueryStringField="Sz" Type="String" />
                    <asp:QueryStringParameter DefaultValue="304" Name="Body" QueryStringField="Body" Type="String" />
      </SelectParameters>

Code-behind:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Configurator
{
    public partial class Product_Config_Full_wQuery : System.Web.UI.Page
    {
        string BaseUrl = "/Product_Config_Full_wQuery.aspx";
        string op;
        string op2;
        string sz;
        string body;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                op = (Server.UrlDecode(Request.QueryString["op"] ));
                RadioButtonList1.SelectedIndex = op2;
                RadioButtonList1.DataBind();


                sz = Server.UrlDecode(Request.QueryString["sz"]);
                body = Server.UrlDecode(Request.QueryString["body"]);
            }
        }


        // Combining all actions into a single protected-event
        protected void RadioButtonAllLists_SelectedIndexChanged(object sender, EventArgs e)
        {
            op = RadioButtonList1.SelectedValue.ToString();
            sz = RadioButtonList2.SelectedValue.ToString();
            body = RadioButtonList3.SelectedValue.ToString();

            if (op != null)
            {
                BaseUrl += "?Op=" + op + "&";
            }
            //else op = "NC";

            if (sz != null)
            {
                BaseUrl += "Sz=" + sz + "&";
            }

            if (body != null)
            {
                BaseUrl += "Body=" + body + "&";
            }
            Response.Redirect(string.Format(BaseUrl, Server.UrlEncode(op), Server.UrlEncode(sz), Server.UrlEncode(body)));
        }
Was it helpful?

Solution 2

I figured I'd post up the code, I used to fix my own issue. Based on about 10 different website sources and some trial/error.

    public partial class Bla zay blaz : System.Web.UI.Page
{


    string BaseUrl = "/blahblah.aspx?";
    string op;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            op = Server.HtmlDecode(Request.QueryString["op"]);
            if (op == null)
                op = "A";
            RadioButtonList1.SelectedValue = op;
}

    protected void RadioButtonChanged(object sender, EventArgs e)
    {
        op = RadioButtonList1.SelectedValue;
        if (op == null)
            op = "NC";

        if (op != "A")
            BaseUrl += "Op=" + op + "&";
Response.Redirect(string.Format(BaseUrl, Server.HtmlEncode(op));
}

}

OTHER TIPS

Since you're dynamically setting the values inside the radio button list, the values the page would be setting those controls to aren't actually there on postback (page lifecycle would be trying to set values into those controls before your data sources would actually retrieve the possible values and add those items into the control itself).

Also, stop using the querystring to hold postback values - if you really need to, set up some HiddenFields to hold information, those will persist through postbacks and you won't have to worry about odd querystring issues

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top