网上找到一段 Asp.net可输入下拉框服务器控件的代码,如下:Imports System.ComponentModel
Imports System.Web.UI
Imports System.Web.UI.Design
Imports System.Web.UI.WebControlsNamespace CBDAspNet.WebControls.HTML ''' <summary>
''' 可输入的下拉框控件
''' </summary>
<ToolboxData("<{0}:TextBox runat=""server"" />")> _
Public Class TextBox
Inherits System.Web.UI.WebControls.TextBox Private _values As Hashtable Public _DropDownList As DropDownList Public Sub New()
_DropDownList = New DropDownList
_values = New Hashtable
End Sub Public Property Values() As Hashtable
Get
Return _values
End Get
Set(ByVal Value As Hashtable)
_values = Value
End Set
End Property Protected Overrides Sub Render(ByVal Output As System.Web.UI.HtmlTextWriter) Dim iWidth As Integer = MyBase.Width.Value
If iWidth = 0 Then
iWidth = 102
'MyBase.Width = Unit.Parse("102px")
End If Dim sWidth As Integer = iWidth + 16
Dim spanWidth As Integer = sWidth - 18 Output.Write("<div style=""POSITION:relative"">")
Output.Write("<span style=""MARGIN-LEFT:" & spanWidth & "px;OVERFLOW:hidden;WIDTH:18px"">") _DropDownList.Width = Unit.Parse(sWidth & "px")
_DropDownList.Style.Add("MARGIN-LEFT", "-" & spanWidth & "px")
_DropDownList.Attributes.Add("onchange", "this.parentNode.nextSibling.value=this.value") If _values.Count > 0 Then
For Each key As String In _values.Keys
Dim item As ListItem = New ListItem
item.Value = key
item.Text = _values(key)
_DropDownList.Items.Add(item)
Next
End If
''如果只有一个可选内容
If _DropDownList.Items.Count = 1 Then
Dim item As ListItem = New ListItem
item.Value = ""
item.Text = " "
_DropDownList.Items.Add(item)
_DropDownList.SelectedIndex = 1
End If
_DropDownList.RenderControl(Output) Output.Write("</span>") MyBase.Style.Clear()
MyBase.Width = Unit.Parse(iWidth & "px")
MyBase.Style.Add("left", "0px")
MyBase.Style.Add("POSITION", "absolute") MyBase.Render(Output) Output.Write("</div>") End Sub End ClassEnd Namespace
问题是:我怎么用在我的工程中,将它当做常规的服务器控件使用一样! (关键是这一步啊)
取值 绑定数据源等操作,怎么写代码!谢谢了 :) 在线等
Imports System.Web.UI
Imports System.Web.UI.Design
Imports System.Web.UI.WebControlsNamespace CBDAspNet.WebControls.HTML ''' <summary>
''' 可输入的下拉框控件
''' </summary>
<ToolboxData("<{0}:TextBox runat=""server"" />")> _
Public Class TextBox
Inherits System.Web.UI.WebControls.TextBox Private _values As Hashtable Public _DropDownList As DropDownList Public Sub New()
_DropDownList = New DropDownList
_values = New Hashtable
End Sub Public Property Values() As Hashtable
Get
Return _values
End Get
Set(ByVal Value As Hashtable)
_values = Value
End Set
End Property Protected Overrides Sub Render(ByVal Output As System.Web.UI.HtmlTextWriter) Dim iWidth As Integer = MyBase.Width.Value
If iWidth = 0 Then
iWidth = 102
'MyBase.Width = Unit.Parse("102px")
End If Dim sWidth As Integer = iWidth + 16
Dim spanWidth As Integer = sWidth - 18 Output.Write("<div style=""POSITION:relative"">")
Output.Write("<span style=""MARGIN-LEFT:" & spanWidth & "px;OVERFLOW:hidden;WIDTH:18px"">") _DropDownList.Width = Unit.Parse(sWidth & "px")
_DropDownList.Style.Add("MARGIN-LEFT", "-" & spanWidth & "px")
_DropDownList.Attributes.Add("onchange", "this.parentNode.nextSibling.value=this.value") If _values.Count > 0 Then
For Each key As String In _values.Keys
Dim item As ListItem = New ListItem
item.Value = key
item.Text = _values(key)
_DropDownList.Items.Add(item)
Next
End If
''如果只有一个可选内容
If _DropDownList.Items.Count = 1 Then
Dim item As ListItem = New ListItem
item.Value = ""
item.Text = " "
_DropDownList.Items.Add(item)
_DropDownList.SelectedIndex = 1
End If
_DropDownList.RenderControl(Output) Output.Write("</span>") MyBase.Style.Clear()
MyBase.Width = Unit.Parse(iWidth & "px")
MyBase.Style.Add("left", "0px")
MyBase.Style.Add("POSITION", "absolute") MyBase.Render(Output) Output.Write("</div>") End Sub End ClassEnd Namespace
问题是:我怎么用在我的工程中,将它当做常规的服务器控件使用一样! (关键是这一步啊)
取值 绑定数据源等操作,怎么写代码!谢谢了 :) 在线等
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货