views:

96

answers:

3

i have to run following javascript through one of my method. But its not running Whats wrong with the code.

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);
        }
}
+1  A: 

Your script tag is not properly closed.

Change

cstext2.Append("script>");

to

cstext2.Append("</script>");
rahul
possibly better not to even add the script tags yourself... just pass true as the last argument: cs.RegisterClientScriptBlock(cstype, csname2, cstext2.ToString(), true);
Paul
A: 

On top of what adamantium said, your JS looks a bit strange. You seem to declare and set the count variable twice - did you mean to do this.

Following that, best thing to do, render the page then view source. is your JS getting rendered to the page? try and stick an alert in there... is it firing?

Paul
A: 
>   cstext2.Append("var count =
> document.getElementById('ctl00_ContentPlaceHolder1_HiddenField2');");

I would use the ClientID property here. HiddenField2.ClientID

ram