Issue Related to Hidden Field in asp.net - c#

I have an asp.net application. i have added Hidden field on Master Page..
<asp:HiddenField runat="server" ID="hiddenCriteria" Value="abcd"/>
When I try to get the value of Hidden field from Javascript :
alert(document.getElementById("ct100_hiddenCriteria").value);
I get the following value :
How to get real stored value ?

var labelID = '<%= hiddenCriteria.ClientID %>';
alert(labelID);

Try setting ClientIDMode to Static and it will output the ID of the control as hiddenCriteria
Makes it more JavaScript friendly.

You can use client id in the javascript function as follows
alert(document.getElementById('<%= hiddenCriteria.ClientID %>').value);
Or you can take benifit of ClientIDode to static as follow
<asp:HiddenField runat="server" ClientIDMode="Static" ID="hiddenCriteria"
Value="abcd"/>
and simply
alert(document.getElementById("hiddenCriteria").value);
For further reading
http://www.codeproject.com/Articles/34151/ASP-NET-4-0-Client-ID-Feature

Related

How to get value if a input fields defined as ClientIDMode="Static"

I defined input control on webform as
<input type="text" id="Amount1" class="auto-sum" ClientIDMode="Static" runat="server">
and on runtime it appears as below.
<input name="ctl00$ContentPlaceHolder1$Amount1" id="Amount1" class="auto-sum" type="text">
When i try to get the value from CodeBehind i can see the control name
i get nothing as Amount. how to get value of such input field
You can try this
TextBox1.Text = Amount1.Value
I would recommend against using ClientIDMode="Static". This can cause problems later on. Especially since you seem to be using ContentPlaceHolder, which would indicate a Master Page.
Consider the following. You add TextBox1 with a static ID on the Master Page, some time later you do the same on a Page which uses a Master Page. You now have two elements on the page with ID TextBox1, instead of ctl00$TextBox1 and ctl00$ContentPlaceHolder1$TextBox1.
If you need it for Client side purposes, you can always use TextBox1.ClientID
<asp:TextBox ID="Amount1" runat="server" CssClass="auto-sum"></asp:TextBox>
<script type="text/javascript">
document.getElementById("<%= Amount1.ClientID %>").value = "It works!";
</script>

How to get value in C# from java script?

I need to get a value that I got in Javascript to my code behind file..
My design code is:
<asp:DropDownList ID="ddlStatusOverview" runat="server" Width="95px" onchange="CheckSelectedItem(this)" >
</asp:DropDownList>
I'm using the dropwonlist in "ItemTemplate" of asp:ListView.
My javascript coding is:
<script type="text/javascript">
function CheckSelectedItem(ddl) {
alert(ddl.value);
}
I want to get that "dll.value" value in code behind.. I already used Webmethod concept. But my problem is I need to get the value in ".ascx" page.. How to do that..?
I don't know how to use hidden field value concept too..
Do this :
Add a hidden field element :
<input name='lala' id='lala' type='hidden'/>
Add this :
function CheckSelectedItem(ddl) {
document.getElementByID('lala').value=ddl.value;
}
On server side :
you can get the value by :
Request.Form["lala"].ToString();
Accoring to your comment :
If I want to call a server side function from javascript with this
"ddl.value" means, how to do that?
Please read this
ASP.NET pass a javascript value in server side
Introduce a hidden field.
<script type="text/javascript">
function CheckSelectedItem(ddl) {
document.getElementByID('hfStatus').value = ddl.value;
// if using jQuery
// $("hfStatus").val(ddl.value);
}
</script>
<asp:HiddenField runat="server" ID="hfStatus" />
Now you can directly access hfStatus from code-behind.

Can't get text with text box when it is readonly?

I have a text box
<asp:textbox runat="server" id="checkIn" ClientIDMode="Static" ReadOnly="true">
</asp:textbox>
The text in the text box is inputted through a Jquery DatePicker. In some code behind I am getting the text from this text box like so.
string x=checkIn.Text;
Why can I not pull the inputted date from the text box? I am guessing it is to do with the fact that it is readonly as when I remove this it works?
Can anyone help me?
In ASP.Net, if the readonly value is changed, it will revert to the original value on the postback.
You can use a wrokaround however, instead of specifying readonly declaratively, assign it as an attribute in code-behind. i.e.
instead of
<asp:textbox runat="server" id="checkIn" ReadOnly="true"...
apply this is code-behind
checkIn.Attributes.Add("readonly", "readonly");
But, the viewstate still may not work with this.
More info:
There is a subtle difference between readonly and disabled controls in HTML. The disabled ones will not be submitted with the form, however the readonly ones will. Literally, readonly is just readonly, but disabled is actually disabled.
From W3C: http://www.w3.org/TR/html401/interact/forms.html#h-17.12
(Get down to section 17.13.2 Successful controls under 17.13 Form submission)
ASP.Net however, reverts to the original value on postback if a control is declared like that i.e. if the attribute is set during init. Which is why setting the attribute later (in page load) will not affect this behavior.
Use HTML input tag instead of asp:textbox
Use this:-
<input type="text" readonly="readonly" runat="server" id="checkIn" clientidmode="Static"/>
Instead of this:-
<asp:textbox runat="server" id="checkIn" ClientIDMode="Static" ReadOnly="true"></asp:textbox>
Write the following code in your code behind file, for asp.net .cs or class file and for vb in .vb file.
You will have to write it on page load event, depending on if you want it before postback or after or irrespective of both...
textbox.Attributes.Add("readonly", "readonly");
Try to make readonly to false before posting the values through jQuery or Java-Script. It should work.
$('#tbox').removeAttr('readonly');
Suppose I have a asp:button then on client click I need to call a function which remove the read-only attribute from the text-box.
<asp:Button ID="MessageButton" runat="server" Text="Hellow"
OnClientClick="return changeAttribute()" />
<asp:textbox runat="server" id="checkIn" ClientIDMode="Static" ReadOnly="true">
</asp:textbox>
and change attribute function in javascript as follows
function changeAttribute()
{
$('#checkIn').removeAttr('readonly');
return true;
}
You can also leave the readOnly attribute off in the asp:TextBox control and use jquery $('#textbox').attr('readOnly','readOnly').
You can actually get the value of a readonly textbox if it is within the form.
var text = Page.Request.Form[TextBox.UniqueID];
Sorry this response is a few years late!
Apply in asp.cs Page : AttrName.Attributes.Add("readonly", "readonly");
Please use:
Request.Form[txtText.UniqueID]
I had the same issue with jQuery, however fixed with a trick:
$('#txtBoxID').removeAttr('readonly');
/// Do your stuff here..
$('#txtBoxID').add('readonly');
;)

Pass Javascript Variable To C# Code Behind

How to get value from javascript to C# code behind ? I have an idea with the code below. In javascript code I want to assign the value of "HiddenField" Control with string value and then I want to take this value from "HiddenField" in code behind. But with this code I cannot do it. Can you tell me how to make it ?
<script>
$(function () {
document.getElementById('HiddenField').value = "active";
console.log(<%= this.HiddenField.Value %>)
});
</script>
<asp:HiddenField ID="HiddenField" runat="server" Value="5" Visible="true" />
you need to use ClientID property of control to get actual element ID in DOM.
<script>
$(function () {
document.getElementById('<%= HiddenField.ClientID%>').value = "active";
console.log(document.getElementById('<%= HiddenField.ClientID%>').value)
});
</script>
<asp:HiddenField ID="HiddenField" runat="server" Value="5" Visible="true" />
Use the Control ID for HTML markup that is generated by ASP.NET.
document.getElementById('<%= HiddenField.ClientID%>').value = "active";
When a Web server control is rendered as an HTML element, the id
attribute of the HTML element is set to the value of the ClientID
property. The ClientID value is often used to access the HTML element
in client script by using the document.getElementById method.
Then send the Hidden value through the javascript function as a variable while calling the controller
Surely works,
Cheers
Phani*
you may take a look at mshtml
As far as I know you call with this C# functions from your javascript code ;-)

Creating unique id for textbox

i like to have unique ids for textboxes and hidden filds .is there any property which will give unique id in asp.net ?
something like
<asp:textbox id="ctr001_1" runat="server" uniqueid="textbox" />
1st thing, why do you need to set the UniqueID property? FYI, ASP.NET web server controls has its own auto-generated readonly UniqueID property that you can access in your coding like below:
Textbox1.UniqueID()
The UniqueID is generated based on the control hierarchy, and each control's UniqueID property is, obviously, unique.
You need to use the TextBox form System.Web.UI.WebControls there you have property UniqueID.
http://msdn.microsoft.com/en-us/library/system.web.ui.control.uniqueid.aspx
Hm, not sure if this is what you are trying to achieve, but try this:
Set an attribute to your control, for example uniqueid="textbox" as you've done, then access the id by myControl.Attributes["uniqueid"].
ASP.NET will generate a UniqueID for the both TextBox and HiddenField on the page. Simply add each tag in markup and specify the ID:
<asp:textbox id="TextBox1" runat="server" />
<asp:HiddenField id="HiddenField1" runat="server" />
Both of these values can be accessed in the codebehind by:
TextBox1.Text
HiddenField1.Value
You can also see their Unquie ClientID by look at:
TextBox1.ClientID
HiddenValue1.ClientID

Categories

Resources