Monday, March 26, 2012

Passing variable to another page

This is my first post on these forums so I apologise if it's a common problem. I'm fairly new to ASP.NET.

The situation is as follows:

On one web page, I have created some textboxes where users can search for records in a db (SQL Sever 2005 Beta). The results of their search are then displayed in a datagrid.

The datagrid also has two 'hyperlink' columns which the user can click to be redirected to other pages where related data is displayed. I use the 'URL format string' property of the hyperlink column to display the correct record by using the following string: NameOfForm.aspx?patid={0}. All of this works fine.

One of the hyperlink columns takes the user to a second aspx page which contains a datagrid and shows the many records related to the record the user originally searched for. This second datagrid also has two 'hyperlink' columns, 'Add' and 'Edit'.

These do pretty much as they say. 'Add' allows the user to add a record to the many end of a database relationship, and 'Edit' displays the specific record selected for editing\viewing. Once again all works well so far.

The problem comes when the datagrid on the second web form is empty (i.e. there are no records in the related table in the db) and the hyperlink columns are not visible. I thought I'd get around this by adding a 'Hyperlink' control to my web form. This redirects the user to a web form where a related record can then be added.

This also works but I what I need is the correct 'patid' to be passed onto this page, which is required as the foreign key of the relationship. How can I pass this variable to the next page?

I hope I've described the problem clearly enough.

TIA,

MoYou can pass the patid in the same way you did in the URL before (NameOfForm.aspx?patid={0}).

You can populate it from Request.QueryString["patid"] - which will grab the value you passed before.

Hope that makes sense.

DJ
You can pass the patid in the same way you did in the URL before (NameOfForm.aspx?patid={0}).

You can populate it from Request.QueryString["patid"] - which will grab the value you passed before.

Hope that makes sense.

DJ

Thanks for the reply DJ.

I have already used Request.QueryString ("patid") on the target page. This works fine when the 'sending' page has records to display. When there are no records in the datagrid, the 'patid' does not get passed on. The url field in the target page reads 'http://localhost/Ertocs/AddCancers.aspx?patid={0}' which is not what I'm expecting.

I've set the 'Navigate URL' property of the hyperlink control to 'AddCancers.aspx?patid={0}'. Is this correct or am I missing something simple? Thanks again for your help.

I must be doinf something wrong
I presume you are using Response.Redirect to get to the target page.

Use:

Response.Redirect(String.Format("AddCancers.aspx?patid={0}", Request.QueryString["patid"]));

You are not specifying what needs to be placed at the 0 index.

HTH

DJ
I presume you are using Response.Redirect to get to the target page.

Use:

Response.Redirect(String.Format("AddCancers.aspx?patid={0}", Request.QueryString["patid"]));

You are not specifying what needs to be placed at the 0 index.

HTH

DJ

Thanks for the help DJ. In the end I changed the hyperlink control to a link button with this code:

Server.Transfer("AddCancers.aspx")

with Request.Querystring("patid") in the target page and it works!
Check if the datatable/dataset is Nothing, and if that condition is true, set the visibility of your hyperlink control to true.

0 comments:

Post a Comment