开发者

HTML/ASP.NET: <input type="hidden" name="reference" value="ABC"/>

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.

PS. <asp:HiddenField ID="reference" runat="server" Value="ABC" /> is not working because the payment gateway specifically needs the 'n开发者_JS百科ame' property.


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:

In your html page use a 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:

    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.


You can just put runat="server" on the control to access it from your code behind:

<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.


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.

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:

<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...


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


Apply the parameter:

 ClientIDMode="Static"

For example:

<asp:HiddenField ID="reference" runat="server" Value="ABC" ClientIDMode="Static" />

with this, the "ID" with maintain exactly as "reference".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜