JimP
JimP

Reputation: 37

TextBox value used in url parameter

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.

Upvotes: 2

Views: 6592

Answers (3)

Pratik
Pratik

Reputation: 1512

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());
   });
});

Upvotes: 0

rs.
rs.

Reputation: 27427

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));
}

Upvotes: 2

Tim B James
Tim B James

Reputation: 20364

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)

Upvotes: 1

Related Questions