Search

Tuesday, April 1, 2014
0 comments

How to pass ampersand in query string parameter in ASP.NET

7:35 PMTuesday, April 1, 2014
According to our previous article how to use query string in asp.net, we use query string parameter for searching value from the database.Now, If you want to pass parameter value with special character like "&" . Let's take an simple example,  how to pass & in QueryString parameter.

I-method

Response.Redirect("~/Default3.aspx?userName=" + Server.UrlEncode("you&me") + "&Password=jacob");

Here, In above mentioned code your username contains & symbol between you&me and we use & for separates the field.

II-method

~/Default3.aspx?userName=you%26me&password=jacob

Here, we use %26, in the place of & symbol. Any browser consider & symbol as %26.

Complete Code

   Default2.aspx code

<asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click">LinkButton</asp:LinkButton>

Default2.aspx.cs code

protected void LinkButton1_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Default3.aspx?userName=" + Server.UrlEncode("you&me") + "&Password=jacob");
    }

Now, your button click event call another webform, where you bind  gridview through QueryString
Default3.aspx code

<form id="form1" runat="server">
    <div>
    
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            DataKeyNames="sno" DataSourceID="SqlDataSource1">
            <Columns>
                <asp:BoundField DataField="sno" HeaderText="sno" InsertVisible="False" 
                    ReadOnly="True" SortExpression="sno" />
                <asp:BoundField DataField="userName" HeaderText="userName" 
                    SortExpression="userName" />
                <asp:BoundField DataField="Password" HeaderText="Password" 
                    SortExpression="Password" />
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
            SelectCommand="SELECT * FROM [user] WHERE (([userName] = @userName) AND ([Password] = @Password))">
            <SelectParameters>
                <asp:QueryStringParameter Name="userName" QueryStringField="userName" 
                    Type="String" />
                <asp:QueryStringParameter Name="Password" QueryStringField="Password" 
                    Type="String" />
            </SelectParameters>
        </asp:SqlDataSource>
    
    </div>

    </form>

 Code Generate the following output

How to pass ampersand in query string parameter in ASP.NET

How to pass ampersand in query string parameter in ASP.NET
Protected by Copyscape Online Copyright Protection Software

0 comments :

Post a Comment

 
Toggle Footer
Top