barsan
barsan

Reputation: 2491

How to read Query String into the ASPX page?

I have a Listview which is populating automatically with sqldatasource and I here I have a Link Button inside the Listview by clicking which I am being redirected to some other page. But while redirecting I need to pass ID and username as querystring to the new page. And this ID and username I am receiving through querystring from the previous page to this page. So, I need to know how do I read this on the frondend or on the aspx page and bind it with the postback url of the Linkbutton? Please suggest me on this. Thanks.

Here is the code I am trying:

  <asp:LinkButton ID="LinkButton2" runat="server" PostBackUrl="WebForm4.aspx?id=<%=id %>">here</asp:LinkButton>

But into the next page I am getting like this:

http://localhost:38524/WebForm4.aspx?id=%3C%=id%20%%3E

Whether I want to get http://localhost:38524/WebForm4.aspx?id=1 as I am setting the value of id as 1 by receiving querystring.

Now I am trying with this as well:

<asp:LinkButton ID="LinkButton2" runat="server" Text="here" PostBackUrl='<%# string.Format
 ("WebForm4.aspx?postquestion={0}", Request.QueryString["id"]) %>'></asp:LinkButton>

But this is not working. When I click on the link button it is not redirecting to the WebForm4.aspx instead it is refreshing into the same page. Please help me on this.

Upvotes: 0

Views: 3813

Answers (3)

Bozhidar Stoyneff
Bozhidar Stoyneff

Reputation: 3634

You almost did it with your second attempt. You reading the query string fine. The little things missing are the tilde followed by the forward slash:

<asp:LinkButton ID="LinkButton2" runat="server" Text="here" 
    PostBackUrl='<%# string.Format("~/WebForm4.aspx?postquestion={0}", Request.QueryString["id"]) %>'></asp:LinkButton>

Also, replace the hash sign with an equal sign; <%# ... %> is Data binding syntax. You need this one to return a string from server side code.

Upvotes: 0

Denys Wessels
Denys Wessels

Reputation: 17049

You can use the example below to pass through both ID and UserName to the next page from the query string:

<asp:LinkButton ID="LinkButton2" runat="server" PostBackUrl='<%# String.Format("WebForm4.aspx?id={0}&userName={1}", Request.QueryString["id"],Request.QueryString["userName"]) %>'>here</asp:LinkButton>

Upvotes: 1

KanisXXX
KanisXXX

Reputation: 777

On the Page where you are passing Querystrings , Declare Public string variables.And on PageLoad read querystrings and pass it to those variables.

public string QueryString1;
public string QueryString2;
protected void Page_Load(object sender, EventArgs e)
{
  Page.DataBind();// Imp to add for setting values from front side
    QueryString1 = Request.QueryString["QueryString1"].ToString();
    QueryString2 = Request.QueryString["QueryString2"].ToString();
    LinkButton2.PostBackUrl = "WebForm4.aspx?id=" + QueryString1;
}

and then on aspx page you can access these value by By <%=QueryString1 %>,<%=QueryString2 %>

<div class="row" runat="server" visible="False" id="mainDiv">

    <div class="col-md-12">
        <div class="panel panel-default">
            <div class="panel-body">
                <div id="divMsg" runat="server">
                       <%=QueryString1 %>
                    <%=QueryString2 %>
                    <asp:Label runat="server" ID="lblMsg"></asp:Label>
                </div>
            </div>
        </div>
    </div>
</div>

Update : See the Page_Load for setting postback from codebehind

Update2 change the postback to

 PostBackUrl='<%#String.Format("WebForm4.aspx?id={0}",QueryString1) %>'

I hope it will help (Tested)

Upvotes: 0

Related Questions