resetting the textbox through client side using javascript in asp.net - javascript

I want to reset the text box through client side using javascript. I used following ways to reset the textbox. But, they are not resetting.
cText = "";
document.getElementById("<%=txtText.ClientID %>").value = cText;
and
document.getElementById("<%=txtText.ClientID %>").value = "";
Please can anyone help me in this?
code:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script type="text/javascript">
function validate() {
var cText = document.getElementById('<%=txtText.ClientID %>').value;
if (cText != "") {
alert("Text is not null.");
document.forms['UserMaster'].elements['txtText'].focus();
$("#<%=txtText.ClientID %>").val("");
return false;
}
return true;
};
</script>
</head>
<body>
<form id="form" runat="server">
<div class="page">
<asp:TextBox ID="txtText" runat="server"></asp:TextBox>
<br/>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" Font-Bold="true" OnClientClick="return validate()"
ForeColor="#9370DB" Height="23px" Width="65px" OnClick="btnSubmit_Click"
/>
<br />
</div>
</form>
</body>
</html>

Here is the solution for your issues:
document.getElementById('<%=txtText.ClientID %>').value='';
you were using double quotes which was not working for you, whereas you need to use single quotes as show in the above code. This was your issues.
.value=" "; //is Wrong
Happy Coding.

I had the same problem and using this helped me solve the issue.
Here you can inspect the control from the browser and get the ID of that control.
// ControkClientID is the ID from Inspect element
document.getElementById(ControlClientID).value='';

Related

how to clear a textbox value in asp.net using a script

I have a textbox in my asp.net that accepts different values, I wrote a script that every time I press F5 button,the values in textboxes be cleaned.When I run the code and press F5,the textboxes values exist. How can I solve this?
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="default.aspx.cs" Inherits="wellDesigned0428._default" %>
<!DOCTYPE html>
<script runat="server"></script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link href="~/StyleSheet1.css" rel="stylesheet" type="text/css" media="screen" />
</head>
<body>
<form id="form1" runat="server">
<div class="body">
<div class="content ">
<asp:TextBox ID="txtCodePers" runat="server" CssClass="textbox" AutoPostBack="True" OnTextChanged="txtCodePers_TextChanged" MaxLength="4" ></asp:TextBox>
<asp:TextBox ID="txtName" runat="server" CssClass="textbox"></asp:TextBox>
</div>
</form>
<script type="text/javascript">
document.onkeydown = fkey;
document.onkeyup = fkey;
document.onkeypress = fkey;
function fkey(e) {
if (e.keyCode == 116) {
alert("f5 pressed");
document.getElementById("txtCodePers").value = "";
document.getElementById("txtName").value = "";
}
}
</script>
</body>
</html>
I think the issue is the id that you are using txtCodePers, it's the id from server side. Either set the clientid or check what is the id for the txtCodePers using vew source.
More than this, I would not recommend you to override the default behavior of f5 which is refresh. What if user users mouse? or uses the browser button for refresh.
The ClientID of your TextBox is not the same as given in the ID-property. So your javascript will not find the elements.
You can either use the ClientID in your script or in your simple case add the ClientIDMode-property to your textboxes:
ClientIDMode="Static"
This will result in the same ClientID as your ID and you don't have to change your javascript.

How to set the value for textbox with javascript on an ASP.NET

I need to set up asp textbox with a value from javascript input.
i have here:
<td ><asp:TextBox ID="AddressTextBox" runat="server" Text='<%# Bind("Address") %>' ClientIDMode="Static" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="AddressTextBox" ErrorMessage="*"
ValidationGroup="InsertCustomer" SetFocusOnError="True"></asp:RequiredFieldValidator>
</td>
I use autocomplete address form from here
so i placed this to the javascript code:
var src = document.getElementById("autocomplete");
var dst = document.getElementById("AddressTextBox");
and this to function fillInAddress() funcion:
function fillInAddress() {
// Get the place details from the autocomplete object.
var place = autocomplete.getPlace();
for (var component in componentForm) {
document.getElementById(component).value = '';
document.getElementById(component).disabled = false;
dst.value = src.value;
}
Im trying to get the full address from the autocomplete to the AddressTextBox field as soon as the address is selected.
But im getting an error that AddressTextBox "The name 'AddressTextBox' does not exist in the current context"
Any ideas? thanks.
AddressTextBox is server control so the ID might be changed.
Try as:
var dst = document.getElementById("<%=AddressTextBox.ClientID%>").value;
This may resolve your error.
Just run this working piece of code, this might help you:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1.Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
function setValues()
{
document.getElementById('dest').value=document.getElementById('src').value;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="src" runat="server"></asp:TextBox>
<asp:TextBox ID="dest" runat="server"></asp:TextBox>
<input type="button" value="Set Value" onclick="setValues()" />
</div>
</form>
</body>
</html>
Now you can customise the solution as per your requirement.
It looks that I made it work. I just added the following:
document.getElementById('ctl00_maincontent_FormView1_AddressTextBox').value = document.getElementById('autocomplete').value;
into function fillInAddress()

Cannot able to set text box value using javascript in ASP.NET

Any one please tell me how to set the value of textbox.
function moreFieldsEditFunction(ExtraFname, ExtraFvalue) {
document.getElementById('<%= TextBox1.ClientID %>').value =ExtraFvalue;
}
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
It is not working
I don't think that you are a million milles off to be honest, I have taken your code and put it into a web form with a button that can be clicked to call the function and it works, without seeing your mark up it will be difficult to diagnose but it should look something like this:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script type="text/javascript">
function moreFieldsEditFunction(ExtraFname, ExtraFvalue) {
document.getElementById('<%= TextBox1.ClientID %>').value = ExtraFvalue;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<input type="button" onclick="moreFieldsEditFunction('TEST', 'TEST2')" value="Click Me" />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</form>
</body>
</html>
This should work.

ASP.NET: How to get JavaScript variable from popup window?

This is code for Firefox.
Default.aspx:
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
<script type="text/javascript" src="pop.js"></script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="Hidden1" type="hidden" runat="server" />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Popup" />
</div>
</form>
</body>
</html>
Default.aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
TextBox1.Text = "Hello";
Button1.Attributes.Add("onClick", "javascript:InvokePop('" + TextBox1.ClientID + "');");
}
PopupWin.aspx:
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
<script type="text/javascript" src="pop.js"></script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtValue" runat="server"></asp:TextBox><br />
<br />
<input type="button" id="btnReturnValue" value="Return value back" onclick="ReturnValue();" />
</div>
</form>
</body>
</html>
pop.js:
function InvokePop(fname)
{
val = document.getElementById(fname).value;
retVal = window.open("PopupWin.aspx?Control1=" + fname, 'Show Popup Window', 'height=90,width=250,resizable=yes,modal=yes');
retVal.focus();
}
function ReturnValue()
{
var newValue = document.getElementById("txtValue").value;
if ((window.opener != null) && (!window.opener.closed))
{
window.opener.document.getElementById("TextBox2").value = newValue;
}
window.close();
}
In this case I click button on Default.aspx page and open Popup.aspx as popup window. I enter some value to text box in Popup.aspx and press button. The new value appears in second text box on Default.aspx page.
That works, but how can I pass the new value entered in Popup.aspx page to some handler in Default.aspx page and use it further? For example, I can have another ASPX button on Default.aspx page and when I click it I can use the value entered in Popup.aspx page.
Well what you can do is the following:
Add a hiddenField on the first page. Im calling it "hfPopupValue".
In pop.js u are currently doing this:
window.opener.document.getElementById("TextBox2").value = newValue;
You can change this to:
window.opener.document.getElementById("hfPopupValue").value = newValue;
Afterwards you can just read the value from the hiddenField.
This should solve your problem.
Have you thought of using jQueryUI's Dialog, which lets you keep all controls on the same page, meaning cleaner code.
It also doesn't look as nasty as a JavaScript popup window.

Assign converted html to a serverside control

I'm using markdown and I've got some input such as **test** which makes the word test appear in bold test and I've converted it with html like so"
var converter1 = Markdown.getSanitizingConverter();
var editor1 = new Markdown.Editor(converter1);
editor1.run();
var d = document.getElementById("wmd_input");
var html = converter1.makeHtml(d.value);
alert(html);
And this alerts <b>test</b> the issue is I need to take this value, namely <b>test</b> and access this via server side code (asp.net). I tried assigning it to a variable like so:
document.getElementById("Label1").value = html;
But it doesn't seem to work, when I go to the code behind it shows Label1 as being empty.
Is this possible?
Edit
I tried to change it to a hidden field same issue:
<script type="text/javascript">
(function () {
var converter1 = Markdown.getSanitizingConverter();
var editor1 = new Markdown.Editor(converter1);
editor1.run();
var d = document.getElementById("wmd_input");
var html = converter1.makeHtml(d.value);
alert(html);
document.getElementById('<%= h1.ClientID %>').value = html;
var h = document.getElementById('<%= h1.ClientID %>');
alert(h.value);
})();
</script>
The issue I have is I have an asp.net server side button that when clicked I try to do this:
Label1.Text = h1.Value;
That is to store the value from the hidden field to a label but this doesnt work. When I put a break point in it shows h1 is empty ""....So I'm not sure what event or how to do this so that when I make changes to my textarea wmd_input that i should be able to see these changes in my server side code...
Here's my entire asp.net form:
<html>
<head>
<title>PageDown Demo Page</title>
<link rel="stylesheet" type="text/css" href="css/demo.css" />
<script type="text/javascript" src="js/Markdown.Converter.js"></script>
<script type="text/javascript" src="js/Markdown.Sanitizer.js"></script>
<script type="text/javascript" src="js/Markdown.Editor.js"></script>
</head>
<body>
<form id="myForm" runat="server">
<div class="wmd-panel">
<div id="wmd-button-bar"></div>
<textarea cols="5" rows="5" id="wmd_input" class="wmd-input" runat="server"></textarea>
<div id="wmd_preview" class="wmd-panel wmd-preview" runat="server"></div>
</div>
<asp:button id="Button1" runat="server" Text="Set" onclick="Button1_Click"></asp:button>
<asp:button id="Button2" runat="server" Text="Get" onclick="Button2_Click"></asp:button><asp:label id="Label1" runat="server">Label</asp:label>
<asp:HiddenField ID="h1" runat="server" EnableViewState="true" />
<script type="text/javascript">
(function () {
var converter1 = Markdown.getSanitizingConverter();
var editor1 = new Markdown.Editor(converter1);
editor1.run();
var d = document.getElementById("wmd_input");
var html = converter1.makeHtml(d.value);
alert(html);
document.getElementById('<%= h1.ClientID %>').value = html;
alert(document.getElementById('<%= h1.ClientID %>').value);
})();
</script>
</form>
</body>
</html>
The asp:Label tag isn't an input control, so it won't get posted back to the server. I'd suggest using asp:HiddenField or asp:TextBox instead. (And Adil's point is crucial too, you need to make sure the client ID is actually what you think it is.)
Here's a test that works for me. On the first page load, the label shows "initial value", but the alert shows "updated". After postback, the label also shows "updated".
Edit Added the client-side update logic inside a client-side event handler.
<%# Page Title="Test" Language="C#" AutoEventWireup="true" %>
<script runat="server">
void Page_Load()
{
l1.Text = h1.Value;
}
</script>
<html>
<body>
<form runat="server">
<asp:HiddenField runat="server" Value="initial value" ID="h1" />
<asp:Label runat="server" ID="l1" />
<asp:Button runat="server" Text="do postback" />
</form>
<script>
document.getElementById('<%= Button1.ClientID %>').onclick = function () {
document.getElementById('<%= h1.ClientID %>').value = 'updated';
alert(document.getElementById('<%= h1.ClientID %>').value);
};
</script>
</body>
</html>
Labels don't post. You will have to use an input or textarea element (<asp:TextBox>). If you don't want the user to see the markup source, you can also use an <asp:HiddenField>.
you should be able to do what you want to do by using knockoutjs http://knockoutjs.com/examples/helloWorld.html You can use element binding.

Categories

Resources