0

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

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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);
   }

Comments

0

To get a server-side control via Javascript, use the below

document.getElementById('<%=btnSave.ClientID%>').click();

1 Comment

Tried following but same error, ScriptManager.RegisterStartupScript(this, this.GetType(), "scr", "setTimeout(function(){document.getElementById('<%=btnSave.ClientID%>').click(); return false;}, 180000);", true);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.