Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I server.transfer the changed value of a control

Status
Not open for further replies.

j9

Programmer
Jun 6, 2001
90
0
0
US
I have a textbox, txtName, runat=server. When the submit button is clicked, I want to change the value of txtName in the code-behind page and then submit the changed form variable to another page. It seems that the value of txtName must also be changed client-side in order for the value to persist to the next page. Here's a brief of what I have. The changed value of txtName does not carry over to Page2.aspx:

private void btnSubmit_Click(object sender, System.EventArgs e)
{
txtName.Value = "somethingelse";
Server.Transfer("Page2.aspx");
}


 
So the user enters "A" in a textbox, submits the form.

Server-side code changes the value of the server control to "B".

Then you do a server.transfer, passing the values, and the second page sees the value of the textbox as "A".

That's because you're not transferring server controls/properties, you're transferring the HTTP form data.

Can you change your sequence of events?

I think you'll have to change the HTTP Form data to correctly transfer:

Request.Form.Set()



Thomas D. Greer

Providing PostScript & PDF
Training, Development & Consulting
 
You simply have to set another parameter (preserverForm) to True to perserve the form and querystring items. e.g.
Code:
Server.Transfer("AnotherPage.aspx",True)

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
Thank you, but how do I do that? What is Request.Form.Set()? I couldn't find any documentation.
 
Unless I'm missing something, Server.Transfer("AnotherPage.aspx",True) does not pick up the values that were changed server-side. I think tgreer is right in that I need to change the http form data. I'm just not sure how to go about it. Thank you.
 
Sorry, looks like the Form collection is read-only.

So you just have to do it another way. For example, you can use HttpContext, or you can build a querystring from your form data, and do a redirect instead of a transfer.

This is an unusual situation, would you mind explaining why you need to do this?



Thomas D. Greer

Providing PostScript & PDF
Training, Development & Consulting
 
Yep, I ran into the read-only exception, too. I was trying to give a simple code example. What really happens is this:

1. The user fills in a bunch of text fields to create a new record.
2. The user clicks the 'save & continue' button that calls btnSubmit_Click().
3. btnSubmit_Click inserts the new record into the database, gets the id of the new record, and SHOULD then submit the value of the id in a hidden form field to the next page.

I suppose I could use a session variable, but this is really bugging me now :)
 
Thank you very much for your help, but I was trying to avoid sending the id in the URL. I guess I can just use a session.
 
Or hit the database again. If you have enough information in the form to write the database record, you have enough information to READ that record.

So you have to choose between the following evils:

1) Session variable
2) Querystring
3) second database hit



Thomas D. Greer

Providing PostScript & PDF
Training, Development & Consulting
 
Good point on the db hit. I think I'll go with a session. Thanks for sticking with me!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top