4

hello iam trying to get the id from a url and send it to the clint side this is what i did

this is my url :

http://localhost:53010/edit.aspx?Id=4

code behind

    Public Partial Class Edit
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) 
Handles Me.Load

End Sub
Private _myId As String = Request.QueryString("id")

Public Property myId() As String
    Get
        Return _myId
    End Get
    Set(ByVal value As String)
        _myId = value
    End Set
End Property

End Class client

<%= myId%>

error

Request is not available in this context

this is also what i get when i move the private prop to page_load() "private " is not valid on local variable declaration – any idea what is going on

Thanks


i solve this problem here is the answer

Public Partial Class Edit
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) 
Handles Me.Load
    MyIdVal = Request.QueryString("id")


End Sub

Private _myIdVal As String
Public Property MyIdVal() As String
    Get
        Return _myIdVal
    End Get
    Set(ByVal value As String)
        _myIdVal = value
    End Set
End Property

End Class

1
  • You need to learn how VB.Net classes and fields work. Commented May 22, 2012 at 14:35

4 Answers 4

3

That's a field initializer.
Field initializers run before the constructor and cannot access the instance they're initializing.
Therefore, you can't use the Request property there.

You need to move that to the constructor or Page_Load.

Sign up to request clarification or add additional context in comments.

6 Comments

when i move it to the page load i get this error "private " is not valid on local variable declaration
You need to move the initialization.
myId = Request.QueryString("id")
when i do Dim myId As String = Request.QueryString("id") it works fine but as i know i need a property to send myId to the client side right ?
You need to declare a field in the class, and set its value in Page_Load.
|
1

You're accessing the Request too early.

It will work if you set myId on Init, Page_Load or any other similar page event.

2 Comments

Your explanation (first line) is incorrect; this has nothing to do with the page lifecycle.
when i move it to the page load i get this error "private " is not valid on local variable declaration –
0

Try to set _myId in your PageLoad.

1 Comment

when i move it to the page load i get this error "private " is not valid on local variable declaration –
-1

So I wanted a class with properties that were set from querystrings and found this thread. I also wanted to be able to access properties on the front page and even in JavaScript from a single location. Here is what I came up with:

// App_Code/QueryStrings.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for QueryStrings
/// </summary>
public class QS
{
    private int id = -1;

    public QS()
    {
        if (HttpContext.Current.Request.QueryString["id"] != null)
            try
            {
                Int32.TryParse(HttpContext.Current.Request.QueryString["id"], out id);
            }
            catch
            {
                id = -2;
            }
        else
            id = -3;
    }

    public int ID
    {
    get
        {
            return id;
        }
    }
}

Then you can call it from your .aspx page as follows:

<body>
    <form id="form1" runat="server">
    <div>
        <% QS qs = new QS(); %>
        ID = <%= qs.ID %>
    </div>
    </form>
</body>

Of course you can call from code behind with the same syntax.

1 Comment

You are using tryparse (returns bool, does not ever throw) and then catching exception on it. id = -2 will never happen.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.