2010-07-01 40 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; }