0

我必须通过我的一种方法运行以下 javascript。但它没有运行代码有什么问题。

private void fillGrid1()
{
        GridView1.DataSource = myDocCenter.GetDsWaitingForMe(Session["UserID"].ToString());
        HiddenField1.Value = { myDocCenter.GetDsWaitingForMe(Session["UserID"].ToString()).Tables[0].Rows.Count).ToString();
        GridView1.DataBind();

        String csname1 = "PopupScript1";
        String csname2 = "ButtonClickScript1";
        Type cstype = this.GetType();

        // Get a ClientScriptManager reference from the Page class.
        ClientScriptManager cs = Page.ClientScript;


        // Check to see if the client script is already registered.
        if (!cs.IsClientScriptBlockRegistered(cstype, csname2))
        {
            StringBuilder cstext2 = new StringBuilder();
            cstext2.Append("<script type=\"text/javascript\"> ");

            // You can  add JavaScript by using "cstext2.Append()".

            cstext2.Append("var count = document.getElementById('ctl00_ContentPlaceHolder1_HiddenField2');");
            cstext2.Append("var count = '100';");
            cstext2.Append("document.getElementById('sp2').innerHTML = count;");
            cstext2.Append("script>");

            cs.RegisterClientScriptBlock(cstype, csname2, cstext2.ToString(), false);
        }
}
4

4 回答 4

1

您的脚本标签未正确关闭。

改变

cstext2.Append("script>");

cstext2.Append("</script>");
于 2009-12-14T13:27:51.327 回答
0

<form>RegisterClientScriptBlock 在标签打开后立即发出脚本。浏览器也会在标签打开后立即执行此脚本,但此时尚未处理引用的元素 - 浏览器无法找到它们。

RegisterStartupScript<form>方法在标签结束之前发出脚本。几乎所有页面元素都由浏览器在这个地方处理,getElementById 可以找到一些东西。

有关详细信息,请参阅http://jakub-linhart.blogspot.com/2012/03/script-registration-labyrinth-in-aspnet.html 。

于 2012-03-11T21:19:53.833 回答
0

除了adamantium所说的,你的JS看起来有点奇怪。您似乎两次声明并设置了 count 变量-您是要这样做吗?

之后,最好的办法是渲染页面然后查看源代码。你的 JS 被渲染到页面了吗?试着在那里发出警报……它开火了吗?

于 2009-12-14T13:40:28.790 回答
0
>   cstext2.Append("var count =
> document.getElementById('ctl00_ContentPlaceHolder1_HiddenField2');");

我会在这里使用 ClientID 属性。HiddenField2.ClientID

于 2009-12-14T13:47:52.367 回答