24

Cross page posting Submit Form Server.Transfer methods ASP.NET

Cross Page Posting Submit Form Server.Transfer Methods In ASP.NET.To transer data using cross page posting we need to use FindControl Method we can write code like this

First of all we need to set the PostBackUrl property of the Button we are gonna use for postback, to the page where we need to redirect, in our case it's Default2.aspx

to retrieve the value of a textbox from previous page in Default2.aspx page
we need to use FindControl method to find the textbox control of previous page

TextBox txtName = (TextBox)PreviousPage.FindControl("txtUserName"));

lblName.text = txtName.Text.ToString();


Submit Form Method to post data and retrieve on another page in ASP.NET

To start with simplest method we can write code like this

<form id="form1" method="post" 
action="Default2.aspx">
Data <input type="text" 
name="Data" size="20"><br>

<input id="Submit1" type="submit" 
value="submit" />
</form>

And to retrieve it on the posted page

lblValue.Text = Request.Form["Data"];


asp.net does not support post method with runat="Server" attribute,
for this we will have to use javascript

<form id="Form1" method="post"
runat="server">
<asp:TextBox ID="txtData" runat="server">
</asp:TextBox>
</form>

<form name="FormSubmit" action="Default2.aspx"
method="post">
<input id="Submit1" type="submit"
value="submit"
onclick="CopyTextToHiddenField()" />
<input name="Hidden1" type="hidden" />
</form>

Ass this JavaScript in the Head section of page to be posted

<script language="javascript">
function CopyTextToHiddenField()
{
var textbox1Value = document.getElementById
("<%=TextBox1.ClientID%>").value;

document.forms[1].document.getElementById
("Hidden1").value = textbox1Value;
}
</script>

To retrieve the value of hidden field
in Default2.aspx page

lblData.Text = Request.Form["Hidden1"];



Server.Transfer Method in ASP.NET

Server.Transfer method sends (transfers) all the state information (all application/session variables and all items in the request collections) created in one ASPX page to a second ASPX page.

you can't use Server.Transfer to send the user to an external site
The Server.Transfer method also has a second parameter—"preserveForm". If you set this to True, using a statement such as Server.Transfer("WebForm2.aspx", True), the existing query string and any form variables will still be available to the page you are transferring to.

For example, if your Default.aspx has a TextBox control called TextBox1 and you transferred to Default2.aspx with the preserveForm parameter set to True, you'd be able to retrieve the value of the original page TextBox control by referencing Request.Form("TextBox1").

protected void Button1_Click1
(object sender, EventArgs e)
{
Server.Transfer("Default2.aspx", true);
}

Now in Default2.aspx page_load event
use the below code to get the value of TextBox1

Response.Write(Request.Form["TextBox1"]);



Related Posts:
1. Data Transfer to other aspx pages using Query String Session cookies and cross page posting

2. Data Transfer Using Cookies Session Variables Cross page posting and QueryStrings in ASP.NET

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


2

Data Transfer Using Cookies Session Variables in ASP.NET C# VB.NET

Data Transfer Using Cookies Session Variables in ASP.NET 2.0,3.5,4.0 C# VB.NET Cookies are stored in client machine and can have max size of 4kb

To store value from textbox


protected void btnGo_Click(object sender, EventArgs e)
{
HttpCookie MyCookie = new HttpCookie("Data");
MyCookie.Value = txtData.Text;
MyCookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(MyCookie);
Response.Redirect("Deault2.aspx");
}
And to retrieve data from cookie in Deault2.aspx page
In the Page_Load event write
String strData = Request.Cookies["Data"].Value);

Data Transfer using Session Variables

Storing and retrieving Data to and from
session variables on the page we want
To store a value in session we need to write
protected void btnGo_Click(object sender, EventArgs e)
{
Session["VariableName"] = txtData.Text;
Response.Redirect("Deault2.aspx");
}
To retrieve value from Session on other page
string strData = Session["VariableName"].ToString();
Don't forget to use proper typecasting as session stores data as Object and we need
to change it to desired type
To remove Session Variables use
Session.Contents.Remove("VariableName"); or
Session.Contents.RemoveAll(); or
Session.Abandon();


Continue



Related Posts:
1. Data Transfer to other aspx pages using Query String Session cookies and cross page posting

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

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

13

Populate DropdownList In SelectedIndexChanged DetailsView GridView

In this example i am explaining how to Bind Populate Second DropDownList In SelectedIndexChanged Event Based On Selection Of Other First Dropdown In Detailsview Gridview ASP.NET and save the record in database, in this example i've added two DropDowns in DetailsView control using TemplateField and InsertItemTemplate

The Second DropDown (ddlProducts) is getting populated based on Category selected in Category DropDown , For this i've used SelectedIndexChanged event and findControl method to find the control and sqldataSource is used as datasource for the dropdowns and DetailView


HTML source of the aspx page is
<asp:DetailsView ID="dView" runat="server" AutoGenerateRows="False" 
                 DataSourceID="SqlDataSourceDetails"
                 DefaultMode="Insert" Height="50px" Width="125px">
<Fields>
<asp:TemplateField HeaderText="Category">
<InsertItemTemplate>
<asp:DropDownList ID="ddlCategory" runat="server" AutoPostBack="true"
                  DataSourceID="SqlDataSourceCategory" 
                  DataTextField="Category" DataValueField="Category" 
        OnSelectedIndexChanged="ddlCategory_SelectedIndexChanged" >
</asp:DropDownList>  
<asp:SqlDataSource ID="SqlDataSourceCategory" runat="server" 
ConnectionString="<%$ ConnectionStrings:dvConnectionString %>"
SelectCommand="SELECT DISTINCT [Category] FROM [Products]">
</asp:SqlDataSource>
</InsertItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Product">
<InsertItemTemplate>
<asp:DropDownList ID="ddlProducts" runat="server" 
                  DataSourceID="SqlDataSourceProd"
                  DataTextField="Product" DataValueField="Product">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSourceProd" runat="server" 
ConnectionString="<%$ ConnectionStrings:dvConnectionString %>"
SelectCommand="SELECT [Product] FROM [Products] 
WHERE ([Category] = @Category)" 
OnSelecting="SqlDataSourceProd_Selecting">
<SelectParameters>
<asp:Parameter Name="Category" Type="String" />
</SelectParameters>
</asp:SqlDataSource> 
</InsertItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Description">
<InsertItemTemplate>
<asp:TextBox runat="server" ID="txtDes" Text='<%#Bind("Des") %>'>
</asp:TextBox>
</InsertItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowInsertButton="True" />
</Fields>
</asp:DetailsView>
        
<asp:SqlDataSource ID="SqlDataSourceDetails" runat="server" 
ConnectionString="<%$ ConnectionStrings:dvConnectionString %>"
SelectCommand="SELECT * FROM [Products]" 
InsertCommand="INSERT INTO Products(Category, Product, Des) 
               VALUES (@Category,@Product,@Des)" 
OnInserting="SqlDataSourceDetails_Inserting" >
<InsertParameters>
<asp:Parameter Name="Category" />
<asp:Parameter Name="Product" />
<asp:Parameter Name="Des" />
</InsertParameters>
</asp:SqlDataSource>

C# code behind
protected void ddlCategory_SelectedIndexChanged
                   (object sender, EventArgs e)
{
 DropDownList ddlProducts = 
     (DropDownList)dView.FindControl("ddlProducts");

        if (ddlProducts != null)
        {
            ddlProducts.DataBind();
        }
}

protected void SqlDataSourceProd_Selecting
    (object sender, SqlDataSourceSelectingEventArgs e)
{
  DropDownList ddlCategory = 
      (DropDownList)dView.FindControl("ddlCategory");

        if (ddlCategory != null)
        {
            e.Command.Parameters["@Category"].Value = ddlCategory.SelectedValue;
        }
}

protected void SqlDataSourceDetails_Inserting
       (object sender, SqlDataSourceCommandEventArgs e)
{
 DropDownList ddlCat = 
         (DropDownList)dView.FindControl("ddlCategory");
 e.Command.Parameters["@Category"].Value = ddlCat.SelectedValue;
 DropDownList ddlProd = 
     (DropDownList)dView.FindControl("ddlProducts");
e.Command.Parameters["@Product"].Value = ddlProd.SelectedValue;
}

VB.NET Code behind
Protected Sub ddlCategory_SelectedIndexChanged
(ByVal sender As Object, ByVal e As EventArgs)

Dim ddlProducts As DropDownList = 
DirectCast(dView.FindControl("ddlProducts"), DropDownList)
    
    If ddlProducts IsNot Nothing Then
        ddlProducts.DataBind()
    End If
End Sub

Protected Sub SqlDataSourceProd_Selecting
(ByVal sender As Object, 
ByVal e As SqlDataSourceSelectingEventArgs)

Dim ddlCategory As DropDownList = 
DirectCast(dView.FindControl("ddlCategory"), DropDownList)
    
    If ddlCategory IsNot Nothing Then
    e.Command.Parameters("@Category").Value = 
                     ddlCategory.SelectedValue
    End If
End Sub

Download the code from here







Find More Articles