Mina Gabriel
Mina Gabriel

Reputation: 25070

request querystring in variable

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

Upvotes: 4

Views: 5483

Answers (4)

user1984748
user1984748

Reputation: 7

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.

Upvotes: -1

SLaks
SLaks

Reputation: 887195

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.

Upvotes: 3

Alex
Alex

Reputation: 35399

You're accessing the Request too early.

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

Upvotes: 1

Erwin
Erwin

Reputation: 4817

Try to set _myId in your PageLoad.

Upvotes: 0

Related Questions