最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Call Javascript function with parameters from C# Page .aspx - Stack Overflow

programmeradmin1浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论