How to clear textbox using jquery(with ConfirmButtonExtender)? - javascript

I have mentioned my code below, the problem is I am unable to clear textbox. Kindly see my javascript code for textbox clearing and piont out the error?
<script type="text/javascript">
function CancelClick() {
document.getElementById("<%=txtname.ClientID %>").innerHTML =txtname.Text='';
}
</script>
<asp:UpdatePanel ID="updatepanel1" runat="server"><ContentTemplate>
<div>
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</asp:ToolkitScriptManager>
<asp:UpdatePanel ID="updatepanel2" runat="server">
</asp:UpdatePanel>
<asp:Button ID="btnConfirm" runat="server" Text="Confirm" onclick="btnConfirm_Click" />
<asp:ConfirmButtonExtender ID="ConfirmButtonExtender1" TargetControlID ="btnConfirm"
ConfirmText ="Are you sure you want to click this?" OnClientCancel ="CancelClick"
ConfirmOnFormSubmit="false" runat="server">
</asp:ConfirmButtonExtender>
<asp:TextBox ID="txtname" runat="server"></asp:TextBox>
</div>
</ContentTemplate>
</asp:UpdatePanel>

With jQuery (as you asked for)
<script type="text/javascript">
function CancelClick() {
$("#<%=txtname.ClientID %>").val("");
}
</script>

You need to set the value not the innerHtml:
document.getElementById("<%=txtname.ClientID %>").value = '';

<script type="text/javascript">
function CancelClick() {
var textBoxId = '<%= #txtname.ClientID %>';
$(textBoxId).val('');
}
</script>

Related

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.

Trying to download a file from a button in UploadPanel. JS function InitializeRequest won't be triggered

I'm trying to download a file if user clicks a button which is in UploadPanel. I'm following a forum page that was suggested here.
When I run my ASP.NET page, I get alert 1 and 2 but not 3. What would be the problem? Here is my code:
Default.aspx
<body>
<form id="form1" runat="server">
<asp:ScriptManager runat="server" />
<script language="javascript">
var prm = Sys.WebForms.PageRequestManager.getInstance();
alert(1);
prm.add_initializeRequest(InitializeRequest);
alert(2);
function InitializeRequest(sender, args)
{
alert(3);
if (sender._postBackSettings.sourceElement.id == "DownloadFile")
{
var iframe = document.createElement("iframe");
iframe.src="GenerateFile.aspx";
iframe.style.display = "none";
document.body.appendChild(iframe);
}
}
</script>
<asp:UpdatePanel runat="server">
<ContentTemplate>
<asp:Button runat="server" ID="DownloadFile" Text="Generate File" />
</ContentTemplate>
</asp:UpdatePanel>
</form>
it could be a problem if your Button Id is not matching because server and client id not same
you should use this
<asp:Button runat="server" ID="DownloadFile" ClientIDMode="Static" Text="Generate File" />

Unable to Redirect to another page using Javascript

In html file to redirect a page using Javascript i used like this
window.location.href = "http://www.google.com/";
Its working fine. But when i tried in .aspx it is not working
Below is my code. Thanks in advance
<head runat="server">
<script type="text/javascript">
function PageRedirect() {
window.location.href = "http://www.google.com/";
}
</script>
</head>
<body>
<form runat="server">
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="PageRedirect()"/>
</form>
Try this:
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="PageRedirect(); return false;"/>

Grabbing Textbox in javascript

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<script type="text/javascript">
function Incrementer()
{
debugger;
var txtBox = document.getElementById('ctl00_MainContent_TextBox1').value;
alert(txtBox);
}
</script>
<asp:TextBox ID="TextBox1" runat="server" Text="0"></asp:TextBox><br />
<asp:Button ID="Button1" runat="server" Text="Up" OnClientClick="Incrementer();"/>
<asp:Button ID="Button2" Text="Down" runat="server"/>
</asp:Content>
I am unable to capture the textbox in JavaScript. What is the problem?
I've re tagged this ASP.net as well because it's quite specific to this problem.
When an ASP.net textbox control is rendered on a page it is assigned a dynamic ID that can change. There is a property for controls, ClientID that provides you with the controls assigned ID so you can use it in your scripts.
Use as follows:
<script type="text/javascript">
function Incrementer()
{
debugger;
var txtBox = document.getElementById('<%=TextBox1.ClientID%>').value;
alert(txtBox);
}
</script>
Try this
var txtBox = document.getElementById('<%=TextBox1.ClientID%>').value;
alert(txtBox);

AJAX UpdatePanel.Visible Property not working with Javascript

I have code below. I want to hide the update panel by using Javascript (without going to server) when the user clicks Hide button. Although javascript funciton seems to be working fine in debugging, it does not hide!
<script type="text/javascript" language="javascript">
function Show() {
document.getElementById("UpdatePanel1").Visible = true;
}
function Hide() {
document.getElementById("UpdatePanel1").Visible = false;
}
</script>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:Button ID="btnShow" runat="server" Text="Show" OnClientClick="Show(); return false;" />
<asp:Button ID="BtnHide" runat="server" Text="Hide" OnClientClick="Hide(); return false;" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<br />
<asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine"></asp:TextBox>
<br />
<asp:Button ID="btnSubmit" runat="server" Text="Gönder"
onclick="btnSubmit_Click" />
<br />
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
you need to use the UpdatePanel1's client id so
document.getElementById('<%=UpdatePanel1.ClientID%>');

Categories

Resources