How can the html encoding for a text box be disabled? - c#

I have some troubles with a text box input control value on an ascx page. It's value is somehow always html encoded, and I don't know how it can be disabled.
For example when the value contains a < character it is always converted to <. The strange thing is, it only happens on fields like Name.Lastname (which have a child property). My first thought was it could be caused by the Html extension method
Html.TextBoxFor(m => m.Name.LastName, new { maxlength = "100" })
but this is not the case, because when I use the html input directly, it's value is still encoded:
<input id="Name_LastName" maxlength="100"
name="Name.LastName"
type="text" value="<%= Model.Name.LastName %>" />
Does somebody know how the html encoding of text box values for fields like Name.LastName (with a child property) can be disabled?

After more research I found out that it was caused by a javascript function which variables were initialized using the <%:, and this function was used to initialize the text boxes. So in the end it had nothing to do with child properties. I changed the <%: with <%= in the javascript part:
var lastName = "<%: Model.Name.LastName %>";
in
var lastName = "<%= Model.Name.LastName %>";

Related

How to maintain state of a label during postback?

How to maintain a value assigned to a label through javascript after postback?
Problem: I have assigned a label value using a clientside function.
But whenever postback happened, label values are gone.
Solution i found: After searching a lot, all are suggessting to store and retrieve
the value to & from a hidden field.
Note: But i want to achieve this without using hidden field as it may
increase pageload time.
The state of label is not maintained in ViewState by asp.net. The labels are converted in to spans and the html of span is not posted on submitting form, this is why changes made by client are not persisted. You can put the state of label in some hidden field when you change it in javascript and access it on server.
HTML
<input id="hdnLabelState" type="hidden" runat="server" >
Javascript
document.getElementById('<%= hdnLabelState.ClientID %>').value = "changed value of span";
Server side (code behind)
string changedLabelValue = hdnLabelState.Value;
use html hidden field
<input type="hidden" runat="server" id="hiddenlabel">
try an html label, like
<label id="lbl" runat="server">Myv alue</label>

updating textbox value

I have an input tag having some text in it. Now I would like that onclick of a button the text will be changed.
For some reason it is not being changed.
This is the input tag:
<input id="network_table" type="text" value="oldValue" runat="server"/>
the following is the way I am trying to change the value of the input tag:
network_table.Value = "newValue";
network_table.Text = "newValue";
Bind the "onclick" event and apply this these methods:
In jQuery :
$('#network_table').val("your val");
http://docs.jquery.com/Val
Javascript
document.getElementById('network_table').value = "your val";
you can do it serverside with "OnClick" event on button, assuming your controls are defined with runat="server" attribute
http://msdn.microsoft.com/fr-fr/library/system.web.ui.webcontrols.button.onclick(v=VS.80).aspx
You could try and assign some meaningless class to the input and use that as a reference point to get in hold of the input field.
<input id="network_table" type="text" value="oldValue" runat="server" class="myInputField"/>
$('.myInputField').val('newValue');
Using the id will not work because you are using the 'runat=server' and it makes the id unavailable on client side and you would need to get the unique id first to be able to get in hold of it. This is a lot cleaner way but you need to make sure not to use the class elsewhere to avoid ambiguous results.
Here is a jsfiddle example which does what you want but on load.
http://jsfiddle.net/yX5ze/

Want to access literal value into javascript

I have got a literal control on page (with some data on it). i want to access it in javascript and want to put some text on it. please tell me how can i access literal control in JavaScript. I was trying with following code-
<asp:Literal ID="lblPerInstanceListing" runat="server"></asp:Literal>
Javascript:
var value= document.getElementById('<%=lblPerInstanceListing.ClientID %>')
I am getting null value return by this.
An ASP.NET Literal control does not by itself insert any HTML into a webpage. Your Literal control is a placeholder for text or HTML you will set in your code behind.
You should wrap your literal in a DIV or SPAN and give that an ID and reference that DIV or SPAN in the JavaScript
WebForm:
<span id="yourId"><asp:Literal ID="lblPerInstanceListing" runat="server"></asp:Literal></span>
JavaScript:
Solved by this code.
var value= document.getElementById('yourId').innerText;
<span id="yourId" runat="server"></span>
js :
var value= document.getElementById('yourId').innerHTML;
It is not better then use this code??
You can put in span enableviewstate to false if You need.
It's a Literal. It renders literal HTML to the page. It doesn't render any container matching the ClientID that you can access on the client side.
Either put a div or span in the literal with an ID you can use, or use e.g. a Label or Panel control that does render a wrapping span or div you can use.

Keeping a hidden input box name the same using c#

I am trying to integrate with a payment gateway. They require a set of hidden input boxes to be posted to their gateway like:
<input id="orderref" name="orderref" type="hidden" runat="server"/>
I have added runat="server" so I can dynamically populate the boxes with values.
However, of course at runtime the input box name gets changed to:
<input name="ctl00$ContentPlaceHolder1$orderref" type="hidden" id="ContentPlaceHolder1_orderref" value="9" />
I'm in a catch 22. If I remove runat="server" all is fine with regards to the name, but I then can't populate the input box with values! Is there a means to force the name of the field to stay the same?
Depending on what .NET version you are using the control over this differs: basically, pre-.NET4 you can only alter the container prefix by implementing your own, but from .NET4 you can omit the container prefixes using ClientIDMode.
Alternatively, you may expose methods, say, on your page or master page and then call them using inline-scripting (<%=MyMethodReturningValue() %>) which is evaluated at the time of render.
EDIT:
To elaborate a little on my second suggestion, you can define a method in your pages code-behind that can be executed in an inline manner by use of embedded code blocks; the referenced link gives simple examples of this, but the methods needn't be in <script> blocks of the page itself (as I previously touched on) so that you can keep your logic separated, such as:
Define a method in your page's code-behind:
public string RenderMessage()
{
return "This need not be a hard-coded string!";
}
Write out your input element, omitting the runat attribute, and adding the embedded code block in place of where the value would be (think of this as a place holder); this code block is going to call the specified method on pre-render, and essentially be replaced by the returning value:
<input id="orderref" name="orderref" type="hidden" value='<%=RenderMessage() %>'/>
If you are using ASP.NET 4.0, you can set clientidmode="Static" on the <input> which will cause the ID to stay exactly as you set it. There are caveats to this, but in general should work for your needs.
Documentation on ClientIDMode
Edit: As it turns out, they need the name attribute to be static, which ClientIDMode does not control, and can not be set programatically in ASP.NET, it only controls the id attribute.
Use the following code:
Page.ClientScript.RegisterHiddenField("orderref", "9")
This creates the following hidden input:
<input id="orderref" name="orderref" type="hidden" value="9" />
For more details see System.Web.UI.ClientScriptManager.RegisterHiddenField on MSDN
I also struggled with this issue and eventually found the following solution:
In the markup I have the following:
<%=html %>
In the codebehind I have a protected variable:
protected string html;
I then set this variable as follows:
html = "<input id='Express_Receipt_Email' type='hidden' value='" + sessionUser.EmailAddress + "' name='Express_Receipt_Email'>";

how to get the value from input control of html

I have a table which is generated by a 3rd party control. This table has only one row and one column. Within this column is html text as follows :
<p>
this is a test</p>
<p>
<input name="amount" type="text" value="this is for amount" /></p>
<p>
this is a test</p>
<p>
<input name="test" type="text" value="this is for test" /></p>
<p>
this is a test</p>
the problem is how to get the value saved inside the html input control ?
I tried the following code but it fails:
t.Rows[0].Cells[0].FindControl("amount");
thanks in advance...
You can use the below snippet to retrieve the required values inside the javascript function:
function getValue()
{
//First method
var val= document.getElementsByName("amount");
alert("Val by Element Name:-" +val(0).value);
//Second method
val= document.getElementById("amount");
alert("Val by element Id:-" +val.value);
//third method
val= document.getElementsByTagName("input");
alert("Val by Tag:-" +val(0).value);
alert("Val by Tag:-" +val(1).value);
}
Assumption: You're aware of the names of the input controls inside the table obtained by the 3rd party tool.
Do you mean when the form is posted back? In that case, just use
Request["amount"]
to get the value.
Since it is a 3rd party control, it should give you access to the values of the controls throught the properties of the controls. Check the properties and documentation.
Use a HiddenControl and jQuery.
<script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
<script>
$(documet).ready(function(){
var inputVals = '';
$('#yourThirdPartyTableID').find('input[type=text]').each(function() {
inputVals = inputVals + $(this).val() + ', ';
});
$('#<%= YourHiddenControlID.ClientID %>').val(inputVals);
});
</script>
The above code will insert values of all text inputs from your third party table into the hidden control separated by a comma. You can change the comma into anything else to make it easier to split on server side code.
If you're looking for the value of the input with "name" of "amount", do the following:
<script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
<script>
$(documet).ready(function(){
$('#<%= YourHiddenControlID.ClientID %>').val($('#yourTableID').find('[name="amount"]').val());
});
</script>
There is no straight forward method to retrieve the value of an input control unless you say runat="server".
My suggestion is to use the runat="server" tag and try getting the value using VALUE property.
Otherwise you can use Page.FindControl('') and see whether the control is returning other than null...then i feels that will work.
if you are using ajax or master-content page then the controls id will be not the one you given....so make sure you are giving the correct id itself (using view source of the page)

Categories

Resources