admin管理员组

文章数量:1336410

I have RegisterClientScriptBlock which is written inside page load of .aspx file protected void Page_Load(object sender, EventArgs e)

The Script actually gets ID From URL and then Pass it to openticketPageLoad() function of javascript.

But it is not getting into openticketPageLoad() function. But .aspx page is loading.

openTickets.aspx.cs

public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "openTicketsScript", "<script type=\'type/javascript\'>$(document).ready(function(){openticketPageLoad(" + Request.QueryString["ID"].ToString() + ");});</script>".ToString(), true);
}
}

Inside my javascript file

function openticketPageLoad(b)
{
alert(b); //No alert window ing.
}

I have RegisterClientScriptBlock which is written inside page load of .aspx file protected void Page_Load(object sender, EventArgs e)

The Script actually gets ID From URL and then Pass it to openticketPageLoad() function of javascript.

But it is not getting into openticketPageLoad() function. But .aspx page is loading.

openTickets.aspx.cs

public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "openTicketsScript", "<script type=\'type/javascript\'>$(document).ready(function(){openticketPageLoad(" + Request.QueryString["ID"].ToString() + ");});</script>".ToString(), true);
}
}

Inside my javascript file

function openticketPageLoad(b)
{
alert(b); //No alert window ing.
}
Share Improve this question edited Feb 7, 2013 at 11:02 Shaggy asked Feb 7, 2013 at 10:20 ShaggyShaggy 5,83030 gold badges106 silver badges170 bronze badges 4
  • Are you using an updatepanel? – Destrictor Commented Feb 7, 2013 at 11:14
  • No. Simple aspx file inherited from Master Page – Shaggy Commented Feb 7, 2013 at 11:17
  • Hmm I'd still suggest removing the $(document).ready() call, perhaps it's being executed after the initial $(document).ready call, sometimes resulting in it not being called at all. Another possibility is that you're overwriting $(document).ready('different function') somewhere. – Destrictor Commented Feb 7, 2013 at 11:21
  • @Destrictor removed $(document).ready() and check overwriting of function but still same issue. – Shaggy Commented Feb 7, 2013 at 11:44
Add a ment  | 

4 Answers 4

Reset to default 2

See the documentation here: http://msdn.microsoft./en-us/library/system.web.ui.clientscriptmanager.registerclientscriptblock.aspx

The last parameter is a boolean that specifies whether ASP should generate Script tags. As you already specify them I expect if you look at your source you are generating nested script tags.

Can you try the following code:

public partial class Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ClientScript.RegisterClientScriptBlock(this.GetType(), 
                "openTicketsScript", string.Format("openticketPageLoad({0});", Request.QueryString["ID"]), true);
    }
}

Try this

Page.ClientScript.RegisterStartupScript(this.GetType(), "openTicketsScript", "<script type=\'type/javascript\'>$(document).ready(function(){openticketPageLoad(" + Request.QueryString["ID"].ToString() + ");});</script>".ToString(), true);

Perhaps you could do is assign the call to your javascript function direct in the load event of the body of the page. To assign the load function of the body from a content page can do the following:

HtmlGenericControl body = this.Master.FindControl("body") as HtmlGenericControl;
                body.Attributes.Add("onLoad", "openticketPageLoad(" + Request.QueryString["ID"].ToString() + ");");

And in the master page add the runat="server" to the body element:

<body id="body" runat="server">

I hope this helps.

本文标签: Call Javascript function with parameters from C Page aspxStack Overflow