cant get text of text box when it dissabled(value changed by javascript)

hi

I cant get the text property of texbox properly, if its dissabled. I can get the value if i changed it with server script but i cant get  value when i tried to change the value by client side script(javascript)

 

0
ajith
9/19/2008 9:10:13 AM
asp.net.getting-started 91979 articles. 4 followers. Follow

6 Replies
1124 Views

Similar Articles

[PageSpeed] 38

 this works for me:

  

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>

<script type="text/javascript">

window.onload = function()
{
var textbox = document.getElementById('<%= MyTextBox.ClientID %>');

// change value from the textbox
textbox.value = 'My new text';

// display the new value
alert(textbox.value);
};


</script>


</head>
<body>
<form id="form1" runat="server">
<div>

<asp:TextBox ID="MyTextBox" runat="server" Enabled="false" Text="some text"></asp:TextBox>

</div>
</form>
</body>
</html>
 

 


Simply JS | june blog
0
x
9/19/2008 12:00:43 PM

I can set the value through Javascript but problem is while retrieving the value from server side(eg: getting textbox text in button click( Textbox1.text) ), i cant get the new value

 

0
ajith
9/19/2008 2:08:39 PM

The TextBox loads it's Text property from postback data so it seems unlikely it would make any difference whether the text was set programmatically or manually entered by a user. So it would appear something else is causing your problem. Is the control dynamically created or simply a static part of your aspx? Can you post sufficient code for us to reproduce the behavior?


"Man is the Only Animal that Blushes. Or needs to." (Mark Twain)

Remember to "mark as answer" any relevant and useful posts!
0
redbull2
9/19/2008 2:17:07 PM

ajith P T:

I can set the value through Javascript but problem is while retrieving the value from server side(eg: getting textbox text in button click( Textbox1.text) ), i cant get the new value

As far as I know ASPNET doesn't remember ViewState for DISABLED controls in the page, that's why you can't get the new values on PostBacks.  So try using ReadOnly instead and set it a code behid part... More info then refer below

http://www.vikramlakhotia.com/ReadOnly_and_Enabled_property_of_TextBox.aspx

 

Here's a working example for your reference:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
     <script type="text/javascript" language="javascript">
        function changeValue()
        {
         document.getElementById('<%= TextBox1.ClientID %>').value = "A new value";
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server" Text="A defualt value"/>
        <asp:Button ID="Button1" runat="server" Text="Set Values Via JavaScript" OnClientClick="changeValue();" />
        <asp:Button ID="Button2" runat="server" Text="Get Values in Code Behind" OnClick="Button2_Click" />
    </div>
    </form>
</body>
</html>

RELEVANT CODES

 

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class TestNew : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        TextBox1.Attributes.Add("readonly", "readonly"); 
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
       
        string strValue =  HttpContext.Current.Request.Form[TextBox1.UniqueID.Replace("_","$")];
        Response.Write(strValue);
    }
}
 

Regards,Vinz

"Code, Beer and Music" that's my way of being a programmer!

How to get your Forum Question Answered | Blog | CodeASP.NET
0
vinz
9/19/2008 3:00:06 PM

You don't need viewstate to persist the text property - it's postback data. Viewstate is needed to persist some other attributes (such as styles) and also to remember the previous text value to raise the TextChanged event.

That said, you're absolutely right that it's because the textbox is disabled - in this case it appears the textbox is loading it's "previous value" from viewstate directly as it's Text (I tried setting a text before disabling, and it is persisted). I didn't test if it loads the postback data when viewstate is disabled.


"Man is the Only Animal that Blushes. Or needs to." (Mark Twain)

Remember to "mark as answer" any relevant and useful posts!
0
redbull2
9/19/2008 3:09:00 PM

Thanks... its helpful

0
ajith
9/20/2008 6:10:37 AM
Reply:

Similar Artilces:

get the changed value of a text box
Hello everybody! I have this in the server side: <asp:textbox id="TEscala" runat="server"></asp:textbox> <asp:imagebutton id="BIrEscala" runtat="server" imageurl="images/irmapa.gif"> </asp:imagebutton> And i want to do this: Private Sub BIrEscala_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles BIrEscala.Click Dim xx As String xx = Tescala.value End Sub The problem is that "xx" doesn´t get the new value written in the ...

How do I get the Text of a Text ....
That may sound confusing, but I need to get the text of a Dynamically created text object. For each row the Text objects text might be somthing different, and when I loop through all the rows, I need to see if any of them are a certain Word. When I used Getitemstring, it doesn't like it. dw_1.Describe( 'text_control.text' ) will give you the .text attribute of a text control in a datawindow. How are you getting different values for the .text attributes on different rows? Using an expression or something? -- Paul Horan[TeamSybase] VCI Springfield, MA www.v...

How do I get the left part of a text box.text
How do I get the left part of a text box.text???Something like: textbox1.text.left(3) to get 'abc' if the text box contains the alphabeth.But then the correct way You can do something as: string mainString = "abcdefg....";string strSubString = mainString.SubString(0,2); Hope this helps, RegardsBilal Hadiar, MCP, MCTS, MCPD, MCTMicrosoft MVP - Telerik MVP Use the Substring() method.string tempstring = TextBox1.Text.Substring(0,3); thanks all...

Getting evaluated text value for text objects
I'm attempting to get the visible text value out of a text object in a report. If I describe the .Text attribute, I can get the text out, however, if the value is an expression I get the unevaluated string. I can describe the Evaluate function called with the .Text attribute as the argument, and this will normally return the evaluated text from the element. The problem I have encountered is when you have, for example, a field named "phone" and the text value of the text element is also the word "phone". Calling evaluate on this will replace the word &quo...

get value of text box
I new to c# programming. What I have is a webpage with two text boxes and a button When the button is submitted I would like to check the value of of the text boxes in an if statment and compare it to a string. What is the best way to do this. I am using if (txtbox.text ="somevalue") { do something }   I receive Cannot implicitly convert type 'string' to 'bool' when I load the page. any help is appreciated. Hi in C# you check equals with == if (txtbox.text == "somevalue") { do something }Thanks,Teemu KeiskiFinland, EU...

Why cant I get the Cell Text value of this
I am using a Datagrid with TemplateColumns, Why can I not read the value of: <ItemTemplate> <%# DataBinder.Eval(Container.DataItem, "BusUnit") %> </ItemTemplate> Is there code I need to do to reference the <%# DataBinder.Eval(Container.DataItem, "BusUnit") %> data "differently" than a BoundColumn??. I use it just fine with it as a Bound Column, and then get the value e.Item.Cells("0).Text to do my DELETE.... Private Sub dgBusUnits_DeleteCommand(ByVal source As Object, ...

How to get the values from the HTML text box
I have a HTML text box (NOT a TextBox server control) ina .aspx page and a button server control. Is it possiblewith ASP.NET to get the value of the HTML text box when Iclick the button? How can I get the value of an HTMLelement from VB on the server? ''''''''''''''''''''''''''''''The Code''''''''''''''''''''''''''''''''''''&#...

Getting value of a text input box.
Hi, i'm trying to get the value of a text input box in a form.  I think i may be over-complicating the situtation, but this is basically what i'm trying to do:String scriptString += "<script language=JavaScript>\n"; scriptString += "document.write('document.getElementById('hour01').value')";scriptString += "</script>\n"; String TextField = ClientScript.RegisterStartupScript(typeof(Page), "arrayScript2", scriptString); Of course ClientScript.RegisterStartupScript doesn't actually return a value, so i kno...

Get text box value from gridview
Hello there,    I have text box on a gridview cell i am trying to get the texbox value when I click on button but it return emty string all the time. Thank you in advance. this my code:  protected void btnbtnView_Click(object sender, EventArgs e) {DataSet dss = new DataSet();for (int i = 0; i < gvDetails.Rows.Count; ++i) { string QTY = ((TextBox)gvDetails.Rows[i].Cells[0].FindControl("txtRequierQTY")).Text;string PartID = gvDetails.Rows[i].Cells[6].Text; lblMessage.Text += "QTY" + QTY;lblMessage.Visible = true;  dss = da.GetLocation(int...

How to get value from dynamic text boxes
<Script Runat="Server"> Sub Page_Load Dim intCounter As Integer If Not IsPostBack Then ViewState( "ProductFieldCount" ) = 1 ElseIf ViewState( "ProductFieldCount" ) > 1 Then For intCounter = 2 To ViewState( "ProductFieldCount" ) AddProductField( intCounter ) Next End If End Sub Sub AddProductFieldCount( s As Object, e As EventArgs ) ViewState( "ProductFieldCount" ) += 1 AddProductField( ViewState( "ProductFieldCount" ) ) End Sub Sub AddProductFiel...

how to get the disabled text box value???
Hi I need to get the disabled text box value, when i am trying to access with x=txtName.text ; it's not returning anything when it is disabled. I can not use request["txtName"]; thanks sridhar I don't see any problems to get value from disabled textbox. I created textbox and disabled it both ways (in properties and programmatically), and I can get value without any problems. Looks like you have other problems. hi, the enabled / disabled property does not affect the value! string text=mytextbox.text should work!...

length or trim ? how can i get textbox.text as text that publichouse to textbox.text as text that public ?
hello friends length or trim ? how can i get textbox.text as text that publichouse to textbox.text as text that public ? second example is ..... textbox1.text is greenhouse.. i want to get green from textbox.text cheer Mark as me if my question or my answer can be helpful for you :) Hi,you can always perform a Trim to a retrieved string to delete the leading and trailing space characters. For your second example where you want to gt a part of the retrieved string you can use the SubString method of the String class.Grz, Kris. Read my blog. Handy Firefox plugins for web developers.Workar...

How can I get the text box.text property of my usercontrol textbox
 I had created a numeric textbox using windows control library and when I added the  text box in my project I cannot get the textbox.text property, How can I get the text box.text property of my usercontrol textbox Thanks in advance Regards Anu Palavila Hi, Do you have this code in your User control? <DefaultProperty("Text"), ToolboxData("<{0}:BaseTextBox runat=server></{0}:BaseTextBox>")> _Public Class TextField    Inherits TextBox ../sara...

how to get Text Box value in java script
i am using master pages. i want get the  Textbox value in java script You can pass the client id of the TextBox in a javascript function and then use it there. txtBox1.Attributes.Add("onblur", "javascript: txtBox_onBlur('" + txtBox1.ClientID + "')"); Then in your aspx page: <script type="text/javascript"> txtBox_onBlur(txtbox) {    var txtID = document.getElementById(txtBox);   alert(txtID); }Thanks,MaxLet Me Google That For You!...

Web resources about - cant get text of text box when it dissabled(value changed by javascript) - asp.net.getting-started

JavaScript Kit- Your comprehensive JavaScript, DHTML, CSS, and Ajax stop
Your comprehensive JavaScript, DHTML, CSS, and Ajax stop

Let's Code: Test-Driven Javascript by James Shore — Kickstarter
James Shore is raising funds for Let's Code: Test-Driven Javascript on Kickstarter! A screencast series focusing on rigorous, professional Javascript ...

Eloquent JavaScript: A Modern Introduction to Programming
... and pragmatics. I loved the tutorial-style game-like program development. This book rekindled my earliest joys of programming. Plus, JavaScript! ...

JavaScript - Wikipedia, the free encyclopedia
programming styles. JavaScript was formalized in the ECMAScript language standard and is primarily used in the form of client-side JavaScript ...

Open Directory - Computers: Programming: Languages: JavaScript: Tools: Obfuscators
... editor - report abuse/spam - help the entire directory only in Tools/Obfuscators Description Top : Computers : Programming : Languages : JavaScript ...

Facebook Open-Sources JavaScript Static-Type Checker Flow
The latest software to be open-sourced by Facebook is Flow , a static-type checker for JavaScript . (more…) New Career Opportunities Daily: ...

Under the Hood: The JavaScript SDK - The use of polyfills - Facebook
Facebook Engineering hat eine Notiz mit dem Titel Under the Hood: The JavaScript SDK - The use of polyfills geschrieben. Du kannst den vollständigen ...

JavaScript
The Facebook SDK for JavaScript provides a rich set of client-side functionality for adding Social Plugins, making API calls and implementing ...

Facebook App Devs Get Javascript
It has been an oft-lamented fact of the Facebook Platform thus far in its life that, in order to use all but the most simple of Javascript functionality–the ...

Badass JavaScript (@badass_js) on Twitter
WebGLWorker is an open source library that allows using WebGL from Web Workers. Great work as always from @kripken . blog.mozilla.org/research/2014/… ...

Resources last updated: 12/6/2015 4:24:37 PM