将查询字符串值指定为隐藏字段值

本文关键字:隐藏 字段 查询 字符串 | 更新日期: 2023-09-27 18:32:20

就像询问执行此操作的最佳方法一样。我正在 aspx 页中检索查询字符串值,我想将此值分配为隐藏输入字段的值。

<%int productId = 0;
  if (Request.QueryString["productId"] != "" && Request.QueryString["productId"] != null)
  {
      productId = Convert.ToInt32(Request.QueryString["productId"]);
  } %>
<input type="hidden" id="hiddenProdIdEditProduct" value=<% productId %> />

就像目前一样,我收到编译错误。

将查询字符串值指定为隐藏字段值

你可以简单地使用它,为什么你需要把它int .

<input type="hidden" id="hiddenProdIdEditProduct" value='<% Request.QueryString["productId"] %>' />

您可能会得到不属于int类型的value

或使用TryParse

<%
   int productId = 0;
   Int32.TryParse(Request.QueryString["productId"], out productId);
%>
<input type="hidden" id="hiddenProdIdEditProduct" value='<% productId %>' />

无需直接在 ASP.NET aspx 页中使用此逻辑。

在服务器端分配它,例如在 Page_Load 事件中。

int productId = 0;
if (Request.QueryString["productId"] != "" && Request.QueryString["productId"] != null)
{
  productId = Convert.ToInt32(Request.QueryString["productId"]);
}
hiddenProdidEditProduct.Text = productId;