【问题标题】:How to pass a variable to another ASP.net page如何将变量传递到另一个 ASP.net 页面
【发布时间】:2017-03-09 22:40:00
【问题描述】:

好的,所以我有一些 c#,它生成样式为列表项的 href 锚标记并将其扔到 aspx 页面上,就像这样;

html += "<a href='../InspectionView.aspx' class='list-group-item' id=''>Inspection ID: " + inspectionID + " - Due Date: " + inspDueDate + " - Inspector(s): Bob Williams <span style='min-width:75px' class='label label-primary pull-right'>" + status + "</span></a>";

现在这是一个循环,变量从 SQL 数据库中提取并用于填充该 html 字符串。

现在,我想要做的是,当用户单击其中一个生成的 href 并被重定向到下一页时,变量 inspectID 被向前传递。我想可能有办法将它存储在 href 标签的 ID 中,但我不知道从那里去哪里。

非常感谢。

【问题讨论】:

  • 你要找的概念叫做“查询字符串”
  • 您需要在该行中构建要传递的 id... 在 URL 的末尾,添加一个 ?inspectionID = insert value here。如果您有多个值,则在每个键/值对之后添加“&nextValue="

标签: c# html asp.net


【解决方案1】:

添加query string 参数。

html += "<a href='../InspectionView.aspx?inspectionID='" + inspectionID + " class='list-group-item' id=''>Inspection ID: " + inspectionID + " - Due Date: " + inspDueDate + " - Inspector(s): Bob Williams <span style='min-width:75px' class='label label-primary pull-right'>" + status + "</span></a>";

在接收页面上阅读:

string inspectionID = Request.QueryString["inspectionID"];

https://msdn.microsoft.com/en-us/library/system.web.httprequest.querystring(v=vs.110).aspx

【讨论】:

  • 在代码请求中["variableName"];假设您使用的是 C#
  • 谢谢,太好了。
【解决方案2】:

一个非常简单的方法是插入一个查询字符串。由于这不是服务器控件,因此它可能是唯一的方法。

类似...

html += "<a href='../InspectionView.aspx?InspectionID="+HttpUtility.UrlEncode(Inspection_ID.ToString())+"&anyotherQSField="+HttpUtility.UrlEncode(anyotherQSFieldVariable) + "' class='list-group-item'> - Due Date: " + inspDueDate + " - Inspector(s): Bob Williams <span style='min-width:75px' class='label label-primary pull-right'>" + status + "</span></a>";

然后在 InspectionView.aspx 中,获取类似以下内容的值:

String strInspection_ID = Request.QueryString["InspectionID"];

您可能需要将其转换为字符串才能用于 ID。 您不必为 Inspection_ID 使用 HttpUtility.UrlEncode,但如果您想在 QS 中使用其他可能包含空格或其他奇数字符的字符串 - 这将是明智的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-15
    • 2019-03-06
    • 2015-12-17
    相关资源
    最近更新 更多