HTML/ASP.NET: <input type="hidden" name="reference" value="ABC"/>
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3154906/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HTML/ASP.NET: <input type="hidden" name="reference" value="ABC"/>
提问by abramlimpin
I just wanna ask if there's a possibility to change:
我只是想问问是否有可能改变:
<input type="hidden" name="reference" value="ABC"/>
into this:
进入这个:
<input type="hidden" name="reference" value="any values I want"/>
where I can set any values behind .cs/C# - making it dynamically. The payment gateway I'm using requires and I can't find a way to included an ASP.NET control ( ?) and I'm needing your suggestions/comments about it. Thanks.
我可以在 .cs/C# 后面设置任何值 - 动态设置。我使用的支付网关需要,但我找不到包含 ASP.NET 控件 (?) 的方法,我需要您的建议/评论。谢谢。
PS. <asp:HiddenField ID="reference" runat="server" Value="ABC" />
is not working because the payment gateway specifically needs the 'name' property.
附注。<asp:HiddenField ID="reference" runat="server" Value="ABC" />
不工作,因为支付网关特别需要“名称”属性。
回答by Samantha
I know this is an old post, but for anyone looking to solve this issue now - If you add runat="server"
to the input, the name will be changed (e.g. MainContentArea_ctl00_ctl01_ctl01_amount
). ClientIdMode="Static"
will only help for the ID.
To get around this:
我知道这是一篇旧帖子,但对于现在想要解决此问题的任何人 - 如果您添加runat="server"
到输入中,名称将更改(例如MainContentArea_ctl00_ctl01_ctl01_amount
)。ClientIdMode="Static"
只会帮助ID。为了解决这个问题:
In your html page use a Literal :
在您的 html 页面中使用 Literal :
<asp:Literal runat="server" ID="litInputAmount"></asp:Literal>
In the code behind file, assign a string to the Text attribute of the Literal This string should be the html as you would like it to be. The correct value can also be added for the value field:
在代码隐藏文件中,为文字的 Text 属性分配一个字符串。该字符串应该是您希望的 html。也可以为 value 字段添加正确的值:
litInputAmount.Text = String.Concat("<input id='inputAmount' type='hidden' name='amount' value='", Price.ToString(), "'>");
This will then be compiled as:
这将被编译为:
<input id="inputAmount" type="hidden" value="224.4" name="amount">
This will give the information to the payment gateway with the correct name, but your value can be managed dynamically. Repeat for any other values that need to be added before sending.
这会将信息提供给具有正确名称的支付网关,但您的值可以动态管理。对发送前需要添加的任何其他值重复此操作。
回答by Dean Harding
You can just put runat="server"
on the control to access it from your code behind:
您只需放置runat="server"
控件即可从您的代码后面访问它:
<input type="hidden" name="reference" id="reference" runat="server" />
Then, in your code behind:
然后,在你后面的代码中:
void Page_Load(object sender, EventArgs e)
{
// ...
reference.Attriutes["value"] = "any values I want";
// ...
}
Note that in this case, the "id" attribute is required because when you have runat="server"
, the id attribute is used to specify the name of the generated variable.
请注意,在这种情况下,“id”属性是必需的,因为当您拥有时runat="server"
,id 属性用于指定生成的变量的名称。
回答by Petar Marinov
You can use standard input of type hidden as if you are working with static HTML or Razor, and rely on the <%=
expression, which is evaluated at render time rather on DataBind()
time as the <%#
expressions would.
您可以像使用静态 HTML 或 Razor 一样使用 hidden 类型的标准输入,并依赖<%=
表达式,该表达式在渲染时而不是DataBind()
像<%#
表达式那样按时计算。
This way, you can have a normal html, for which you can have ASP.NET WebFroms generate the hidden input's value for you server side, without actually having to mark the input with runat="server"
or using <asp:HiddenInput
control. See the example below, which should do the job:
通过这种方式,您可以拥有一个普通的 html,您可以让 ASP.NET WebFroms 为您的服务器端生成隐藏输入的值,而实际上不必runat="server"
用<asp:HiddenInput
控件标记输入或使用控件。请参阅下面的示例,它应该可以完成这项工作:
<input type="hidden" id="add-to-wishlist-url" value='<%= YourServerSideExpressionHere.Execute() %>' />
Of course, this approach is not one size fits all, but seems like the closest to the meet the requirement described 7 years ago...
当然,这种方法不是一刀切,但似乎最接近7年前描述的满足要求......
回答by HoseiniPeyrov
//<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
protected string GetVariableValue(string AspxPage, string inputTagName)
{
ra migirad
string RegPattern = string.Format("(?<=({0}\".value.\")).*(?=\"./>)", inputTagName);
Regex regex = new Regex(RegPattern, RegexOptions.IgnoreCase);
Match match = regex.Match(AspxPage);
if (string.IsNullOrEmpty(match.Value))
{
RegPattern = string.Format("<input[^>]*{0}[^>]*value=\"([^\"]*)\"", inputTagName);
regex = new Regex(RegPattern, RegexOptions.IgnoreCase);
match = regex.Match(AspxPage);
return match.Groups[1].Value;
}
return match.Value;
}
//<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
protected string GetVariableValue(string AspxPage, string inputTagName)
{
ra migirad
string RegPattern = string.Format("(?<=({0}\".value.\")).*(?=\"./>)", inputTagName);
Regex regex = new Regex(RegPattern, RegexOptions.IgnoreCase);
Match match = regex.Match(AspxPage);
if (string.IsNullOrEmpty(match.Value))
{
RegPattern = string.Format("<input[^>]*{0}[^>]*value=\"([^\"]*)\"", inputTagName);
regex = new Regex(RegPattern, RegexOptions.IgnoreCase);
match = regex.Match(AspxPage);
return match.Groups[1].Value;
}
return match.Value;
}