Showing posts with label QueryString. Show all posts
Showing posts with label QueryString. Show all posts
3

Asp.Net QueryString Example

QueryString Example In Asp.Net 2.0,3.5,4.0 With Multiple Variable Values Using C# And VB.NET. Several time in ASP.NET applications we need to transfer data or information provided by user from one aspx page to another.

We can achieve this using several methods like Cookies,Session or Crosspage posting

In this post i am explaining how to use querystrings.

Example url with querystring can be something similar like this

http://yourdomainname.com/defauld.aspx?variable1=value1&variable2=value2

Suppose we have a textbox txtData and we want it's value on other page
than in code behind we would write in click event of btnGo

private void btnGO_Click(object sender, System.EventArgs e)
{
Response.Redirect("Default2.aspx?Value=" +
txtData.Text);
}

Or

private void btnGO_Click(object sender, System.EventArgs e)
{
Response.Redirect("Default2.aspx?city=" +
txtData.Text + "&country=" + txtcountry.Text);
}


Now to retrieve these values on other page we need to use request.querystring, we can either retrieve them by variable name or by index

private void Page_Load(object sender,System.EventArgs e)
{
txtCity.Text = Request.QueryString["city"];
txtCountry.Text = Request.QueryString["country"];
}

Or we can also use

private void Page_Load(object sender,System.EventArgs e)
{
txtCity.Text = Request.QueryString[0];
txtCountry.Text = Request.QueryString[1];
}



QueryString can't be used for sending long data because it has a max lenght limit

Data being transferred is visible in url of browser

To use spaces and & in query string we need to replace space by %20 and & by %26


private void btnGO_Click(object sender, System.EventArgs e)
{
Response.Redirect("Default2.aspx?Value=" +
txtData.Text.Replace(" ","%20");
}

Or we can use Server.UrlEncode method

private void btno_Click(object sender, System.EventArgs e)
{
Response.Redirect("Default2.Aspx?" +
"Name=" + Server.UrlEncode(txtData.Text));
}


11

GridView Examples In ASP.NET 2.0 3.5 4.0 4.5

24

Pass Send GridView Row Value/Data Using Hyperlink In ASP.NET

In this example i am describing how to Pass Transfer Or Send GridView Row Data Values To Other asp.net page using hyperlink.

Pass Transfer Send GridView Row Data Values With Hyperlink
I've put a hyperlink column in gridview to pass values through querystring, and using request.querystring on the second page to retrieve values.

You would also like to read
LinkButton in GridView and QueryString in ASP.NET to pass data


We need to set DataNavigateUrlFields and DataNavigateUrlFormatString properties of hyperlink in gridview to pass the row data 
HTML markup of the page


<asp:GridView ID="GridView1" runat="server" 
              AutoGenerateColumns="False" 
              DataSourceID="SqlDataSource1">
<Columns>
<asp:HyperLinkField DataNavigateUrlFields="ID,Name,Location" 
DataNavigateUrlFormatString=
"Default2.aspx?id={0}&name={1}&loc={2}" 
Text="Transfer values to other page" />
<asp:BoundField DataField="ID" HeaderText="ID" 
                SortExpression="ID" />
<asp:BoundField DataField="Name" HeaderText="Name" 
                SortExpression="Name" />
<asp:BoundField DataField="Location" HeaderText="Location" 
                SortExpression="Location" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [ID], [Name], [Location] FROM [Details]">
</asp:SqlDataSource>

Now write code mentioned below to retrieve values on Default2.aspx page
C# code behind
protected void Page_Load(object sender, EventArgs e)
    {
        string strID = Request.QueryString["id"];
        string strName = Request.QueryString["name"];
        string strLocation = Request.QueryString["loc"];
        lblID.Text = strID;
        lblName.Text = strName;
        lblLocation.Text = strLocation;
    }
VB.NET code behind
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    Dim strID As String = Request.QueryString("id")
    Dim strName As String = Request.QueryString("name")
    Dim strLocation As String = Request.QueryString("loc")
    lblID.Text = strID
    lblName.Text = strName
    lblLocation.Text = strLocation
End Sub

Hope this helps


41

LinkButton in GridView and QueryString in ASP.NET

This example explains how to use LinkButton in GridView and QueryString in ASP.NET to pass or transfer variable data to other page using QueryString and QueryStringParameters.

I've put a searchbox at the top of page and based on search text entered in textbox, gridview is populated, In the gridview i've put a Link Button in ItemTemplate column.

LinkButton in GridView and QueryString in ASP.NET

LinkButton field is hyperlink which points to Details.aspx and sends RecordID as QueryString to Details.aspx page where another gridview is populated to show Details of selected record by retrieving the QueryString variable using QueryStringParameters.

HTML SOURCE OF DEFAULT.ASPX


<asp:TextBox ID="txtSearch" runat="server"/>
<asp:Button ID="btnSearch" runat="server"
            Text="Search" OnClick="btnSearch_Click"/>
 
<asp:GridView ID="GridView1" runat="server"
              AutoGenerateColumns="False"
              DataSourceID="SqlDataSource1">
<Columns>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:LinkButton ID="lnkname" runat="server"
                Text='<%#Eval("Name") %>'
                PostBackUrl='<%#"~/Details.aspx?ID="+Eval("ID")%>'>
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
 
<asp:BoundField DataField="City" HeaderText="City"/>
</Columns>
</asp:GridView>
 
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [ID], [Name], [City], [Country]
               FROM [Location]
               WHERE ([Name] LIKE '%' + @Name + '%')">
<SelectParameters>
<asp:ControlParameter ControlID="txtSearch" Name="Name" 
                      PropertyName="Text" Type="String"/>
</SelectParameters>
</asp:SqlDataSource>

HTML SOURCE OF DETAILS.ASPX
   1:  <asp:GridView ID="GridView1" runat="server"
   2:                AutoGenerateColumns="False"
   3:                DataSourceID="SqlDataSource1">
   4:  <Columns>
   5:  <asp:BoundField DataField="ID" HeaderText="ID"/>
   6:  <asp:BoundField DataField="Name" HeaderText="Name"/>
   7:  <asp:BoundField DataField="City" HeaderText="City"/>
   8:  <asp:BoundField DataField="Country" HeaderText="Country"/>
   9:  </Columns>
  10:  </asp:GridView>
  11:   
  12:  <asp:SqlDataSource ID="SqlDataSource1" runat="server"
  13:  ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
  14:  SelectCommand="SELECT [ID], [Name], [City], [Country]
  15:                 FROM [Location] WHERE ([ID] = @ID)">
  16:   
  17:  <SelectParameters>
  18:  <asp:QueryStringParameter Name="ID" QueryStringField="ID" 
  19:                            Type="Decimal"/>
  20:  </SelectParameters>
  21:  </asp:SqlDataSource>



Related Posts:
Creating winforms AutoComplete TextBox using C# in Windows application
Disable copy paste cut and right click in textbox on aspx page using javascript
Ajax autocomplete extender textbox in EditItemTemaplate of GridView
ASP.NET-Exporting paging enabled GridView to pdf using iTextSharp and C#

4

Data Transfer Using QueryString To Other Asp.Net Pages

This posts explains how to Data Transfer Using QueryString To Other Asp.Net Pages. Several time while developing asp .NET applications we need to transfer data from one page to another, we can achieve this by several methods , some of them are

1. Using QueryStrings

2. Using Cookies

3. Using Session Variables

4. Using Cross Page Posting

5 . Using Server.Transfer


1 . QueryStrings
Suppose we have a textbox txtData and we want it's value on other page
than in code behind we would write in click event of btnGo

private void btnGO_Click(object sender, System.EventArgs e)
{
Response.Redirect("Default2.aspx?Value=" +
txtData.Text);
}

To retrieve this value on second page
private void Page_Load(object sender, System.EventArgs e)
{
txtData.Text = Request.QueryString["Value"];
}

If we have passed more than one values in querystring we can either retrieve them
by variable name or by index
private void Page_Load(object sender,System.EventArgs e)
{
txtData.Text = Request.QueryString[0];
}

QueryString can't be used for sending long data because it has a max lenght limit

Data being transferred is visible in url of browser

To use spaces and & in query string we need to replace space by %20 and & by %26
private void btnGO_Click(object sender, System.EventArgs e)
{
Response.Redirect("Default2.aspx?Value=" +
txtData.Text.Replace(" ","%20");
}

Or we can use Server.UrlEncode method

private void btno_Click(object sender, System.EventArgs e)
{
Response.Redirect("Default2.Aspx?" +
"Name=" + Server.UrlEncode(txtData.Text));
}


Continue



Related Posts:
1. Data Transfer Using Cookies Session Variables Cross page posting and QueryStrings in ASP.NET

2. Cross page posting, Submit Form and Server.Transfer methods in ASP .NET for data transfer

3. LinkButton in GridView and QueryString Parameters to pass/transfer variable and data-ASP.NET Articles

Find More Articles