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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problems with retrieving querystring value

Status
Not open for further replies.

zerkat

Programmer
Jul 12, 2007
103
US
This should be simple and I do have this working on another application - the only difference is that I am not setting my querystring in the ascx page which is what I am trying to do with this. Is it even possible to set a redirect that sets a querystring value in an ascx page?

I have my navigation in an ascx page and created a reference for it in my master page. I tried to include the navigation directly in the master page but had the same issue.

Basically I am using linkbuttons in my navigation; the link buttons have click events in the ascx code behind like so:

Protected Sub lnkButton1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkButton1.Click

Response.Redirect("/subdirectoryName/page.aspx?ID=" & varName.ToString)

End Sub

Then on my aspx page in the code behind:

Private Function GetID() As Integer
Dim SomeID As String
Dim ThisID As Integer

SomeID = Request.QueryString("SomeID ")

If Not SomeID = "" Then
Try
ThisID = Integer.Parse(SomeID)
Catch ex As Exception
Throw New Exception("Invalid ID")
End Try
Else
ThisID = 0
End IF

Then I just call this function in the pageLoad Event. Unless I hard code a value into my function, I get my exception every time. If I look in the address bar of IE I can visibly see that the querystring is there. I asked a coworker if the queryString is not accessible when it's set in an ascx or master page and he said that it shouldn't matter where it's coming from.
 
ascx is a webusercontrol and are not directly related to any url parameters. rather the page should pass values to the ascx. then you can place the ascx on any page. example
Code:
class MyUserControl : UserControl
{
   public int Id {ge;set;}

   protected void Page_Load(object sender, EventArgs e)
   {
      //do something with the Id
   }
}
Code:
class MyPage : Page
{
   //this page contains an instance of MyUserControl

   protected void Page_Load(object sender, EventArgs e)
   {
      var id = (int)Response.QueryString["id"];
      InstanceOfMyUserControl.Id = id;
   }
}

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
I am not understanding your comment - rather the page should pass values to the ascx. then you can place the ascx on any page. Also not familiar with C#.

Do you mean create a class that sets the value of the ID then have the ascx inherit?
 
the ascx (more specifically, the code behind) should not pull the values from the request. In this case QueryString (it could also be session, forms, cache, cookies, etc). the user control should not know where the values it requires come from, it should simply use the values it is given.

this will allow you to place the control on any page without wondering if the values are in there proper place (querystring, session, cookie, etc).

So if the ascx doesn't get the values itself another object must give it values. This is where the page would parse the value from, in this instance, QueryString. and set the property on the ascx control.

translating C# to VB isn't difficult. there are even translators on the net to convert between the two.

Do you mean create a class that sets the value of the ID then have the ascx inherit?
I would recommend favoring composition over inheritance.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top