服务器控件OnClientClick调用js

using System;
using System.Data;
using System.Globalization;
using System.Threading;
using System.Web.UI.WebControls;
 
namespace CSharp
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("ID", typeof(int));
                for (int i = 1; i < 6; i++)
                {
                    dt.Rows.Add(i);
                }
                Repeater1.DataSource = dt;
                Repeater1.DataBind();
            }
        }
        protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "MyButton")
            {
                (e.CommandSource as Button).OnClientClick = "SayMe('"+e.CommandArgument+"')";
            }
        }
    }
}

<!DOCTYPE html>
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>语言测试</title>
   <script>
       function SayMe(obj) {
           alert(obj);
       }
   </script>
</head>
<body>
    <form id="form1" runat="server">
        <asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand">
            <ItemTemplate>
                <asp:Button ID="Button1" runat="server" Text="点击" CommandArgument='<%#Eval("ID") %>' CommandName="MyButton" />
            </ItemTemplate>
        </asp:Repeater>
    </form>
</body>
</html>

你可能感兴趣的:(服务器控件OnClientClick调用js)