C# 脚本块寄存器
C# Script Block Register
任何人都可以帮助我,因为我试图先注册一个脚本并使用 C# 将一个名为 "DoClick()" 的函数绑定到按钮中,但在运行时发生了一些错误。请在下面查看我的代码。这样当按钮被点击时,他们调用函数“DoClick()。谢谢大家
public void regiterAdsScript(int loc)
{
string adsLink = ads_link(loc);
// Define the name and type of the client script on the page.
String csName = "ButtonClickScript";
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, csName))
{
StringBuilder csText = new StringBuilder();
csText.Append("<script type=\"text/javascript\"> \n");
csText.Append("function DoClick() { <script type='text/javascript' src='//abcd.site?id=123'></script> } \n");
csText.Append("</script>");
cs.RegisterClientScriptBlock(csType, csName, csText.ToString());
Button1.Attributes.Add("onClick", "return DoClick()");
}
}
你的脚本注册码应该是这样的
Button1.Attributes.Add("onClick", "javascript:DoClick();");
此外,您的脚本看起来不对。无论你想让你的脚本做什么,都应该在函数声明之后。如果你想发出警报,它应该是这样的
csText.Append("function DoClick() { alert('MK'); } \n");
您还需要在调用 Button1_Click 之前调用函数 regiterAdsScript()。我在 Page_load 本身中调用了它。以下是您的示例程序:
protected void Page_Load(object sender, EventArgs e)
{
regiterAdsScript();
}
protected void Button1_Click(object sender, EventArgs e)
{
//functionality to be implemented
}
public void regiterAdsScript()
{
string adsLink = ads_link(loc);
// Define the name and type of the client script on the page.
String csName = "ButtonClickScript";
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, csName))
{
StringBuilder csText = new StringBuilder();
csText.Append("<script type=\"text/javascript\"> \n");
csText.Append("function DoClick() { alert('MK'); } \n");
csText.Append("</script>");
cs.RegisterClientScriptBlock(csType, csName, csText.ToString());
Button1.Attributes.Add("onClick", "javascript:DoClick();");
}
}
任何人都可以帮助我,因为我试图先注册一个脚本并使用 C# 将一个名为 "DoClick()" 的函数绑定到按钮中,但在运行时发生了一些错误。请在下面查看我的代码。这样当按钮被点击时,他们调用函数“DoClick()。谢谢大家
public void regiterAdsScript(int loc)
{
string adsLink = ads_link(loc);
// Define the name and type of the client script on the page.
String csName = "ButtonClickScript";
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, csName))
{
StringBuilder csText = new StringBuilder();
csText.Append("<script type=\"text/javascript\"> \n");
csText.Append("function DoClick() { <script type='text/javascript' src='//abcd.site?id=123'></script> } \n");
csText.Append("</script>");
cs.RegisterClientScriptBlock(csType, csName, csText.ToString());
Button1.Attributes.Add("onClick", "return DoClick()");
}
}
你的脚本注册码应该是这样的
Button1.Attributes.Add("onClick", "javascript:DoClick();");
此外,您的脚本看起来不对。无论你想让你的脚本做什么,都应该在函数声明之后。如果你想发出警报,它应该是这样的
csText.Append("function DoClick() { alert('MK'); } \n");
您还需要在调用 Button1_Click 之前调用函数 regiterAdsScript()。我在 Page_load 本身中调用了它。以下是您的示例程序:
protected void Page_Load(object sender, EventArgs e)
{
regiterAdsScript();
}
protected void Button1_Click(object sender, EventArgs e)
{
//functionality to be implemented
}
public void regiterAdsScript()
{
string adsLink = ads_link(loc);
// Define the name and type of the client script on the page.
String csName = "ButtonClickScript";
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, csName))
{
StringBuilder csText = new StringBuilder();
csText.Append("<script type=\"text/javascript\"> \n");
csText.Append("function DoClick() { alert('MK'); } \n");
csText.Append("</script>");
cs.RegisterClientScriptBlock(csType, csName, csText.ToString());
Button1.Attributes.Add("onClick", "javascript:DoClick();");
}
}