Click here to Skip to main content
13,139,082 members (51,585 online)
Rate this:
 
Please Sign up or sign in to vote.
See more:
Hi All,

Please any one suggest me how to fire button click event on page load in asp.net.
But we do not want to call method in the place of button click event on page load.

Thanks

Mukesh kumar
Posted 26-Sep-12 20:54pm
Comments
OriginalGriff 27-Sep-12 2:58am
   
Why would you want to do that? It means that the page load would immediately trigger a roundtrip to the server.
Ignoring that this is
a) a bit stupid and
b) not a nice user experience
it is also silly in that unless you are careful (and document it all pretty well) you risk future versions looping, since the round trip will cause a postback, which causes a page load, which tiggers a click, which causes a postback...
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

you can directly call your button click event like this.

//btnSubmit is my asp.net button control's id
btnSubmit_Click(btnSubmit,null);


or using jquery like this..
$(document).ready(function(){
   //Id of your button control either it is server  control or simple html control
   $("[id*='btnSubmit']").click();
});



if you want to call your button click event in your page load you can directly call like

protected void Page_Load(object sender, EventArgs e)
{
    //btnSubmit is my asp.net button control's id
    //It will fire each and every time your page load event is fire.
    btnSubmit_Click(btnSubmit,null);
}
  Permalink  
v4
Comments
Strange_Pirate 27-Sep-12 6:29am
   
Better Described code:

< a href="#" id="someButton"> Foo < /a >
<script type="text/javascript">
$(document).ready(function() {
// bind the click event
$('#someButton').click(function() {
alert('baz');
});

// trigger the click event
$('#someButton').click();
});
</script>
Tejas Vaishnav 27-Sep-12 8:28am
   
no need to write click event in jquery to call it.
if you simply write $("[id*='btnSubmit']")click(); in you document's ready state then it will automatically call or post back your page and fire that button click event.
Strange_Pirate 27-Sep-12 9:40am
   
I just try to help him out, if we provide the whole thing as ready mate then that person have no idea how it works. and also he/she become lazy to work with their own self. so i always provide hints not direct solution so they can resolve their problem by them selves so if in future they get any problem regarding this then they can handle it easily.
Tejas Vaishnav 28-Sep-12 1:40am
   
You are trying to tease me or what..??
Strange_Pirate 28-Sep-12 7:07am
   
No way man...!
Following You.
Great articles and knowledge sharing by you.
Wanna learn so many new things from you .!
Will You help me to do so .?
Balaji Moorthy 26-Sep-15 13:43pm
   
no...................... he is right actually
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 5

<script type="text/javascript">
        window.onload = function callButtonClickEvent() {
            document.getElementById('<%=btnSubmit.ClientId %>').click();
        }
</script>
  Permalink  
Comments
Tomas Takac 13-Nov-14 6:29am
   
Why are you posting an answer to a question almost 2 months old? Moreover you solution is the same as #4.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

Hi,

you can create one javascript function to call your server side button click event like :

document.getelementbyid('btn1').click();


and you can call this javascript function on page load.

I hope it may be help you.

Thanks,
Viprat
  Permalink  
v2
Comments
Er. Gayatri 27-Sep-12 3:28am
   
Hi,

Can you please give details of why do you want to do it or what do you actually want to do?
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

you can use below javascript function to call button click event in page load event.

<script type="text/javascript">
        window.onload = function callButtonClickEvent() {
            document.getElementById('<%=btnSubmit.ClientId %>').click();
        }
</script>



Thanks,
Vipul Patel
3i InfoCom
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web02 | 2.8.170915.1 | Last Updated 13 Nov 2014
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100