Click to See Complete Forum and Search --> : Problem cross-page posting data


Mike Pliam
June 8th, 2010, 11:26 AM
I have a web source page that contains a textbox, id = txtV1. I wish to cross-page post this data to a target page. Say that txtV1 contains the text "0.12345". Now on a button click, the C# code behind is supposed to send the data to the target page where it is to be displayed in a similar textbox, id = txtV1.

The source page code behind looks like this:

public partial class _Default : System.Web.UI.Page
{
protected String s1;

protected void buttonSend_Click(object sender, EventArgs e)
{
// get the string values from the controls
s1 = txtV1.Text;
//s1 = "Dustin Byfuglien";
Response.Redirect("Target.aspx?Field1="+s1);
}


The target page code looks like this:

public partial class Target : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
String tsv1;
tsv1 = Request.QueryString["Field1"];
txtV1.Text = tsv1;
}
}


While it appears to me that this should work, it does not -- only "0" appears in the target textbox. However, if one uncomments the s1 string assignment on the source page, the name "Dustin Byfuglien" appears as expected in the target textbox.

I cannot figure out why the string assignments s1 = txtV1.Text; and s1 = "Dustin Byfuglien"; should behave so differently.

Please help. I need to be able to send numeric data in text form from one page to another, and from what I can tell, this should be the simplest way to do it. (It would be far more efficient to simply generate a data set on one page and send it to another without have to go through the rigamarole of first setting the number in a textbox control.)

Mike Pliam
June 8th, 2010, 12:21 PM
I have discovered where the problem lies -- but I don't know how to fix it. The important info that I omitted is inherent in the phrase Say that txtV1 contains the text "0.12345".

I failed to mention that float f1 is a protected class variable that is supposed to be updated in another button click on the same page-

public partial class _Default : System.Web.UI.Page
{
// define strings and floats as class members
protected String s1;
protected float f1;

protected void btnRandom_Click(object sender, EventArgs e)
{
Random random = new Random();
f1 = (float)random.NextDouble(); f1 *= 100.0F;
txtV1.Text = Convert.ToString(f1);
}
protected void buttonSend_Click(object sender, EventArgs e)
{
// get the string values from the controls (or from f1 directly, s1 = Convert.ToString(f1) )
s1 = txtV1.Text;
//s1 = "Dustin Byfuglien";
Response.Redirect("Target.aspx?Field1="+s1);
}
}


Notice that this only works if the string data is initiated within the buttonSend_Click function. THAT IS THE PROBLEM! I cannot figure out why the class variable is not updated properly in the btnRandom_Click function.

Alsvha
June 8th, 2010, 01:05 PM
So - let me see if I get this correct.

First - you intend to get a click on your btnRandom which fills a value into the txtV1 textbox.
Second - you intend a click on buttonSend should take the text from txtV1 textbox and redirect to Target.aspx via querystring where you then want to take the value from the querystring.

Is this correct?

If so, then indeed it should work so the issue seems to either be something you might do in the Page Load of your page containing btnRandom or prior to the onclick event.

Try debugging the page, and check if clicking on btnRandom actually enters the method as you expect.
If it does, then make sure you do not reset txtV1 on Page_Load or similar, meaning you might remove the value on the postback click of buttonSend.

I just made the following Default.aspx page containing the markup code:

<asp:TextBox ID="txt1" runat="server" />
<asp:Button ID="bt1" runat="server" Text="1" onclick="bt1_Click" />
<asp:Button ID="bt2" runat="server" Text="2" OnClick="bt2_Click" />

And the code behind:


public partial class _Default : System.Web.UI.Page
{
protected float f1;
protected string s1;

protected void Page_Load(object sender, EventArgs e)
{
}

protected void bt1_Click(object sender, EventArgs e)
{
Random random = new Random();
f1 = (float)random.NextDouble(); f1 *= 100.0F;
txt1.Text = Convert.ToString(f1);

}

protected void bt2_Click(object sender, EventArgs e)
{
s1 = txt1.Text;
Response.Redirect("Target.aspx?Field1=" + s1);

}
}


And when clicking the first button ("bt1") it'll put text into my textbox ("txt1") and clicking button 2 ("bt2") will transmit it to the receiving page.

I can't see anything inherently wrong with your code, so the issue might be somewhere else around the code not written here?