3

就像询问这样做的最佳方式一样。我正在检索一个 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 %> />

因为目前我收到编译错误。

4

2 回答 2

2

您可以简单地使用,为什么需要转换它int

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

你可能会得到value哪个不是int类型。

或使用TryParse

<%
   int productId = 0;
   Int32.TryParse(Request.QueryString["productId"], out productId);
%>
<input type="hidden" id="hiddenProdIdEditProduct" value='<% productId %>' />
于 2013-09-23T09:29:19.873 回答
1

无需在 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;
于 2013-09-23T09:30:34.150 回答