Asp.Net 用后台代码给Server控件添加Client端JS方法

简单记录一下这个方法:Asp.Net 用后台代码给Server控件添加Client端JS方法

前台代码如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="Scripts/jquery-1.9.0.min.js"></script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:RadioButton ID="RadioButton1" runat="server" GroupName="EricSun" />
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <br />
        <asp:RadioButton ID="RadioButton2" runat="server" GroupName="EricSun" />
    </div>
    </form>
</body>
</html>

<script type="text/javascript">
    function RadioBtnOnClick(obj) {
        $("#<%=TextBox1.ClientID%>").attr('disabled', 'disabled');
    }
</script>

后台代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.RadioButton1.InputAttributes.Add("OnClick", "RadioBtnOnClick(this);");
            this.RadioButton2.InputAttributes.Add("OnClick", "RadioBtnOnClick(this);");
        }
    }
}

。。。。

posted @ 2013-03-13 08:17  Eric Sun  阅读(415)  评论(0编辑  收藏  举报