Click here to Skip to main content
15,895,777 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
JavaScript
<script type="text/javascript">
document.getElementById("TextBox1").innerText= 'Hi";
</script>

HTML
XML
<form id="form1" runat="server">
  <div>
    <asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>
  </div>
</form>


Code Behind
C#
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
    string Query = string.Format("INSERT INTO GMaps(latLng) VALUES('{0}')", TextBox1.Text);
    SqlConnection cn = new SqlConnection("ConnectionString");
    SqlCommand cmd = new SqlCommand(Query, cn);
    cn.Open();
    cmd.ExecuteNonQuery();
    cn.Close();
}
Posted
Comments
Sajith Dilhan 24-Mar-15 7:14am    
You are not calling the javascript from your code. When the textbox is set to AutoPostBack = "True" it will always call the TextBox1_TextChanged event.

Try something like this
<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True" OnTextChanged="TextBox1_TextChanged" OnClientClick="function1()">

and
<script>
function function1()
{
document.getElementById("TextBox1").innerText= 'Hi";
}
</script>

1 solution

To call javascript you have to add
onblur="your_javascript_function" and if you don't want the postback return false from it.

ASP.NET
<asp:textbox id="TextBox1" runat="server" autopostback="True" ontextchanged="TextBox1_TextChanged" onblur="return your_javascript_function()" xmlns:asp="#unknown">
</asp:textbox>


Also, this line
JavaScript
document.getElementById("TextBox1").innerText= 'Hi";

would not work due TextBox1 id in the html is NOT TextBox1, but something like Page_Ctl01_Ctl02_TextBox1

ASP.NET ensures that all control ids are unique and server mangles the ids you wrote so they are unique by prepending them with parents

You can access the element like this:
JavaScript
document.getElementById("<%=TextBox1.ClientID %>").innerText= 'Hi";


If this helps please take time to accept the solution. Thank you.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900