小弟刚从Winform转过来,对Web实在是不是很了解,望高手多多指教~在WebForm中,小弟做了一个自定义控件,该控件继承TextBox控件,然后如下代码
 
public override void RenderBeginTag(HtmlTextWriter writer)
        {
            writer.RenderBeginTag(HtmlTextWriterTag.Table);
            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write(this.Describe);
            writer.RenderEndTag();
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            base.RenderBeginTag(writer);
            writer.RenderEndTag();
            if (!this.IsForeignKey)
            {
                writer.RenderEndTag();
            }
        }        public override void RenderEndTag(HtmlTextWriter writer)
        {
            base.RenderEndTag(writer);
            if (this.IsForeignKey)
            {
                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                this.button = new HtmlInputButton();
                this.button.Disabled = !this.Enabled;
                this.button.Value = "...";
                if (!base.DesignMode && this.AutoPostBack)
                {
                    this.button.Attributes.Add("onbtnclick", "__doPostBack('" + this.ClientID + "','button')");
                    this.Page.ClientScript.GetPostBackClientHyperlink(this.button, this.ClientID + "$button");
                }
                this.button.RenderControl(writer);
                writer.RenderEndTag();
                writer.RenderEndTag();
                writer.RenderEndTag();
            }
            else
            {
                writer.RenderEndTag();
            }
        }
主要是第二个事件,这个button是HtmlInputButton,目的是在TextBox后面添加一个Button,然后当在界面用这个空间时,点击Button会触发一个事件,就像在界面上放上一个TextBox然后后面再放一个HtmlInputButton一样,但是问题是,我现在把这俩控件集成到一起后,如何把新增的Button时间的onclick事件抛出来?当我在界面上点Button的onbtnclick时,可以触发JS里面相应的事件??该如何做呢??

解决方案 »

  1.   

    你这个要做组合控件,不是单单继承textbox改写。组合控件里子控件的事件都委托到CreatChildControls方法里执行(不知道中文这么说对不对-。-),我这有个很早前做的,很烂的组合控件一部分代码。你可以看看,MSDN里有很详细的例子可以从那学
    Imports Microsoft.VisualBasic
    Imports Rabo.Services.PortalLibrary
    Imports System.Data
    Imports System
    Imports System.Collections.Generic
    Imports System.ComponentModel
    Imports System.Text
    Imports System.Web
    Imports System.Web.UI
    Imports System.Web.UI.Control
    Imports System.Web.UI.WebControls
    Imports System.Web.UI.WebControls.WebControlPublic Class RaboFileUploadListView
        Inherits WebControl
        Implements INamingContainer#Region "Properties"    Public Property BasePath() As String
            Get
                Return ViewState("BasePath")
            End Get
            Set(ByVal value As String)
                ViewState("BasePath") = value
            End Set
        End Property    Public Property PlaceFiles() As String
            Get
                Return ViewState("PlaceFiles")
            End Get
            Set(ByVal value As String)
                ViewState("PlaceFiles") = value
            End Set
        End Property    Public Property HttpBasePath() As String
            Get
                Return ViewState("httpBasePath")
            End Get
            Set(ByVal value As String)
                ViewState("httpBasePath") = value
            End Set
        End Property    Public Property PlaceFilesDownload() As String
            Get
                Return ViewState("PlaceFilesDownload")
            End Get
            Set(ByVal value As String)
                ViewState("PlaceFilesDownload") = value
            End Set
        End Property#End Region#Region "Methods"
        Protected Overrides Sub CreateChildControls()
            Dim fu As New FileUpload        Dim bt As New Rabo.Services.PortalControls.LinkButton
            bt.Text = "Upload"
            bt.CssClass = "BLUlinkButton"        Dim lb As New ListBox
            lb.Width = "255"
            lb.Height = "50"
            lb.SelectionMode = ListSelectionMode.Multiple        Dim bt2 As New Rabo.Services.PortalControls.LinkButton
            bt2.Text = "Remove"
            bt2.CssClass = "BLUlinkButton"
            bt2.ButtonStyle = Services.PortalControls.LinkButton.enuButtonStyle.RenderBothPipes        Dim bt3 As New Rabo.Services.PortalControls.LinkButton
            bt3.Text = "Download"
            bt3.CssClass = "BLUlinkButton"
            bt3.ButtonStyle = Services.PortalControls.LinkButton.enuButtonStyle.RenderEndPipe        Controls.Add(fu)
            Controls.Add(bt)
            AddHandler bt.Click, AddressOf Me.ButtonClicked
            Controls.Add(New LiteralControl("<BR>"))        Controls.Add(lb)
            Me.LoadListBoxData()
            Controls.Add(New LiteralControl("<BR>"))        Controls.Add(bt2)
            AddHandler bt2.Click, AddressOf Me.ButtonClicked2
            Controls.Add(bt3)
            AddHandler bt3.Click, AddressOf Me.ButtonClicked3
        End Sub    Private Sub LoadListBoxData()
            DisplayUploadedFiles(Me.BasePath, Me.PlaceFiles, CType(Me.Controls("3"), ListBox))
        End Sub    Private Sub ButtonClicked(ByVal sender As Object, ByVal e As EventArgs)
            OnCheck(Me.BasePath, Me.PlaceFiles, CType(Me.Controls("0"), FileUpload))
            DisplayUploadedFiles(Me.BasePath, Me.PlaceFiles, CType(Me.Controls("3"), ListBox))
        End Sub    Private Sub ButtonClicked2(ByVal sender As Object, ByVal e As EventArgs)
            RemoveUploadedFiles(Me.BasePath, Me.PlaceFiles, CType(Me.Controls("3"), ListBox))
            DisplayUploadedFiles(Me.BasePath, Me.PlaceFiles, CType(Me.Controls("3"), ListBox))
        End Sub    Private Sub ButtonClicked3(ByVal sender As Object, ByVal e As EventArgs)
            DownloadUploadedFiles(Me.BasePath, Me.PlaceFiles, Me.HttpBasePath, Me.PlaceFilesDownload, CType(Me.Controls("3"), ListBox))
            DisplayUploadedFiles(Me.BasePath, Me.PlaceFiles, CType(Me.Controls("3"), ListBox))
        End Sub    Private Sub OnCheck(ByVal BasePath As String, ByVal PlaceFiles As String, ByRef ChildFileUpload As FileUpload)
            FolderExists(BasePath)
            FolderExists(BasePath & PlaceFiles)
            If ChildFileUpload.FileName IsNot Nothing AndAlso ChildFileUpload.FileName <> "" Then
                ChildFileUpload.SaveAs(BasePath & PlaceFiles & ChildFileUpload.FileName)
                ChildFileUpload.Dispose()
            End If
        End Sub    Private Sub FolderExists(ByVal FolderPath As String)
            Dim folder As New System.IO.DirectoryInfo(FolderPath)
            If Not folder.Exists() Then
                folder.Create()
            End If
        End Sub    Private Function FileExists(ByVal FolderPath As String) As Boolean
            Dim folder As New System.IO.DirectoryInfo(FolderPath)
            If folder.Exists Then
                If folder.GetFiles() Is Nothing Then
                    Return False
                Else
                    Return True
                End If
            Else
                Return False
            End If
        End Function    Private Sub DisplayUploadedFiles(ByVal BasePath As String, ByVal PlaceFiles As String, ByRef ChildListBox As ListBox)
            Dim folderPath As String = BasePath & PlaceFiles
            Dim folder As New System.IO.DirectoryInfo(folderPath)
            If FileExists(BasePath & PlaceFiles) = True Then
                ChildListBox.DataSource = folder.GetFiles()
                ChildListBox.DataBind()
            Else
                ChildListBox.Items.Clear()
            End If
        End Sub    Private Sub RemoveUploadedFiles(ByVal BasePath As String, ByVal PlaceFiles As String, ByRef ChildListBox As ListBox)
            Dim fileText As System.IO.FileInfo
            Dim folderPath As String = BasePath & PlaceFiles
            Dim folder As New System.IO.DirectoryInfo(folderPath)        If FileExists(Me.BasePath & Me.PlaceFiles) = True Then
                For Each fileText In folder.GetFiles
                    For Each item As ListItem In ChildListBox.Items
                        If item.Selected AndAlso item.Value = fileText.Name Then
                            fileText.Delete()
                        End If
                    Next
                Next
            End If
        End Sub    Private Sub DownloadUploadedFiles(ByVal BasePath As String, ByVal PlaceFiles As String, ByVal HttpBasePath As String, ByVal PlaceFilesDownload As String, ByRef ChildListBox As ListBox)
            Dim fileText As System.IO.FileInfo
            Dim folderPath As String = BasePath & PlaceFiles
            Dim httpFolderPath As String = HttpBasePath & PlaceFilesDownload
            Dim folder As New System.IO.DirectoryInfo(folderPath)        If FileExists(BasePath & PlaceFiles) = True Then
                For Each fileText In folder.GetFiles
                    If ChildListBox.SelectedValue <> "" AndAlso ChildListBox.SelectedValue = fileText.Name Then
                        Dim type As System.Type
                        Dim Downloadlink As String = Replace(fileText.FullName.ToString, folderPath, httpFolderPath)
                        type = Me.Page.GetType
                        Me.Page.ClientScript.RegisterClientScriptBlock(type, "Onload", javscriptStringPopupWindow(Downloadlink))
                    End If
                Next
            End If
        End Sub    Private Function javscriptStringPopupWindow(ByVal url As String) As String
            Dim strScript As String = Nothing
            strScript = strScript + "<script language=JavaScript>"
            strScript = strScript + "window.open('" & url & "');"
            strScript = strScript + "</script>"        Return strScript
        End Function#End Region
    End Class
      

  2.   

    using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;
    namespace arrayApp{    public partial class Form1 : Form    {        Button[] btns; //声明一个Button类型的数组        public Form1()        {            InitializeComponent();        }
            private void Form1_Load(object sender, EventArgs e)        {           btns= new Button[9];           ShowButtonArray();        }        private void ShowButtonArray()        {            //C#实现类似vb6.0的控件数组            for (int i = 0; i < 9; i++)            {                btns[i]=new Button();//创建按钮实例                btns[i].Location = new System.Drawing.Point(80 + 50 * (i % 3), 80 + 50 * (i / 3));                btns[i].Size = new System.Drawing.Size(48, 48);                btns[i].Text = i.ToString();                btns[i].Click += new System.EventHandler(this.btns_Click);//统一事件处理                this.Controls.Add(btns[i]);//在窗体上呈现按钮控件            }        }        //定义事件        private void btns_Click(object sender, System.EventArgs e)        {            MessageBox.Show(((Button)sender).Text + " was clicked.");        }    }}