Understanding query strings

Discussion in 'ASP .Net Web Controls' started by Rickey Whitworth, Dec 7, 2005.

  1. I have a page with two web controls - a drop-down list of server names and a
    gridview that shows details about that server. The gridview is using the
    drop down list as a parameter, so changing the selected server in the
    drop-down list causes a postback and the gridview updates to the new server.

    What I would like to do is add the ability to pass a querystring to the page
    similar to the following:

    http://somewhere.com/servers.aspx?server=server1

    so that when the page loads, server1 is pre-selected. I also want the
    ability to change the server name once I get to the page if I need to.

    I thought about binding the gridview to the querystring, but I cannot figure
    out how to make the querystring change on postback.
    Rickey Whitworth, Dec 7, 2005
    #1
    1. Advertising

  2. If you want the querystring to change, why don't you just use a
    response.redirect to send the user to the same page, such as in the
    following:

    Response.Redirect('http://somewhere.com/servers.aspx?server=' &
    servernamevariable)

    If there is other information you want to keep, you can place them in a
    Session variable.

    If you need help in using querystrings, feel free to ask. Good Luck!
    --
    Nathan Sokalski

    http://www.nathansokalski.com/

    "Rickey Whitworth" <> wrote in message
    news:ev1nvr0%...
    >I have a page with two web controls - a drop-down list of server names and
    >a gridview that shows details about that server. The gridview is using the
    >drop down list as a parameter, so changing the selected server in the
    >drop-down list causes a postback and the gridview updates to the new
    >server.
    >
    > What I would like to do is add the ability to pass a querystring to the
    > page similar to the following:
    >
    > http://somewhere.com/servers.aspx?server=server1
    >
    > so that when the page loads, server1 is pre-selected. I also want the
    > ability to change the server name once I get to the page if I need to.
    >
    > I thought about binding the gridview to the querystring, but I cannot
    > figure out how to make the querystring change on postback.
    >
    Nathan Sokalski, Dec 7, 2005
    #2
    1. Advertising

  3. Hi Rickey,

    For the GridView control, it can accept the filtering parameter only from
    single source(control or querystring...), so would still use the
    DropDownList as the filtering control to change the GridView's displaying
    data when postback? If so, I think we'd better remain using the
    Dropdownlist as the GridView's select parameter source.... And as for
    querystring , since we just use it to initialize the Dropdownlist when the
    page first time load, so I think we can just manually add some code to
    parse the querystring and set the DropDownList's selected value in
    Page_Load (when not postback....). For example:

    protected void Page_Load(object sender, EventArgs e)
    {
    if (!IsPostBack)
    {
    string cid = Request.QueryString["cid"];
    if (cid != null && cid != string.Empty)
    {
    DropDownList1.SelectedValue = cid;
    }
    }
    }

    Hope helps. Thanks,

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)

    --------------------
    | From: "Rickey Whitworth" <>
    | Subject: Understanding query strings
    | Date: Wed, 7 Dec 2005 10:25:19 -0600
    | Lines: 17
    | X-Priority: 3
    | X-MSMail-Priority: Normal
    | X-Newsreader: Microsoft Outlook Express 6.00.2900.2527
    | X-RFC2646: Format=Flowed; Original
    | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2527
    | Message-ID: <ev1nvr0#>
    | Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
    | NNTP-Posting-Host: 66.182.150.119
    | Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
    | Xref: TK2MSFTNGXA02.phx.gbl
    microsoft.public.dotnet.framework.aspnet.webcontrols:31607
    | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
    |
    | I have a page with two web controls - a drop-down list of server names
    and a
    | gridview that shows details about that server. The gridview is using the
    | drop down list as a parameter, so changing the selected server in the
    | drop-down list causes a postback and the gridview updates to the new
    server.
    |
    | What I would like to do is add the ability to pass a querystring to the
    page
    | similar to the following:
    |
    | http://somewhere.com/servers.aspx?server=server1
    |
    | so that when the page loads, server1 is pre-selected. I also want the
    | ability to change the server name once I get to the page if I need to.
    |
    | I thought about binding the gridview to the querystring, but I cannot
    figure
    | out how to make the querystring change on postback.
    |
    |
    |
    Steven Cheng[MSFT], Dec 8, 2005
    #3
  4. Thank you, that is what I was trying previously, but I thought it might be
    the wrong approach. It was working, then I started getting this error:

    An error has occurred because a control with id
    'ctl00$ContentPlaceHolder1$GridView2$ctl04$ctl00' could not be located or a
    different control is assigned to the same ID after postback. If the ID is
    not assigned, explicitly set the ID property of controls that raise postback
    events to avoid this error.

    I added an @Trace directive to the page to identify which control
    ctl04$ctl100 was, and determined it was the pager control. If I disable
    paging on the gridview, the problem goes away.

    The problem only happens in this situation:
    1) I navigate to the page using a querystring (which then selects the
    appropriate value from the dropdown list)
    2) I then select a different value from the drop down list

    the error says to explicitly set ids of controls that cause postback, and I
    assume a pager control causes postback, but how do I set its ID?

    "Steven Cheng[MSFT]" <> wrote in message
    news:%23B99c95%...
    > Hi Rickey,
    >
    > For the GridView control, it can accept the filtering parameter only from
    > single source(control or querystring...), so would still use the
    > DropDownList as the filtering control to change the GridView's displaying
    > data when postback? If so, I think we'd better remain using the
    > Dropdownlist as the GridView's select parameter source.... And as for
    > querystring , since we just use it to initialize the Dropdownlist when the
    > page first time load, so I think we can just manually add some code to
    > parse the querystring and set the DropDownList's selected value in
    > Page_Load (when not postback....). For example:
    >
    > protected void Page_Load(object sender, EventArgs e)
    > {
    > if (!IsPostBack)
    > {
    > string cid = Request.QueryString["cid"];
    > if (cid != null && cid != string.Empty)
    > {
    > DropDownList1.SelectedValue = cid;
    > }
    > }
    > }
    >
    > Hope helps. Thanks,
    >
    > Steven Cheng
    > Microsoft Online Support
    >
    > Get Secure! www.microsoft.com/security
    > (This posting is provided "AS IS", with no warranties, and confers no
    > rights.)
    >
    > --------------------
    > | From: "Rickey Whitworth" <>
    > | Subject: Understanding query strings
    > | Date: Wed, 7 Dec 2005 10:25:19 -0600
    > | Lines: 17
    > | X-Priority: 3
    > | X-MSMail-Priority: Normal
    > | X-Newsreader: Microsoft Outlook Express 6.00.2900.2527
    > | X-RFC2646: Format=Flowed; Original
    > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2527
    > | Message-ID: <ev1nvr0#>
    > | Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
    > | NNTP-Posting-Host: 66.182.150.119
    > | Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
    > | Xref: TK2MSFTNGXA02.phx.gbl
    > microsoft.public.dotnet.framework.aspnet.webcontrols:31607
    > | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
    > |
    > | I have a page with two web controls - a drop-down list of server names
    > and a
    > | gridview that shows details about that server. The gridview is using the
    > | drop down list as a parameter, so changing the selected server in the
    > | drop-down list causes a postback and the gridview updates to the new
    > server.
    > |
    > | What I would like to do is add the ability to pass a querystring to the
    > page
    > | similar to the following:
    > |
    > | http://somewhere.com/servers.aspx?server=server1
    > |
    > | so that when the page loads, server1 is pre-selected. I also want the
    > | ability to change the server name once I get to the page if I need to.
    > |
    > | I thought about binding the gridview to the querystring, but I cannot
    > figure
    > | out how to make the querystring change on postback.
    > |
    > |
    > |
    >
    Rickey Whitworth, Dec 8, 2005
    #4
  5. How do I pre-select items from a drop-down list when it is also databound?

    Currently, I pre-select in the prerender event, because it wasn't working in
    the load event.
    Then I noticed that even though I was pre-selecting the value in the
    drop-down list, the gridview was not updated to reflect the change. So I
    also added this code

    dsLogicalLogView.Select(New UI.DataSourceSelectArguments())


    This refreshes the sqldatasource and the gridview is updated when the page
    displays. But I wonder if this is causing the other problem I posted with
    the pager control?


    "Steven Cheng[MSFT]" <> wrote in message
    news:%23B99c95%...
    > Hi Rickey,
    >
    > For the GridView control, it can accept the filtering parameter only from
    > single source(control or querystring...), so would still use the
    > DropDownList as the filtering control to change the GridView's displaying
    > data when postback? If so, I think we'd better remain using the
    > Dropdownlist as the GridView's select parameter source.... And as for
    > querystring , since we just use it to initialize the Dropdownlist when the
    > page first time load, so I think we can just manually add some code to
    > parse the querystring and set the DropDownList's selected value in
    > Page_Load (when not postback....). For example:
    >
    > protected void Page_Load(object sender, EventArgs e)
    > {
    > if (!IsPostBack)
    > {
    > string cid = Request.QueryString["cid"];
    > if (cid != null && cid != string.Empty)
    > {
    > DropDownList1.SelectedValue = cid;
    > }
    > }
    > }
    >
    > Hope helps. Thanks,
    >
    > Steven Cheng
    > Microsoft Online Support
    >
    > Get Secure! www.microsoft.com/security
    > (This posting is provided "AS IS", with no warranties, and confers no
    > rights.)
    >
    > --------------------
    > | From: "Rickey Whitworth" <>
    > | Subject: Understanding query strings
    > | Date: Wed, 7 Dec 2005 10:25:19 -0600
    > | Lines: 17
    > | X-Priority: 3
    > | X-MSMail-Priority: Normal
    > | X-Newsreader: Microsoft Outlook Express 6.00.2900.2527
    > | X-RFC2646: Format=Flowed; Original
    > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2527
    > | Message-ID: <ev1nvr0#>
    > | Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
    > | NNTP-Posting-Host: 66.182.150.119
    > | Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
    > | Xref: TK2MSFTNGXA02.phx.gbl
    > microsoft.public.dotnet.framework.aspnet.webcontrols:31607
    > | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
    > |
    > | I have a page with two web controls - a drop-down list of server names
    > and a
    > | gridview that shows details about that server. The gridview is using the
    > | drop down list as a parameter, so changing the selected server in the
    > | drop-down list causes a postback and the gridview updates to the new
    > server.
    > |
    > | What I would like to do is add the ability to pass a querystring to the
    > page
    > | similar to the following:
    > |
    > | http://somewhere.com/servers.aspx?server=server1
    > |
    > | so that when the page loads, server1 is pre-selected. I also want the
    > | ability to change the server name once I get to the page if I need to.
    > |
    > | I thought about binding the gridview to the querystring, but I cannot
    > figure
    > | out how to make the querystring change on postback.
    > |
    > |
    > |
    >
    Rickey Whitworth, Dec 8, 2005
    #5
  6. Thanks for your response Rickey,

    Seems the problem you met is specific to the pager, are you using the
    default build-in paging of GridView? Also, does the code you used to set
    the dropdownlist's selected item the same as mine? Based on my local
    test, using the page_load to assign the SelectedValue worked correctly no
    matter I used paging or not...... Anyway, here is my test page's code(I
    also tested in master page scenaro......), you can have a test on your side
    to see whether it works....


    I just use the "Product" and "Categories" table in NorthWind database for
    testing......
    =========aspx=============
    <div>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server"
    ConnectionString="<%$ ConnectionStrings:LocalNorthWind %>"
    SelectCommand="SELECT DISTINCT [CategoryID], [CategoryName]
    FROM [Categories]"></asp:SqlDataSource>
    <asp:DropDownList ID="DropDownList1" runat="server"
    AutoPostBack="True" DataSourceID="SqlDataSource1"
    DataTextField="CategoryName" DataValueField="CategoryID"
    OnDataBinding="DropDownList1_DataBinding"
    OnDataBound="DropDownList1_DataBound">
    </asp:DropDownList>
    <asp:SqlDataSource ID="SqlDataSource2" runat="server"
    ConnectionString="<%$ ConnectionStrings:LocalNorthWind %>"
    SelectCommand="SELECT [ProductName], [ProductID], [CategoryID],
    [Discontinued] FROM [Products] WHERE ([CategoryID] = @CategoryID)"
    OnSelecting="SqlDataSource2_Selecting">
    <SelectParameters>
    <asp:ControlParameter ControlID="DropDownList1"
    Name="CategoryID" PropertyName="SelectedValue"
    Type="Int32" />
    </SelectParameters>
    </asp:SqlDataSource>
    <asp:GridView ID="GridView1" runat="server" AllowPaging="True"
    AutoGenerateColumns="False"
    DataKeyNames="ProductID" DataSourceID="SqlDataSource2"
    PageSize="3" OnDataBinding="GridView1_DataBinding"
    OnDataBound="GridView1_DataBound">
    <Columns>
    <asp:BoundField DataField="ProductName"
    HeaderText="ProductName" SortExpression="ProductName" />
    <asp:BoundField DataField="ProductID"
    HeaderText="ProductID" InsertVisible="False"
    ReadOnly="True" SortExpression="ProductID" />
    <asp:BoundField DataField="CategoryID"
    HeaderText="CategoryID" SortExpression="CategoryID" />
    <asp:CheckBoxField DataField="Discontinued"
    HeaderText="Discontinued" SortExpression="Discontinued" />
    </Columns>
    <PagerSettings Position="TopAndBottom" />
    </asp:GridView>

    </div>

    =====code behind==========
    public partial class ListGridView : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {
    if (!IsPostBack)
    {
    string cid = Request.QueryString["cid"];
    if (cid != null && cid != string.Empty)
    {
    Trace.Warn("<br>DropDownList1.SelectedValue = cid;");
    DropDownList1.SelectedValue = cid;

    }
    }
    }
    protected void DropDownList1_DataBinding(object sender, EventArgs e)
    {
    Trace.Warn("<br>DropDownList1_DataBinding");
    }
    protected void DropDownList1_DataBound(object sender, EventArgs e)
    {
    Trace.Warn("<br>DropDownList1_DataBound");
    }
    protected void GridView1_DataBinding(object sender, EventArgs e)
    {
    Trace.Warn("<br>GridView1_DataBinding");
    }
    protected void GridView1_DataBound(object sender, EventArgs e)
    {
    Trace.Warn("<br>GridView1_DataBound");
    }
    protected void SqlDataSource2_Selecting(object sender,
    SqlDataSourceSelectingEventArgs e)
    {
    Trace.Warn("SqlDataSource2_Selecting");
    }
    }

    ==============================

    Hope helps. Thanks,

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)






    --------------------
    | From: "Rickey Whitworth" <>
    | References: <ev1nvr0#>
    <#B99c95#>
    | Subject: Re: Understanding query strings
    | Date: Thu, 8 Dec 2005 15:10:40 -0600
    | Lines: 99
    | X-Priority: 3
    | X-MSMail-Priority: Normal
    | X-Newsreader: Microsoft Outlook Express 6.00.2900.2527
    | X-RFC2646: Format=Flowed; Original
    | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2527
    | Message-ID: <ukGh3vD$>
    | Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
    | NNTP-Posting-Host: 66.182.150.119
    | Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP15.phx.gbl
    | Xref: TK2MSFTNGXA02.phx.gbl
    microsoft.public.dotnet.framework.aspnet.webcontrols:31633
    | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
    |
    | Thank you, that is what I was trying previously, but I thought it might
    be
    | the wrong approach. It was working, then I started getting this error:
    |
    | An error has occurred because a control with id
    | 'ctl00$ContentPlaceHolder1$GridView2$ctl04$ctl00' could not be located or
    a
    | different control is assigned to the same ID after postback. If the ID is
    | not assigned, explicitly set the ID property of controls that raise
    postback
    | events to avoid this error.
    |
    | I added an @Trace directive to the page to identify which control
    | ctl04$ctl100 was, and determined it was the pager control. If I disable
    | paging on the gridview, the problem goes away.
    |
    | The problem only happens in this situation:
    | 1) I navigate to the page using a querystring (which then selects the
    | appropriate value from the dropdown list)
    | 2) I then select a different value from the drop down list
    |
    | the error says to explicitly set ids of controls that cause postback, and
    I
    | assume a pager control causes postback, but how do I set its ID?
    |
    | "Steven Cheng[MSFT]" <> wrote in message
    | news:%23B99c95%...
    | > Hi Rickey,
    | >
    | > For the GridView control, it can accept the filtering parameter only
    from
    | > single source(control or querystring...), so would still use the
    | > DropDownList as the filtering control to change the GridView's
    displaying
    | > data when postback? If so, I think we'd better remain using the
    | > Dropdownlist as the GridView's select parameter source.... And as for
    | > querystring , since we just use it to initialize the Dropdownlist when
    the
    | > page first time load, so I think we can just manually add some code to
    | > parse the querystring and set the DropDownList's selected value in
    | > Page_Load (when not postback....). For example:
    | >
    | > protected void Page_Load(object sender, EventArgs e)
    | > {
    | > if (!IsPostBack)
    | > {
    | > string cid = Request.QueryString["cid"];
    | > if (cid != null && cid != string.Empty)
    | > {
    | > DropDownList1.SelectedValue = cid;
    | > }
    | > }
    | > }
    | >
    | > Hope helps. Thanks,
    | >
    | > Steven Cheng
    | > Microsoft Online Support
    | >
    | > Get Secure! www.microsoft.com/security
    | > (This posting is provided "AS IS", with no warranties, and confers no
    | > rights.)
    | >
    | > --------------------
    | > | From: "Rickey Whitworth" <>
    | > | Subject: Understanding query strings
    | > | Date: Wed, 7 Dec 2005 10:25:19 -0600
    | > | Lines: 17
    | > | X-Priority: 3
    | > | X-MSMail-Priority: Normal
    | > | X-Newsreader: Microsoft Outlook Express 6.00.2900.2527
    | > | X-RFC2646: Format=Flowed; Original
    | > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2527
    | > | Message-ID: <ev1nvr0#>
    | > | Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
    | > | NNTP-Posting-Host: 66.182.150.119
    | > | Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
    | > | Xref: TK2MSFTNGXA02.phx.gbl
    | > microsoft.public.dotnet.framework.aspnet.webcontrols:31607
    | > | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
    | > |
    | > | I have a page with two web controls - a drop-down list of server names
    | > and a
    | > | gridview that shows details about that server. The gridview is using
    the
    | > | drop down list as a parameter, so changing the selected server in the
    | > | drop-down list causes a postback and the gridview updates to the new
    | > server.
    | > |
    | > | What I would like to do is add the ability to pass a querystring to
    the
    | > page
    | > | similar to the following:
    | > |
    | > | http://somewhere.com/servers.aspx?server=server1
    | > |
    | > | so that when the page loads, server1 is pre-selected. I also want the
    | > | ability to change the server name once I get to the page if I need to.
    | > |
    | > | I thought about binding the gridview to the querystring, but I cannot
    | > figure
    | > | out how to make the querystring change on postback.
    | > |
    | > |
    | > |
    | >
    |
    |
    |
    Steven Cheng[MSFT], Dec 9, 2005
    #6
  7. Thank you for the response and the code sample. I believe this should take
    care of my problem. I do have one question. If you go to the page you
    created and pass a querystring, the page loads, the drop down item is
    selected and the gridview is populated. But in my test, if I then change the
    querystring in the address bar and hit enter key, the new item is selected
    in the drop down box, but the gridview is empty. If I hit refresh it
    appears. Is this by design?

    Also, is there a way to make the querystring in the address bar change when
    the drop-down list selected item changes? If not, the querystring is no
    longer valid once a different item has been selected, meaning that if a user
    bookmarks the page, it will take them to the item in the querystring, not
    the new item they selected. If the querystring cannot be changed, can it be
    removed from the url after loading so that at least it want appear?


    "Steven Cheng[MSFT]" <> wrote in message
    news:$qICBHH$...
    > Thanks for your response Rickey,
    >
    > Seems the problem you met is specific to the pager, are you using the
    > default build-in paging of GridView? Also, does the code you used to set
    > the dropdownlist's selected item the same as mine? Based on my local
    > test, using the page_load to assign the SelectedValue worked correctly no
    > matter I used paging or not...... Anyway, here is my test page's code(I
    > also tested in master page scenaro......), you can have a test on your
    > side
    > to see whether it works....
    >
    >
    > I just use the "Product" and "Categories" table in NorthWind database for
    > testing......
    > =========aspx=============
    > <div>
    > <asp:SqlDataSource ID="SqlDataSource1" runat="server"
    > ConnectionString="<%$ ConnectionStrings:LocalNorthWind %>"
    > SelectCommand="SELECT DISTINCT [CategoryID], [CategoryName]
    > FROM [Categories]"></asp:SqlDataSource>
    > <asp:DropDownList ID="DropDownList1" runat="server"
    > AutoPostBack="True" DataSourceID="SqlDataSource1"
    > DataTextField="CategoryName" DataValueField="CategoryID"
    > OnDataBinding="DropDownList1_DataBinding"
    > OnDataBound="DropDownList1_DataBound">
    > </asp:DropDownList>
    > <asp:SqlDataSource ID="SqlDataSource2" runat="server"
    > ConnectionString="<%$ ConnectionStrings:LocalNorthWind %>"
    > SelectCommand="SELECT [ProductName], [ProductID], [CategoryID],
    > [Discontinued] FROM [Products] WHERE ([CategoryID] = @CategoryID)"
    > OnSelecting="SqlDataSource2_Selecting">
    > <SelectParameters>
    > <asp:ControlParameter ControlID="DropDownList1"
    > Name="CategoryID" PropertyName="SelectedValue"
    > Type="Int32" />
    > </SelectParameters>
    > </asp:SqlDataSource>
    > <asp:GridView ID="GridView1" runat="server" AllowPaging="True"
    > AutoGenerateColumns="False"
    > DataKeyNames="ProductID" DataSourceID="SqlDataSource2"
    > PageSize="3" OnDataBinding="GridView1_DataBinding"
    > OnDataBound="GridView1_DataBound">
    > <Columns>
    > <asp:BoundField DataField="ProductName"
    > HeaderText="ProductName" SortExpression="ProductName" />
    > <asp:BoundField DataField="ProductID"
    > HeaderText="ProductID" InsertVisible="False"
    > ReadOnly="True" SortExpression="ProductID" />
    > <asp:BoundField DataField="CategoryID"
    > HeaderText="CategoryID" SortExpression="CategoryID" />
    > <asp:CheckBoxField DataField="Discontinued"
    > HeaderText="Discontinued" SortExpression="Discontinued" />
    > </Columns>
    > <PagerSettings Position="TopAndBottom" />
    > </asp:GridView>
    >
    > </div>
    >
    > =====code behind==========
    > public partial class ListGridView : System.Web.UI.Page
    > {
    > protected void Page_Load(object sender, EventArgs e)
    > {
    > if (!IsPostBack)
    > {
    > string cid = Request.QueryString["cid"];
    > if (cid != null && cid != string.Empty)
    > {
    > Trace.Warn("<br>DropDownList1.SelectedValue = cid;");
    > DropDownList1.SelectedValue = cid;
    >
    > }
    > }
    > }
    > protected void DropDownList1_DataBinding(object sender, EventArgs e)
    > {
    > Trace.Warn("<br>DropDownList1_DataBinding");
    > }
    > protected void DropDownList1_DataBound(object sender, EventArgs e)
    > {
    > Trace.Warn("<br>DropDownList1_DataBound");
    > }
    > protected void GridView1_DataBinding(object sender, EventArgs e)
    > {
    > Trace.Warn("<br>GridView1_DataBinding");
    > }
    > protected void GridView1_DataBound(object sender, EventArgs e)
    > {
    > Trace.Warn("<br>GridView1_DataBound");
    > }
    > protected void SqlDataSource2_Selecting(object sender,
    > SqlDataSourceSelectingEventArgs e)
    > {
    > Trace.Warn("SqlDataSource2_Selecting");
    > }
    > }
    >
    > ==============================
    >
    > Hope helps. Thanks,
    >
    > Steven Cheng
    > Microsoft Online Support
    >
    > Get Secure! www.microsoft.com/security
    > (This posting is provided "AS IS", with no warranties, and confers no
    > rights.)
    >
    >
    >
    >
    >
    >
    > --------------------
    > | From: "Rickey Whitworth" <>
    > | References: <ev1nvr0#>
    > <#B99c95#>
    > | Subject: Re: Understanding query strings
    > | Date: Thu, 8 Dec 2005 15:10:40 -0600
    > | Lines: 99
    > | X-Priority: 3
    > | X-MSMail-Priority: Normal
    > | X-Newsreader: Microsoft Outlook Express 6.00.2900.2527
    > | X-RFC2646: Format=Flowed; Original
    > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2527
    > | Message-ID: <ukGh3vD$>
    > | Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
    > | NNTP-Posting-Host: 66.182.150.119
    > | Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP15.phx.gbl
    > | Xref: TK2MSFTNGXA02.phx.gbl
    > microsoft.public.dotnet.framework.aspnet.webcontrols:31633
    > | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
    > |
    > | Thank you, that is what I was trying previously, but I thought it might
    > be
    > | the wrong approach. It was working, then I started getting this error:
    > |
    > | An error has occurred because a control with id
    > | 'ctl00$ContentPlaceHolder1$GridView2$ctl04$ctl00' could not be located
    > or
    > a
    > | different control is assigned to the same ID after postback. If the ID
    > is
    > | not assigned, explicitly set the ID property of controls that raise
    > postback
    > | events to avoid this error.
    > |
    > | I added an @Trace directive to the page to identify which control
    > | ctl04$ctl100 was, and determined it was the pager control. If I disable
    > | paging on the gridview, the problem goes away.
    > |
    > | The problem only happens in this situation:
    > | 1) I navigate to the page using a querystring (which then selects the
    > | appropriate value from the dropdown list)
    > | 2) I then select a different value from the drop down list
    > |
    > | the error says to explicitly set ids of controls that cause postback,
    > and
    > I
    > | assume a pager control causes postback, but how do I set its ID?
    > |
    > | "Steven Cheng[MSFT]" <> wrote in message
    > | news:%23B99c95%...
    > | > Hi Rickey,
    > | >
    > | > For the GridView control, it can accept the filtering parameter only
    > from
    > | > single source(control or querystring...), so would still use the
    > | > DropDownList as the filtering control to change the GridView's
    > displaying
    > | > data when postback? If so, I think we'd better remain using the
    > | > Dropdownlist as the GridView's select parameter source.... And as
    > for
    > | > querystring , since we just use it to initialize the Dropdownlist when
    > the
    > | > page first time load, so I think we can just manually add some code to
    > | > parse the querystring and set the DropDownList's selected value in
    > | > Page_Load (when not postback....). For example:
    > | >
    > | > protected void Page_Load(object sender, EventArgs e)
    > | > {
    > | > if (!IsPostBack)
    > | > {
    > | > string cid = Request.QueryString["cid"];
    > | > if (cid != null && cid != string.Empty)
    > | > {
    > | > DropDownList1.SelectedValue = cid;
    > | > }
    > | > }
    > | > }
    > | >
    > | > Hope helps. Thanks,
    > | >
    > | > Steven Cheng
    > | > Microsoft Online Support
    > | >
    > | > Get Secure! www.microsoft.com/security
    > | > (This posting is provided "AS IS", with no warranties, and confers no
    > | > rights.)
    > | >
    > | > --------------------
    > | > | From: "Rickey Whitworth" <>
    > | > | Subject: Understanding query strings
    > | > | Date: Wed, 7 Dec 2005 10:25:19 -0600
    > | > | Lines: 17
    > | > | X-Priority: 3
    > | > | X-MSMail-Priority: Normal
    > | > | X-Newsreader: Microsoft Outlook Express 6.00.2900.2527
    > | > | X-RFC2646: Format=Flowed; Original
    > | > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2527
    > | > | Message-ID: <ev1nvr0#>
    > | > | Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
    > | > | NNTP-Posting-Host: 66.182.150.119
    > | > | Path:
    > TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
    > | > | Xref: TK2MSFTNGXA02.phx.gbl
    > | > microsoft.public.dotnet.framework.aspnet.webcontrols:31607
    > | > | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
    > | > |
    > | > | I have a page with two web controls - a drop-down list of server
    > names
    > | > and a
    > | > | gridview that shows details about that server. The gridview is using
    > the
    > | > | drop down list as a parameter, so changing the selected server in
    > the
    > | > | drop-down list causes a postback and the gridview updates to the new
    > | > server.
    > | > |
    > | > | What I would like to do is add the ability to pass a querystring to
    > the
    > | > page
    > | > | similar to the following:
    > | > |
    > | > | http://somewhere.com/servers.aspx?server=server1
    > | > |
    > | > | so that when the page loads, server1 is pre-selected. I also want
    > the
    > | > | ability to change the server name once I get to the page if I need
    > to.
    > | > |
    > | > | I thought about binding the gridview to the querystring, but I
    > cannot
    > | > figure
    > | > | out how to make the querystring change on postback.
    > | > |
    > | > |
    > | > |
    > | >
    > |
    > |
    > |
    >
    Rickey Whitworth, Dec 13, 2005
    #7
  8. Thanks for your followup Rickey,

    For your two questions:
    =============================
    I do have one question. If you go to the page you
    | created and pass a querystring, the page loads, the drop down item is
    | selected and the gridview is populated. But in my test, if I then change
    the
    | querystring in the address bar and hit enter key, the new item is
    selected
    | in the drop down box, but the gridview is empty. If I hit refresh it
    | appears. Is this by design?
    =============================
    I'm not sure about this behavior, if you're using the exact same page as I
    pasted, this should be a browser related behavior since it run correctly on
    myside (i've tested through both IE and firefox ...). And through the page
    trace we can found that the GridView will perform databinding after the
    DropDownList's databinding. The browser cache is also one possible cause,
    but I feel strange that the dropdownlist change and the GridView not....
    You can add some code to the Page_load and printout the timestamp to see
    whether the page is actually rendered from serverside.....



    =====================
    Also, is there a way to make the querystring in the address bar change when
    the drop-down list selected item changes? If not, the querystring is no
    longer valid once a different item has been selected, meaning that if a
    user
    bookmarks the page, it will take them to the item in the querystring, not
    the new item they selected. If the querystring cannot be changed, can it be
    removed from the url after loading so that at least it want appear?
    ======================

    I'm afraid this is a hard task for the page since url querystring is
    somewhat a clientside setting (the user interactively specify the url...).
    we can change the url, but should use clientside redirection like
    Response.Redirect.... which I don't think is what you want. Also, this
    is why my test page only use the Querystring when the page first time load
    ( in if(! IsPostBack) {...} )

    Thanks,

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)



    --------------------
    | From: "Rickey Whitworth" <>
    | References: <ev1nvr0#>
    <#B99c95#>
    <ukGh3vD$>
    <$qICBHH$>
    | Subject: Re: Understanding query strings
    | Date: Tue, 13 Dec 2005 07:38:45 -0600
    | Lines: 273
    | X-Priority: 3
    | X-MSMail-Priority: Normal
    | X-Newsreader: Microsoft Outlook Express 6.00.2900.2527
    | X-RFC2646: Format=Flowed; Original
    | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2527
    | Message-ID: <OXxztq#$>
    | Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
    | NNTP-Posting-Host: 66.182.150.119
    | Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP12.phx.gbl
    | Xref: TK2MSFTNGXA02.phx.gbl
    microsoft.public.dotnet.framework.aspnet.webcontrols:31738
    | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
    |
    | Thank you for the response and the code sample. I believe this should
    take
    | care of my problem. I do have one question. If you go to the page you
    | created and pass a querystring, the page loads, the drop down item is
    | selected and the gridview is populated. But in my test, if I then change
    the
    | querystring in the address bar and hit enter key, the new item is
    selected
    | in the drop down box, but the gridview is empty. If I hit refresh it
    | appears. Is this by design?
    |
    | Also, is there a way to make the querystring in the address bar change
    when
    | the drop-down list selected item changes? If not, the querystring is no
    | longer valid once a different item has been selected, meaning that if a
    user
    | bookmarks the page, it will take them to the item in the querystring, not
    | the new item they selected. If the querystring cannot be changed, can it
    be
    | removed from the url after loading so that at least it want appear?
    |
    |
    | "Steven Cheng[MSFT]" <> wrote in message
    | news:$qICBHH$...
    | > Thanks for your response Rickey,
    | >
    | > Seems the problem you met is specific to the pager, are you using the
    | > default build-in paging of GridView? Also, does the code you used to
    set
    | > the dropdownlist's selected item the same as mine? Based on my local
    | > test, using the page_load to assign the SelectedValue worked correctly
    no
    | > matter I used paging or not...... Anyway, here is my test page's code(I
    | > also tested in master page scenaro......), you can have a test on your
    | > side
    | > to see whether it works....
    | >
    | >
    | > I just use the "Product" and "Categories" table in NorthWind database
    for
    | > testing......
    | > =========aspx=============
    | > <div>
    | > <asp:SqlDataSource ID="SqlDataSource1" runat="server"
    | > ConnectionString="<%$ ConnectionStrings:LocalNorthWind %>"
    | > SelectCommand="SELECT DISTINCT [CategoryID], [CategoryName]
    | > FROM [Categories]"></asp:SqlDataSource>
    | > <asp:DropDownList ID="DropDownList1" runat="server"
    | > AutoPostBack="True" DataSourceID="SqlDataSource1"
    | > DataTextField="CategoryName" DataValueField="CategoryID"
    | > OnDataBinding="DropDownList1_DataBinding"
    | > OnDataBound="DropDownList1_DataBound">
    | > </asp:DropDownList>
    | > <asp:SqlDataSource ID="SqlDataSource2" runat="server"
    | > ConnectionString="<%$ ConnectionStrings:LocalNorthWind %>"
    | > SelectCommand="SELECT [ProductName], [ProductID],
    [CategoryID],
    | > [Discontinued] FROM [Products] WHERE ([CategoryID] = @CategoryID)"
    | > OnSelecting="SqlDataSource2_Selecting">
    | > <SelectParameters>
    | > <asp:ControlParameter ControlID="DropDownList1"
    | > Name="CategoryID" PropertyName="SelectedValue"
    | > Type="Int32" />
    | > </SelectParameters>
    | > </asp:SqlDataSource>
    | > <asp:GridView ID="GridView1" runat="server" AllowPaging="True"
    | > AutoGenerateColumns="False"
    | > DataKeyNames="ProductID" DataSourceID="SqlDataSource2"
    | > PageSize="3" OnDataBinding="GridView1_DataBinding"
    | > OnDataBound="GridView1_DataBound">
    | > <Columns>
    | > <asp:BoundField DataField="ProductName"
    | > HeaderText="ProductName" SortExpression="ProductName" />
    | > <asp:BoundField DataField="ProductID"
    | > HeaderText="ProductID" InsertVisible="False"
    | > ReadOnly="True" SortExpression="ProductID" />
    | > <asp:BoundField DataField="CategoryID"
    | > HeaderText="CategoryID" SortExpression="CategoryID" />
    | > <asp:CheckBoxField DataField="Discontinued"
    | > HeaderText="Discontinued" SortExpression="Discontinued" />
    | > </Columns>
    | > <PagerSettings Position="TopAndBottom" />
    | > </asp:GridView>
    | >
    | > </div>
    | >
    | > =====code behind==========
    | > public partial class ListGridView : System.Web.UI.Page
    | > {
    | > protected void Page_Load(object sender, EventArgs e)
    | > {
    | > if (!IsPostBack)
    | > {
    | > string cid = Request.QueryString["cid"];
    | > if (cid != null && cid != string.Empty)
    | > {
    | > Trace.Warn("<br>DropDownList1.SelectedValue = cid;");
    | > DropDownList1.SelectedValue = cid;
    | >
    | > }
    | > }
    | > }
    | > protected void DropDownList1_DataBinding(object sender, EventArgs e)
    | > {
    | > Trace.Warn("<br>DropDownList1_DataBinding");
    | > }
    | > protected void DropDownList1_DataBound(object sender, EventArgs e)
    | > {
    | > Trace.Warn("<br>DropDownList1_DataBound");
    | > }
    | > protected void GridView1_DataBinding(object sender, EventArgs e)
    | > {
    | > Trace.Warn("<br>GridView1_DataBinding");
    | > }
    | > protected void GridView1_DataBound(object sender, EventArgs e)
    | > {
    | > Trace.Warn("<br>GridView1_DataBound");
    | > }
    | > protected void SqlDataSource2_Selecting(object sender,
    | > SqlDataSourceSelectingEventArgs e)
    | > {
    | > Trace.Warn("SqlDataSource2_Selecting");
    | > }
    | > }
    | >
    | > ==============================
    | >
    | > Hope helps. Thanks,
    | >
    | > Steven Cheng
    | > Microsoft Online Support
    | >
    | > Get Secure! www.microsoft.com/security
    | > (This posting is provided "AS IS", with no warranties, and confers no
    | > rights.)
    | >
    | >
    | >
    | >
    | >
    | >
    | > --------------------
    | > | From: "Rickey Whitworth" <>
    | > | References: <ev1nvr0#>
    | > <#B99c95#>
    | > | Subject: Re: Understanding query strings
    | > | Date: Thu, 8 Dec 2005 15:10:40 -0600
    | > | Lines: 99
    | > | X-Priority: 3
    | > | X-MSMail-Priority: Normal
    | > | X-Newsreader: Microsoft Outlook Express 6.00.2900.2527
    | > | X-RFC2646: Format=Flowed; Original
    | > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2527
    | > | Message-ID: <ukGh3vD$>
    | > | Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
    | > | NNTP-Posting-Host: 66.182.150.119
    | > | Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP15.phx.gbl
    | > | Xref: TK2MSFTNGXA02.phx.gbl
    | > microsoft.public.dotnet.framework.aspnet.webcontrols:31633
    | > | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
    | > |
    | > | Thank you, that is what I was trying previously, but I thought it
    might
    | > be
    | > | the wrong approach. It was working, then I started getting this error:
    | > |
    | > | An error has occurred because a control with id
    | > | 'ctl00$ContentPlaceHolder1$GridView2$ctl04$ctl00' could not be
    located
    | > or
    | > a
    | > | different control is assigned to the same ID after postback. If the
    ID
    | > is
    | > | not assigned, explicitly set the ID property of controls that raise
    | > postback
    | > | events to avoid this error.
    | > |
    | > | I added an @Trace directive to the page to identify which control
    | > | ctl04$ctl100 was, and determined it was the pager control. If I
    disable
    | > | paging on the gridview, the problem goes away.
    | > |
    | > | The problem only happens in this situation:
    | > | 1) I navigate to the page using a querystring (which then selects the
    | > | appropriate value from the dropdown list)
    | > | 2) I then select a different value from the drop down list
    | > |
    | > | the error says to explicitly set ids of controls that cause postback,
    | > and
    | > I
    | > | assume a pager control causes postback, but how do I set its ID?
    | > |
    | > | "Steven Cheng[MSFT]" <> wrote in message
    | > | news:%23B99c95%...
    | > | > Hi Rickey,
    | > | >
    | > | > For the GridView control, it can accept the filtering parameter only
    | > from
    | > | > single source(control or querystring...), so would still use the
    | > | > DropDownList as the filtering control to change the GridView's
    | > displaying
    | > | > data when postback? If so, I think we'd better remain using the
    | > | > Dropdownlist as the GridView's select parameter source.... And as
    | > for
    | > | > querystring , since we just use it to initialize the Dropdownlist
    when
    | > the
    | > | > page first time load, so I think we can just manually add some code
    to
    | > | > parse the querystring and set the DropDownList's selected value in
    | > | > Page_Load (when not postback....). For example:
    | > | >
    | > | > protected void Page_Load(object sender, EventArgs e)
    | > | > {
    | > | > if (!IsPostBack)
    | > | > {
    | > | > string cid = Request.QueryString["cid"];
    | > | > if (cid != null && cid != string.Empty)
    | > | > {
    | > | > DropDownList1.SelectedValue = cid;
    | > | > }
    | > | > }
    | > | > }
    | > | >
    | > | > Hope helps. Thanks,
    | > | >
    | > | > Steven Cheng
    | > | > Microsoft Online Support
    | > | >
    | > | > Get Secure! www.microsoft.com/security
    | > | > (This posting is provided "AS IS", with no warranties, and confers
    no
    | > | > rights.)
    | > | >
    | > | > --------------------
    | > | > | From: "Rickey Whitworth" <>
    | > | > | Subject: Understanding query strings
    | > | > | Date: Wed, 7 Dec 2005 10:25:19 -0600
    | > | > | Lines: 17
    | > | > | X-Priority: 3
    | > | > | X-MSMail-Priority: Normal
    | > | > | X-Newsreader: Microsoft Outlook Express 6.00.2900.2527
    | > | > | X-RFC2646: Format=Flowed; Original
    | > | > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2527
    | > | > | Message-ID: <ev1nvr0#>
    | > | > | Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
    | > | > | NNTP-Posting-Host: 66.182.150.119
    | > | > | Path:
    | > TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
    | > | > | Xref: TK2MSFTNGXA02.phx.gbl
    | > | > microsoft.public.dotnet.framework.aspnet.webcontrols:31607
    | > | > | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
    | > | > |
    | > | > | I have a page with two web controls - a drop-down list of server
    | > names
    | > | > and a
    | > | > | gridview that shows details about that server. The gridview is
    using
    | > the
    | > | > | drop down list as a parameter, so changing the selected server in
    | > the
    | > | > | drop-down list causes a postback and the gridview updates to the
    new
    | > | > server.
    | > | > |
    | > | > | What I would like to do is add the ability to pass a querystring
    to
    | > the
    | > | > page
    | > | > | similar to the following:
    | > | > |
    | > | > | http://somewhere.com/servers.aspx?server=server1
    | > | > |
    | > | > | so that when the page loads, server1 is pre-selected. I also want
    | > the
    | > | > | ability to change the server name once I get to the page if I
    need
    | > to.
    | > | > |
    | > | > | I thought about binding the gridview to the querystring, but I
    | > cannot
    | > | > figure
    | > | > | out how to make the querystring change on postback.
    | > | > |
    | > | > |
    | > | > |
    | > | >
    | > |
    | > |
    | > |
    | >
    |
    |
    |
    Steven Cheng[MSFT], Dec 14, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kurt Krueckeberg
    Replies:
    2
    Views:
    695
    =?ISO-8859-1?Q?Ney_Andr=E9_de_Mello_Zunino?=
    Nov 17, 2004
  2. Rick

    Comparing strings from within strings

    Rick, Oct 21, 2003, in forum: C Programming
    Replies:
    3
    Views:
    369
    Irrwahn Grausewitz
    Oct 21, 2003
  3. Klaus Neuner
    Replies:
    7
    Views:
    475
    Klaus Neuner
    Jul 26, 2004
  4. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    739
    Malcolm
    Jun 24, 2006
  5. g

    Strings for Strings Query

    g, Nov 8, 2003, in forum: ASP General
    Replies:
    2
    Views:
    99
    Aaron Bertrand [MVP]
    Nov 8, 2003
Loading...

Share This Page