English 中文(简体)
变量中的查询字符
原标题:request querystring in variable

你好,我正试图从骨髓脏取ID 并把它发送到科林特那边,这是我做了什么。

这是我的骨髓:

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

后面的代码

    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

结束类 client

<%= myId%>

错误

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

谢谢 谢谢


我在这里解决问题就是答案

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

结束类

最佳回答

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.

您需要将此移动到构建器或 Page_Load

问题回答

您重新访问 request 请求 的时间太早了 。

如果您在 Init Page_Load 或任何其他类似的页面事件上设定 myId , 或设置 Page_Load 或任何其他类似的页面事件, 将会有效 。

尝试在您的页面Load 中设置 MyId 。

所以我想要一个具有从查询字符串中设置的属性的类, 并找到这条线索。 我还想要能够访问首页上的属性, 甚至从一个地点访问 JavaScript 的属性。 以下是我所想到的:

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

然后您可以在您的.aspx 页面上将其命名为:

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

当然,你可以用同样的语法从暗号后面调用。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!