0

我在我的网络控件上关注按钮,

<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"  />

我正在尝试使用后面代码中的 JavaScript 单击此按钮。像,

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

但它会引发错误Error: TypeError: document.getElementById(...) is null

4

3 回答 3

3

它会引发此错误,因为您声明的 ID 与呈现的 ID 不同。

<asp:Button runat="server" ID="btnSave" clientIdMode="static">

添加clientIdMode="static"属性,以便您的 ID 将完全按照您声明的方式呈现。

于 2013-03-19T15:31:15.670 回答
1

要通过 获取服务器端控件Javascript,请使用以下命令

document.getElementById('<%=btnSave.ClientID%>').click();
于 2013-03-19T15:31:20.560 回答
1

最有可能返回 null 是因为 btnSave 控件的 id 在客户端呈现时已更改,除非您使用CliendIDMode="static"

为了使您的代码正常工作,您需要将其更改为:

  if (!IsPostBack)
   {
        ScriptManager.RegisterStartupScript(this, this.GetType(), "scr", "setTimeout(function(){document.getElementById('"+btnSave.ClientID+"').click(); return false;}, 180000);", true);
   }
于 2013-03-19T15:32:19.053 回答