2

I have an ASP.NET site and I have a page which uses a url query string:

routeToScene.aspx?RunNumber=3253665

This loads fine when I pass the parameter RunNumber=XXXXXXX in the URL.

What I want to do is load this page by passing the RunNumber=XXXXXXX via a text box on another page called: routToHospitalParam.aspx with a simple submit button that takes the value in the textbox and passes it to the URL of the second page. This should be simple and I feel foolish for not finding an answer.

This is the code in the routeToHospitalParam.aspx page...

<asp:TextBox ID="TextBox1" Width="80px" MaxLength="7" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Submit" PostBackUrl="~/MAP/routeToHospital.aspx" + TextBox1.text />

The issue is when I try use the TextBox1.value (assuming that is correct) in the PostBackUrl which loads the other page.

Liam
  • 27,717
  • 28
  • 128
  • 190
JimP
  • 37
  • 2
  • 6

3 Answers3

2

You can do this

In aspx change code to handle onclick event:

<asp:Button ID="Button1" runat="server" Text="Submit" 
OnClick="Button1_OnClick" />

In codebehind button click event redirect user with queryprameter:

protected void Button1_OnClick(object sender, EvenArgs e)
{
  Response.redirect("~/MAP/routeToHospital.aspx?RunNumber=" + HttpUtility.UrlEncode(TextBox1.text));
}
rs.
  • 26,707
  • 12
  • 68
  • 90
  • Thank you, this was the best method and I only had to remove the ';' to make it work in my VB code. – JimP Mar 13 '13 at 17:44
1

You really want to take a look at Cross-Page Posting in ASP.Net

It uses the PreviousPage property of the Page object. That was you can do away with trying to pass the value via the querystring.

e.g.

C#

TextBox textBox = (TextBox)Page.PreviousPage.FindControl("TextBox1");

VB

Dim textBox as TextBox = CType(Page.PreviousPage.FindControl("TextBox1"), TextBox)
Tim B James
  • 20,084
  • 4
  • 73
  • 103
0

You can try some jQuery function like below to set postback url:

$(function(){
$('#Button1').click(function(){
    $('#Button1').attr("action", "/MAP/routeToHospital.aspx?RunNumber=" + $('#TextBox1').val());
   });
});

or add this on blur event of textbox:

$(function(){
$('#TextBox1').blur(function(){
    $('#Button1').attr("action", "/MAP/routeToHospital.aspx?RunNumber=" + $('#TextBox1').val());
   });
});
Pratik
  • 1,472
  • 7
  • 20
  • 36