Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET VB.NET
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 at 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 at 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 at 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 at 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 at 28-Sep-12 1:40am
   
You are trying to tease me or what..??
Strange_Pirate at 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 .?
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 at 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
0 OriginalGriff 555
1 Sergey Alexandrovich Kryukov 518
2 CPallini 490
3 George Jonsson 231
4 Afzaal Ahmad Zeeshan 194
0 OriginalGriff 5,450
1 CPallini 4,500
2 Sergey Alexandrovich Kryukov 4,032
3 George Jonsson 3,057
4 Gihan Liyanage 2,445


Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 27 Sep 2012
Copyright © CodeProject, 1999-2014
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