Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 696
abramlimpin Avatar asked Jul 01 '10 03:07

abramlimpin


2 Answers

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.

like image 195
Samantha Avatar answered Sep 22 '22 01:09

Samantha


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.

like image 24
Dean Harding Avatar answered Sep 20 '22 01:09

Dean Harding