小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

asp.net 給用戶(hù)控件增加事件和屬性

 悟靜 2012-12-15

意圖是這樣的:

管理頁(yè)面中,總會(huì)有一些新增啊,刪除啊,修改啊之類(lèi)的按鈕,為了方便,我想做個(gè)用戶(hù)控件;

這個(gè)用戶(hù)控件,里面有三個(gè)按鈕,新增、修改、刪除;這個(gè)用戶(hù)控件要在多個(gè)頁(yè)面上出現(xiàn),當(dāng)然,修改與刪除的對(duì)像也不相同;所以,這個(gè)用戶(hù)控件,在不同的頁(yè)面中,這三個(gè)按鈕會(huì)使用不同的事件;

(如果只是加事件,直接雙擊按鈕就可以加了)

用戶(hù)控件的代碼
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="UserLogin.ascx.cs" Inherits="UserLogin" %>
<html xmlns="http://www./1999/xhtml" >

<body>
<table border="1">
<tr>
<td>用戶(hù)名:</td>
<td><asp:TextBox ID="txt1" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>密  碼:</td>
<td><asp:TextBox ID="txt2" TextMode="password" runat="server"></asp:TextBox></td>
</tr>

<tr>
 <td><asp:Button ID="ButAdd" runat="server" Text="新增" OnClick="ButAdd_Click" />
<asp:Button ID="ButMod" runat="server" Text="修改" OnClick="ButMod_Click" />
<asp:Button ID="ButDel" runat="server" Text="刪除" OnClick="ButDel_Click" />
</td>
 <td><asp:LinkButton  ID="LinkButton1" Text="登陸" OnClick="MyOnClick" runat="server" ></asp:LinkButton></td>
</tr>
</table>
</body>
</html>

用戶(hù)控件C#中的代碼
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

//using System.Collections.Generic;
//using System.Text;

public partial class UserLogin : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    //添加屬性
    public string UserName
    {
        get { return txt1.Text; }
        set { txt1.Text = value; }
    }
    //添加屬性
    public string PassWord
    {
        get { return txt2.Text; }
        set { txt2.Text = value; }
    }

    //定義一個(gè)委托
    public delegate void userEvent(object sender, EventArgs arg);
    //

    //添加事件
    public event userEvent Login; //添加事件句柄
    protected void MyOnClick(object Sender, EventArgs e)
    {
        if (Login != null)
            Login(this, new EventArgs()); //激活Login事件
    }


    ////定義一個(gè)委托
    //public delegate void userEvent(object sender, EventArgs arg);
    ////

    //添加三個(gè)事件
    public event userEvent Add;
    public event userEvent Delete;
    public event userEvent Modify;
    //


    #region 控件中按鈕的事件
    protected void ButAdd_Click(object sender, EventArgs e)
    {
        if (this.Add != null)
            this.Add(this, e);
    }
    protected void ButDel_Click(object sender, EventArgs e)
    {
        if (this.Delete != null)
            this.Delete(this, e);
    }

    protected void ButMod_Click(object sender, EventArgs e)
    {
        if (this.Modify != null)
            this.Modify(this, e);
    }
    #endregion
}

上面用到了事件與委托;利用用戶(hù)控件中的按鈕來(lái)觸發(fā)自己定義的事件;

那么,在aspx頁(yè)面中引用這個(gè)控件時(shí):
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Src="UserLogin.ascx" TagName="UserLogin" TagPrefix="uc1" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www./TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www./1999/xhtml" >
<head runat="server">
    <title>無(wú)標(biāo)題頁(yè)</title>
</head>
<body style="margin-left:0px">
    <form id="form1" runat="server">
    <div>
        <uc1:UserLogin ID="UserLogin1" OnAdd="ButAdd_Click" OnDelete="ButDel_Click" OnModify="ButMod_Click" OnLogin="OnLoginPress" runat="server" />
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox></div>
    </form>
</body>
</html>

在這個(gè)aspx頁(yè)面的C#文件中,有這樣一些事件
    protected void OnLoginPress(Object Render, EventArgs e)
    {
        TextBox1.Text = UserLogin1.UserName;
        TextBox2.Text = UserLogin1.PassWord;
    }

    protected void ButAdd_Click(object sender, EventArgs e)
    {

        //this.Response.Write(sender.GetType().ToString());
        TextBox1.Text = "添加了數(shù)據(jù)!";
    }
    protected void ButDel_Click(object sender, EventArgs e)
    {
        TextBox1.Text = "我已刪除";
        //this.Response.Write("刪除");
    }
    protected void ButMod_Click(object sender, EventArgs e)
    {
        TextBox1.Text = "修改了數(shù)據(jù)!";
        //this.Response.Write("修改");
    }

那么,在不同的aspx文檔中,我就可以寫(xiě)不同的事件了;很簡(jiǎn)單吧;

這樣,我們還可以在用戶(hù)控件中判斷當(dāng)前登錄用戶(hù)的權(quán)限,例如,如果沒(méi)有刪除權(quán)限,刪除按鈕就不出現(xiàn);

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶(hù)發(fā)布,不代表本站觀(guān)點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶(hù) 評(píng)論公約

    類(lèi)似文章 更多