Shaggy
Shaggy

Reputation: 5830

Call Javascript function with parameters from C# Page .aspx

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 coming.
}

Upvotes: 0

Views: 9776

Answers (4)

Vasil Trifonov
Vasil Trifonov

Reputation: 1867

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

Upvotes: 1

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.

Upvotes: 1

Prashant16
Prashant16

Reputation: 1526

Try this

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

Upvotes: 1

Fishcake
Fishcake

Reputation: 10774

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

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

Upvotes: 2

Related Questions