How to use CrossPagePostBack in Asp.net 2.0
Explain me the use of CrossPagePostBack in Asap.net 2.0 with example (C# code)
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Rajendra DeopuraPosted Oct 24, 2008, 6:14 AM
In Default mode all the Webforms post to themselves. However, sometimes you need to post your form to some other Webforms. This is called as CrossPagePostBack.
Example
Open a New Website with a Default form 'Default.aspx'.
Add a Label control, a DropDownList control and a Button Control to the Default.aspx form. Set, the text property of Label to "Select an Id"
Bind the dropdownList box to Authors id. I am using BIBLIO.MDB database file. Set the Text property to 'Search' .
Add another webform to the web site named Authors.aspx.
Set the PostBackUrl property of Button control of Default.aspx to Authors.aspx page.
Go to the view code of Default.aspx page and write the follwing code :
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
BindDropDownList()
End If
End Sub
Sub BindDropDownList()
Dim constr As String = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\Documents and Settings\USER\Desktop\BIBLIO.mdb"
Dim cmdstr As String = "select AU_ID from Authors"
Dim da As New OleDbDataAdapter(cmdstr, constr)
Dim ds As New DataSet
da.Fill(ds, "auth")
Dim dt As DataTable = ds.Tables("auth")
DropDownList1.DataSource = dt
DropDownList1.DataTextField = "AU_ID"
DropDownList1.DataValueField = "AU_ID"
DropDownList1.DataBind()
End Sub
On Authors.aspx Page, Drop down a GridView control. Go to the view code and write the following code :
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
Dim ddl As DropDownList = CType(PreviousPage.FindControl("DropDownList1"), DropDownList)
Dim id As Integer = Integer.Parse(ddl.SelectedValue)
BindGrid(id)
End If
End Sub
Sub BindGrid(ByVal id As Integer)
Dim constr As String = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\Documents and Settings\USER\Desktop\BIBLIO.mdb"
Dim cmdstr As String = "select AU_ID,Author from Authors WHERe AU_ID= @Id"
Dim con As New OleDbConnection(constr)
Dim com As New OleDbCommand(cmdstr, con)
con.Open()
com.Parameters.AddWithValue("@id", id)
Dim r As OleDbDataReader = com.ExecuteReader
GridView1.DataSource = r
GridView1.DataBind()
r.Close()
con.Close()
End Sub
I have created this code in VB.Net. If you need the project, mail me at [email protected].
Happy Coding!