使用C#Page.aspx中的参数调用Javascript函数

使用C#Page.aspx中的参数调用Javascript函数,c#,javascript,asp.net,C#,Javascript,Asp.net,我有RegisterClientScriptBlock,它写在.aspx文件的页面加载中受保护的无效页面加载(对象发送方,事件参数e) 该脚本实际上从URL获取ID,然后将其传递给javascript的openticketPageLoad()函数 但它没有进入openticketPageLoad()函数。但是.aspx页面正在加载。 openTickets.aspx.cs public partial class Default : System.Web.UI.Page { protected

我有RegisterClientScriptBlock,它写在.aspx文件的页面加载中
受保护的无效页面加载(对象发送方,事件参数e)

该脚本实际上从URL获取ID,然后将其传递给javascript的openticketPageLoad()函数

但它没有进入openticketPageLoad()函数。但是.aspx页面正在加载。

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

请参阅此处的文档:


最后一个参数是一个布尔值,用于指定ASP.net是否应生成脚本标记。由于您已经指定了它们,我希望如果您查看源代码,您将生成嵌套的脚本标记。

请参阅此处的文档:

最后一个参数是一个布尔值,用于指定ASP.net是否应生成脚本标记。由于您已经指定了它们,我希望如果您查看源代码,您将生成嵌套的脚本标记。

试试这个

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

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

也许您可以在页面主体的加载事件中直接将调用分配给javascript函数。要从内容页分配正文的加载功能,可以执行以下操作:

HtmlGenericControl body = this.Master.FindControl("body") as HtmlGenericControl;
                body.Attributes.Add("onLoad", "openticketPageLoad(" + Request.QueryString["ID"].ToString() + ");");
在母版页中,将runat=“server”添加到body元素:

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


我希望这会有所帮助。

也许您可以在页面主体的加载事件中直接将调用分配给javascript函数。要从内容页分配正文的加载功能,可以执行以下操作:

HtmlGenericControl body = this.Master.FindControl("body") as HtmlGenericControl;
                body.Attributes.Add("onLoad", "openticketPageLoad(" + Request.QueryString["ID"].ToString() + ");");
在母版页中,将runat=“server”添加到body元素:

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


我希望这能有所帮助。

您能尝试以下代码吗:

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

您可以尝试以下代码:

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

试过了,但似乎不起作用。它并没有进入我的javascript函数中,我试过了,但似乎不起作用。它没有进入我的javascript函数你能用Firebug(或类似的东西)检查源代码并发布输出吗?Used Firebug..在我的opentickets.aspx页面的head标记内,我找不到以下脚本
$(document).ready(函数(){openticketPageLoad('2');})
你能用Firebug(或类似的东西)检查源代码并发布输出吗?Used Firebug..在我的opentickets.aspx页面的head标记内,我找不到以下脚本
$(document).ready(function(){openticketPageLoad('2');})
您使用的是updatepanel吗?不是。从主页继承的简单aspx文件嗯,我仍然建议删除
$(document).ready()
调用,可能是在初始
$(document).ready
调用之后执行的,有时会导致根本不调用它。另一种可能是您正在某个地方覆盖
$(文档).ready('different function')
。@Destrictor删除了
$(文档).ready()
,并检查函数的覆盖情况,但仍然存在相同的问题。您是否使用了updatepanel?否。从母版页继承的简单aspx文件嗯,我仍然建议删除
$(文档).ready()。另一种可能是您正在覆盖某个地方的
$(document).ready('different function')
。@Destrictor删除了
$(document).ready()
,并检查函数的覆盖情况,但仍然存在相同的问题。