【问题标题】:ASP.net - Get ID from URLASP.net - 从 URL 获取 ID
【发布时间】:2012-05-10 04:28:13
【问题描述】:

我有一个数据列表,我正在通过 URL 将用户的用户 ID 发送到另一个页面:

<asp:HyperLink ID="LastNameLabel" runat="server" Text='<%# Eval("LastName") %>' NavigateUrl='<%# FormatUrl( (int) Eval("FriendsAccountID")) %>' />

和功能:

protected string FormatUrl(int FriendsAccountID)
{
    return "WebForm3.aspx?" + FriendsAccountID;
}

这可行,但我有点菜鸟,我不确定在下一页上后如何获取 ID。有什么想法吗?

【问题讨论】:

    标签: asp.net url query-string


    【解决方案1】:

    使用会话并使用query string传递您的ID。然后从其他页面您可以轻松访问该ID

    【讨论】:

      【解决方案2】:

      将您的代码更改为:

      protected string FormatUrl(int FriendsAccountID) { return "WebForm3.aspx?UserID=" + FriendsAccountID; }
      

      然后您可以像这样访问 UserID:

      Request.QueryString["UserID"];
      

      这是QueryString on MSDN的链接。

      【讨论】:

      • 可以直接进入页面加载WebForm3吗?
      • 是的。每个 Webform 都可以访问 Request 对象。
      【解决方案3】:

      您应该将 ID 设置为该 url 变量,如下所示:

      protected string FormatUrl(int FriendsAccountID)
      {
        return "WebForm3.aspx?FriendID=" + FriendsAccountID.ToString();
      }
      

      在其他页面(在您的情况下为 WebForm3.aspx)用户 Request.Params 或 QueryString 以获取该值:

      protected void Page_Load(object sender, EventArgs e)
      {
          int friendID = -1;
      
          if (Request.Params["FrientID"] != null && int.TryParse(Request.Params["FrientID"], out friendID)
          {
            // you got a valid friend id in friendID variable
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-10-03
        • 1970-01-01
        • 2012-07-05
        • 2020-01-30
        • 2016-01-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多