2010-07-01 80 views
4

我只想问,如果有改变的可能性:HTML/ASP.NET:<INPUT TYPE = “隐藏” 名称= “参考” 价值= “ABC”/>

<input type="hidden" name="reference" value="ABC"/> 

到这一点:

<input type="hidden" name="reference" value="any values I want"/> 

其中我可以在.cs/C#后面设置任何值 - 使其动态生成。我使用的支付网关要求,我找不到一种方法来包含ASP.NET控件(?) ,我需要您的建议/评论。谢谢。

PS。 <asp:HiddenField ID="reference" runat="server" Value="ABC" />不起作用,因为支付网关特别需要“名称”属性。

回答

5

我知道这是一个旧帖子,但对于现在想要解决此问题的任何人 - 如果您将runat="server"添加到输入中,名称将会更改(例如MainContentArea_ctl00_ctl01_ctl01_amount)。 ClientIdMode="Static"只会对ID有帮助。 为了解决这个问题:

在HTML页面中使用文字:

<asp:Literal runat="server" ID="litInputAmount"></asp:Literal> 

在代码隐藏文件,指定一个字符串字面的Text属性,该字符串应该是HTML,你会喜欢它。正确的值也可以为值字段添加:

litInputAmount.Text = String.Concat("<input id='inputAmount' type='hidden' name='amount' value='", Price.ToString(), "'>"); 

这将被编译为:

<input id="inputAmount" type="hidden" value="224.4" name="amount"> 

这将信息提供给支付网关使用正确的名称,但你的价值可以动态管理。重复发送前需要添加的任何其他值。

3

你可以把runat="server"上的控制,从您的代码中访问它背后:

<input type="hidden" name="reference" id="reference" runat="server" /> 

然后,在后面的代码:

void Page_Load(object sender, EventArgs e) 
{ 
    // ... 

    reference.Attriutes["value"] = "any values I want"; 

    // ... 
} 

注意,在这种情况下,“ID “属性是必需的,因为当你有runat="server"时,id属性用于指定生成变量的名称。

+2

其实你不需要Attributes属性。由于ASP.NET在添加runat =“server”时使其成为HtmlInputHidden类型的控件,所以您可以直接编写:'reference.Value =“我想要的任何值”;' 欲了解更多信息,请访问:http://msdn.microsoft .COM/EN-US /库/ system.web.ui.htmlcontrols.htmlinputhidden(VS.80)的.aspx。 – XIII 2010-07-01 09:31:55

0

//<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; }