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

c# - call a Javascript function from ascx page code behind - Stack Overflow

programmeradmin0浏览0评论

I am using the below client script to pass a value to a javascript function. It works fine in an aspx page but in an ascx page it, is not working. Please help me to solve this.

ScriptManager.RegisterStartupScript(this, this.GetType(), "tabmvng", "<script language='javascript'>SetActiveTab(3); </script>", false);

I am using the below client script to pass a value to a javascript function. It works fine in an aspx page but in an ascx page it, is not working. Please help me to solve this.

ScriptManager.RegisterStartupScript(this, this.GetType(), "tabmvng", "<script language='javascript'>SetActiveTab(3); </script>", false);
Share Improve this question edited Apr 2, 2013 at 11:53 Youn Elan 2,4523 gold badges26 silver badges33 bronze badges asked Apr 2, 2013 at 11:40 user2176150user2176150 3033 gold badges12 silver badges21 bronze badges 1
  • It helps a lot if you tell us what is not working, rather than expecting people to guess it. – nunespascal Commented Apr 2, 2013 at 11:45
Add a ment  | 

5 Answers 5

Reset to default 5

Try this, It is all because UserControl, you are no longer dealing with a Page.

 ScriptManager.RegisterClientScriptBlock(this.Page, typeof(UpdatePanel), UniqueID, "myFunction('" + parameter + "');", true);

try the following

<script type='text/javascript'>
function SetActiveTab(a){
alert(a);
}
</script>

ScriptManager.RegisterStartupScript(this, this.GetType(), "tabmvng", "SetActiveTab(3);", true);
Control Caller = this; //user control
string MyScript= "SetActiveTab(3);";
ScriptManager.RegisterStartupScript(Caller, typeof(Caller), "Script Name", MyScript), true);

Get through Script manager having trouble adding scripts to the Page object from that user control use a reference to the calling user control. In addition, it will wrap the script for you so no need to add the script tag.

EDIT NOTE: I assume this function exists in your script somewhere: SetActiveTab(3);

ScriptManager.RegisterStartupScript(this.Page, typeof(System.Web.UI.Page), "javascript", "YourScript", true);

I had the same issue of the Javascript not being in the html output. Turns out if you are doing this from ascx (control) you have to pass reference to the control, in example Me.

ScriptManager.RegisterStartupScript(Me, GetType(Page), Guid.NewGuid().ToString(), jscript, True)

After that, all started to work! thanks Mark Schultheiss!

发布评论

评论列表(0)

  1. 暂无评论