Use different name in asp:hiddenfield - asp.net

I have a for which is built within an ASP.net usercontrol. This form is then used within a CMS as part of integration with a merchant gateway. The gateway requires that a number of hiddenfields be passed in which is fine in the main however one of these needs to be called Profile. The CMS I am using also defines a global variable called Profile and as such when I try and add a hidden field with this ID I get errors.
Is there a way of setting the 'name' property to Profile, and the ID to something different?

How about using plain HTML:
<input type="hidden"
name="Profile"
id="SomeId"
value="<%= Server.HtmlEncode("some value") %>"
/>

Related

In ASP.NET, can I allow a string starting with a number to be an ID?

I'm new to ASP.NET (building for Sitefinity) and working on a Salesforce form for a client. Some of their inputs looks like this:
<input id="00N1600000Ernn3" maxlength="50" name="00N1600000Ernn3" size="20" type="text" />
So I created an input in asp like this:
<asp:TextBox ID="00N1600000Ernn3" MaxLength="50" runat="server" ClientIDMode="Static" />
When I compile I see this error coming from the designer file:
invalid token '00' in class, struct, or interface member declaration
If I switch the id to a start with a character it fixes the issue. My problem is Salesforce uses that as the name when submitted to update their database and analytics.
The Control ID is used to generate a property in the 'designer' file. Therefore, the ID must conform to the naming rules for identifiers. These rules specify that the identifier must start with a letter:
https://msdn.microsoft.com/en-us/library/aa664670(VS.71).aspx
Sitefinity does have a Salesforce Connector tool but the use of that module is tied to what license your client is using. One approach I've taken is instead of trying to match the ids of the Salesforce form, you can just paste the form html in a user control and then register that user control as a widget in Sitefinity using Thunder.
You may have to add in a button control to specify the postback url for the form:
<asp:Button runat="server" ID="btnSubmit" PostBackUrl="https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8" Text="Submit" />

ASP.NET: Can I read the ViewState to restore a control in the client site?

I am curious: is it possible to read the initial state of a DropDownList control using JavaScript?
Let’s say that when the page is loaded in the browser, the dropdown has ten options. Then, using JavaScript I remove all the options.
Can I read the ASP.NET ViewState to get the initial ten options and restore them?
The short answer is yes you can use JavaScript to read the viewstate values as they are stored in a field called __viewstate, which is rendered in the browser as an input field like this:
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="..." />
The problem you will run into is the __viewstate value is encrypted.
My suggestion is to use a hidden field to store the values of the dropdown or whatever else you want to store and then access the value like this:
<input type="hidden" id="hiddenField" runat="server" value="" />
Then in your code-behind, since the input has the runat="server" you can set the value to whatever you wish, like this:
hiddenField.Value= ViewState["dropdownvalues"].ToString();
Finally, you can use JavaScript to get the values from the hidden field, like this:
<script type="text/javascript">
function test()
{
var name = document.getElementById('hiddenField').value;
alert(name)
}
</script>

Is it possible to eliminate name mangling in ASP.NET?

I know I can eliminate ID mangling in ASP.NET 4.0, but can I also eliminate name mangling? This looks like the best I can do:
<input name="ctl00$body$txtName" type="text" id="txtName" />
Is that correct?
ASP.NET relies on name mangling to route posted form data to input controls in nested naming containers. The ways to avoid name mangling are:
Don't use nested naming containers such as master pages or user controls. Input controls that are placed directly on an .aspx page will have simple names.
Don't use the standard ASP.NET input controls. Instead, you could:
Put <input type="text" name="name" /> (without runat="server") in the .ascx/.aspx and access its value via Request.Form["name"].
Create a custom server control that does the same.
Ok, so here's the deal. I needed to change the values of form elements dynamically (server side), I need to keep the MasterPage, and I have panels on the page as well. There is no way around it.
What I have done instead, is use server side "yellow tags", and public variables:
HTML:
<input type='hidden' name='x_login' id='x_login' value="<%= x_login %>" />
Code:
Public x_login As String = "some value"
And to access the value after a postback:
Request.Form("x_login")
Thanks to Michael Liu's answer for the very last bit there. I've upvoted his answer just for that reason.

Why can't I access the value of a hidden input field from my ASP.NET code-behind?

From my page's code-behind I want to access the value of this hidden field. The value is set properly. I confirmed by checking its value.
<div class="hiddenValues">
<input id="HiddenReportId" type="hidden" />
</div>
From my code behind, I'm using the following to access the above input
string id = Request.Form["HiddenReportId"];
When I run the application this line throws a null exception. Any suggestions ? Thanks!
The input needs to be inside of the form tag (which it may be, can't tell from the code snippet). Also, it needs to have a name attribute:
<div class="hiddenValues">
<input id="HiddenReportId" name="HiddenReportId" type="hidden" />
</div>
Its id attribute may be redundant and isn't necessary if you're not using it. But form elements are identified by their name attributes in a POST.
(It feels a bit unintuitive from an ASP.NET perspective to the uninitiated, I know. ASP.NET convention is to identify everything by an ID, but web browsers use name when crafting a POST. And the web browser knows nothing of the server-side technology being used, it follows HTTP standards instead.)

ASP.net over ride client ID for form elements

Given a textbox:
<asp:Textbox runat="server" id="txtAddress1" />
This renders as something similar to:
<input name="ctl00$mainContent$txtAddress1" type="text" id="ctl00_mainContent_txtAddress1" />
I don't think browsers autocomplete features recognise this name/ID as a field they can autofill, they are not standard recognised names.
Is there any way to overide the client ID's so that autocomplete has a better chance of recognising them?
2 Points with this.
1) The "Override the Name" feature was introduced in ASP.Net 4.0, where for any property you can choose a hardcoded name instead of the dynamic name. You need to be careful on this as you don't want 2 objects sharing a name.
2) ASP.Net 2.0 and above (may have been in v1.0) has a property on the control called "AutoCompleteType" which provides a hint to the browser on what sort of information is required in the box.
Assuming you're using Asp.net 4.0, and you're aware of the points mentioned by DJIDave, you can use the ClientIDMode property on a control, and set it to 'Static'. Then, what ever you specify in the Id field in Asp.Net will be brought through to your final markup, and will not be 'mangled' (for want of a better word) by Asp.Net.

Resources