Passing strings between webforms

  • Thread starter Thread starter Guest
  • Start date Start date
How are the forms related?

For example, if you create a child form, you can create public properties
in the child form that the parent can access. For example:

public MyChildForm : System.Windows.Form
{
...
private string privateData = string.Empty;

...

public string MyData
{
get
{
return privateData;
}
set
{
privateData = value;
}
}

...
}

MyChildForm childForm = new MyChildForm();
childForm.MyData = "This is my data"
if (DialogResult.OK == childForm.ShowDialog())
{
string myNewData = childForm.
...
}


If the forms are completely unrelated, then you need to provide them both
with a shared object that they can pass their data to and retrieve the data
of the other.

Pete
 
session or query string, or the code below will do it using the Reference
Page attribute

--
Regards

John Timney
ASP.NET MVP
Microsoft Regional Director

Newbie said:
How do I pass string values from one webform to another?


<%@ Page Language="VB" ClassName="FirstPageClass" %>

<html>
<head>

<script runat="server">
Public ReadOnly Property FirstName() As String
Get
' first is the name of a TextBox control.
Return first.Text
End Get
End Property

Public ReadOnly Property LastName() As String
Get
' last is the name of a TextBox control.
Return last.Text
End Get
End Property

Sub ButtonClicked(sender As Object, e As EventArgs)
Server.Transfer("ReceivingValuesPage.aspx")
End Sub

</script>

</head>

<body>

<form runat="server">
First Name:
<asp:TextBox id="first"
runat="server"/>
<br>
Last Name:
<asp:TextBox id="last"
runat="server"/>
<br>
<asp:Button
OnClick="ButtonClicked"
Text="Go to second page"
runat=server />
</form>

</body>

</html>




<%@ Page Language="VB" %>
<%@ Reference Page="passingValuesPage.aspx" %>

<html>

<head>

<script runat="server">

Dim fp As FirstPageClass

Sub Page_Load()
If Not IsPostBack Then
fp = CType(Context.Handler, FirstPageClass)
End If
End Sub



</script>

</head>

<body>

<form runat="server">

Hello <%=fp.FirstName%> <%=fp.LastName%>

</form>

</body>

</html>
 
God, sorry, I was doing winform stuff. My apologies. I'm all backwards
today. I should stop answering questions. In one the guy specified WinForms
and I gave him a webforms solution, and this guy specified web forms and I
gave him a winform solution. ARGH.

Pete
 
Hello Newbie,

It depends on how long you need to be able to access that string value. If you just need it to be passed from the first form to the second, then I would use Context.Items.
eg. Context.Items["KEY_HERE"] = yourstring;

then to retrieve it on the next page you simply do:

string yourstring = Context.Items["KEY_HERE"];

Hope this helps and that I've answered correctly!

There is a really good section in MSDN about "STATE" and webforms- have a search for it.



W
 
yeah, I spotted that one. Easy mistake to make but at least you were trying
to help :0)

--
Regards

John Timney
ASP.NET MVP
Microsoft Regional Director
 
Back
Top