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

5

6

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 'name' property.

Everett answered 1/7, 2010 at 3:28 Comment(0)
L
8

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.

Librate answered 14/1, 2014 at 13:44 Comment(0)
P
3

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.

Pneumonoultramicroscopicsilicovolcanoconiosis answered 1/7, 2010 at 3:44 Comment(2)
Actually you don't need the Attributes property. Since ASP.NET makes it a type HtmlInputHidden control when you add the runat="server" you can directly write: reference.Value = "any values I want"; For more information: msdn.microsoft.com/en-us/library/….Viridescent
...but unfortunally, this solution will change the "name" attribute from "reference" to "MainContentArea_ct100"...etc..Booted
A
1

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

Amyloid answered 8/6, 2018 at 11:52 Comment(0)
O
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; }

Onstage answered 10/5, 2016 at 7:23 Comment(0)
I
0

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

Ingot answered 12/11, 2022 at 0:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.