Reputation: 1164
I've following button on my web control,
<asp:Button runat="server" ID="btnSave" Text="Save" onclick="btnSave_Click" OnClientClick="myShowWaitScreenWithNoClose('Processing...', 'Saving survey.Please wait it may take minutes..');" CssClass="buttons" Height="46px" Width="212px" Visible="false" />
and I'm trying to click this button using JavaScript from code behind. Like,
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "scr", "setTimeout(function(){document.getElementById('btnSave').click(); return false;}, 180000);", true);
}
}
But it throws an error Error: TypeError: document.getElementById(...) is null
Upvotes: 0
Views: 188
Reputation: 63956
Is returning null most likely because the id of the btnSave control is changed when it's rendered on the client side unless you use CliendIDMode="static"
For your code to work, you need to change it to:
if (!IsPostBack)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "scr", "setTimeout(function(){document.getElementById('"+btnSave.ClientID+"').click(); return false;}, 180000);", true);
}
Upvotes: 1
Reputation: 8664
To get a server-side control via Javascript
, use the below
document.getElementById('<%=btnSave.ClientID%>').click();
Upvotes: 1
Reputation: 7898
It throws this error because the ID you declare is not the same as what is rendered.
<asp:Button runat="server" ID="btnSave" clientIdMode="static">
Add the clientIdMode="static"
attribute so your ID will render exactly as your declared it.
Upvotes: 3