且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从codebehind函数调用javascript函数

更新时间:2023-11-24 11:54:52

在代码隐藏页面中添加以下代码:

Add following code in code-behind page:
public partial class Default : System.Web.UI.Page
{
	protected void Page_Load(object sender, EventArgs e)
	{
		LoadMessages();
	}
	
	public void LoadMessages()
	{
		DataTable dt = m_Data.GetMessages(m_UserID_Login);
		if (dt.Rows.Count > 0)
		{
		  foreach (DataRow row in dt.Rows)
		  {
			Page.ClientScript.RegisterStartupScript(this.GetType(), "CallMyFunction", "sendPrivateMessagetest();", true);
		  }
		}
	}
}



ASPX代码:


ASPX code:

<head runat="server">
    <title></title>
    <script type="text/javascript">
        function sendPrivateMessagetest() {
            alert('message');
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    </form>
</body>
</html>



注意:确保在页面中注入了JavaScript函数。


Note: Make sure that the JavaScript function is injected in your page.