Siddiq Baig
Siddiq Baig

Reputation: 586

get rid of autogenrated javascript event in asp.net

in asp.net appliaction i am getting autogenrated javascript event which i realy dont want. i have a button named login because of this autogenrated javascript events my client is not able to login which is genrated automatically and i realy dont know why this is happing

Asp.net Button in custome user Control .ascx file

<asp:Button ID="cmdLogin" runat="server" Text="Login" CssClass="Footer_button" 
                        CommandName="login"  CausesValidation="True" />

As you can see there is no event at all in the button but when i run my application in any browser it render the autogenrated event

AutoGenrated javascript event

    <input name="ctl00$header1$slidingpanel1$Login1$cmdLogin" value="Login" id="ctl00_header1_slidingpanel1_Login1_cmdLogin" class="Footer_button" type="submit"
onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;ctl00$header1$slidingpanel1$Login1$cmdLogin&quot;, &quot;&quot;, true, &quot;&quot;, &quot;&quot;, false, false))">

i want to get rid of this auto genrated onclick event and also it is rendring an onsubmit event in aspnetform in browser

<form name="aspnetForm" method="post" action="default.aspx" onsubmit="javascript:return WebForm_OnSubmit();" id="aspnetForm">

i want to get rid of this autogenrated javascript event Kindly help me regarding this issue as other can also have this problem Thank in advance

Upvotes: 1

Views: 4677

Answers (4)

Gyanendra
Gyanendra

Reputation: 1

function ButtonWorkAgain() { window.WebForm_OnSubmit = function () { return true; }; }

Upvotes: -1

Siddiq Baig
Siddiq Baig

Reputation: 586

yes nothingisneccessary it is javascript and i have figer it out tommarow that why this is happining for other i want to share it....

Acutlly i am using Asp.net login control and its event Authenticate from code behind thats why i dont need to put click event on button because this button is under login control and haddling the login authentication by asp.net login control event "Authenticate" so no need to put any event on button but login control from code behind

<asp:Login ID="Login1" runat="server">
                <LayoutTemplate>
                    <label for="log">User Name: </label>
                    <asp:TextBox ID="Username" runat="server" ></asp:TextBox>

                    <label for="pwd">Password: </label>
                    <asp:TextBox ID="password" TextMode="Password" runat="server"></asp:TextBox>
                  // this is the Button where javascript event were rendered in browsre
                    <asp:Button ID="cmdLogin" runat="server" Text="Login" CommandName="login" CssClass="Footer_button" />                       
                </LayoutTemplate>
 </asp:Login>

This login Control is under custome Control file .ascx which i registered this control in Default.aspx page also there are more custome control in dafault page which i regestered in Default.aspx page like below

<%@ Page Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false"
    CodeFile="Default.aspx.vb" Inherits="_Default" Title="Minato Cars - Home Page" %>

<%@ Register Src="~/Controls/CallRequest.ascx" TagName="CallRequest" TagPrefix="psc" %>
<%@ Register Src="~/Controls/RequestACar.ascx" TagName="RequestACar" TagPrefix="psc" %>
<%@ Register Src="~/Controls/Skype.ascx" TagName="Skype" TagPrefix="psc" %>
<%@ Register Src="~/Controls/EmailInfo.ascx" TagName="EmailInfo" TagPrefix="psc" %>
<%@ Register Src="~/Controls/QuickContact.ascx" TagName="QuickContact" TagPrefix="psc" %>
<%@ Register Src="~/Controls/SearchByVehicleType.ascx" TagName="SearchByVehicleType" TagPrefix="psc" %>
<%@ Register Src="~/Controls/SearchbyMake.ascx" TagName="SearchByMake" TagPrefix="psc" %>

(this is the main point to understand)

i had some asp.net validation in CallRequest.ascx & in QuickContact.ascx Custome Control. and the default page is using Masterpage..

As we all know whenever we use Asp.net Validation it render the javascript event _dopostback in browser with onsubmit event in form Or in asp.net aspnetform and some time it also render the onclick event on the button which type is submit (type=submit of button) that is why i was getting autogenrated javascript event due to asp.net validation. i have handeld it by using jQuery validation without form and now its working.. althoug i have test Cause Validation of submit button but the problem was still same which i have never faced before... but its ok Jquery validation did his job well for know...

Sorry for my english

Upvotes: 0

nothingisnecessary
nothingisnecessary

Reputation: 6253

It's javascript so you can totally get rid of it. (Should you? That wasn't the question..)

Just put this at the bottom of your page (will throw an exception if called though);

window.WebForm_OnSubmit = null;
window.WebForm_DoPostBackWithOptions = null;

Or override it to do what you want, including no-op:

window.WebForm_OnSubmit = window.WebForm_DoPostBackWithOptions = function() { };

Or use javascript to remove it from the controls when the document is rendered:

document.getElementById("ctl00_header1_slidingpanel1_Login1_cmdLogin").onclick = null;

or

document.getElementById("ctl00_header1_slidingpanel1_Login1_cmdLogin").onclick = function()
{
    alert("My custom onclick handler");
}

and to remove the postback handler from the form:

document.getElementById("aspnetForm").onsubmit = null;

But if you are not using postback and not attaching a client onclick handler to this button what the heck are you using it for? Just submitting the form normally? Please provide more details.

I usually override the ASP.NET version of doPostBack and/or form.onsubmit with my own custom function that takes care of other stuff, like force a wait for pending AJAX calls to finish (or abort them).. depends on what I'm trying to achieve.

Upvotes: 2

Murali Murugesan
Murali Murugesan

Reputation: 22619

You cannot change this event hooking model built with ASP.Net webforms.

As long as you need, Server side event handlers like 'onclick', 'onselectedindexchanged', etc you need to call when the event is triggered from page.

This event mapping is done by the javascript code like onclick, onchange, etc.

ASP.Net internally adds a script(.axd) in your page and handles the WebForm_PostbackOptions function.

Check out How to prevent PostBack without using JavaScript? also

if you want to prevent event binding, stop using server side events and handle it by yourself, or use ASP.Net MVC

Upvotes: 0

Related Questions