Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
Updated 8-Jan-19 2:56am
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...

you can directly call your button click event like this.

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


or using jquery like this..
JavaScript
$(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

C#
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);
}
 
Share this answer
 
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 .?
XML
<script type="text/javascript">
        window.onload = function callButtonClickEvent() {
            document.getElementById('<%=btnSubmit.ClientId %>').click();
        }
</script>
 
Share this answer
 
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.
Hi,

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

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


and you can call this javascript function on page load.

I hope it may be help you.

Thanks,
Viprat
 
Share this answer
 
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?
you can use below javascript function to call button click event in page load event.

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



Thanks,
Vipul Patel
3i InfoCom
 
Share this answer
 
this function is what you need:
public static void DoClick(this System.Web.UI.WebControls.Button btn)
    {
        System.Reflection.MethodInfo m = typeof(System.Web.UI.WebControls.Button)
            .GetMethod("OnClick", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
        m.Invoke(btn, new object[] { EventArgs.Empty });
    }
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900