Click here to Skip to main content
15,889,116 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi,
I need to call button click event code behind function at every 5 seconds. the button click should be automatically fired using java script SetInterval. but at runtime i had error "object reference not set". please help me. and one more thing it should be possible on user control.thanks in advance.

What I have tried:

<asp:button ID="updateButton" runat="server" Text="" OnClick="updateButton_Click" Style="display:none"/>

java script:
setInterval(function () {
$("#updateButton").click();
}, 5000);

and code behind

protected void updateButton_Click(object sender, EventArgs e)
{
name.innerText="some dynamic value";
}
Posted
Updated 26-Jul-16 1:28am
Comments
F-ES Sitecore 26-Jul-16 6:47am    
What line gives the error?
Member 11125335 26-Jul-16 6:50am    
setInterval(function () {
$("#updateButton").click();
}, 5000);

while entering the click event "object reference null" error occured

1 solution

When working with WebForm's server controls at the client, It's always recommended to use the control's ClientID when referencing them. So try something like this:

JavaScript
setInterval(function () {
   $("#<%= updateButton.ClientID %>").click();
}, 5000);


Or this:

JavaScript
setInterval(function () {
  __doPostBack('<%=updateButton.ClientID%>','');
}, 5000);
 
Share this answer
 
v2
Comments
Member 11125335 26-Jul-16 9:18am    
Thank you very much. its working.
Vincent Maverick Durano 26-Jul-16 9:29am    
Glad to be of help :)

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