Hi All,
I made a custom server control that inherit DataGrid to expand some functions (include onMouseover highlight , onClick then highlight the selected row..).
Now I got some problems. When I build the control and use it in my web form. It does not support all the attributes of datagrid in VS.NET Web form html editor.
Although it does not support, but it can work when I manually add the codes in the editor just like below
<cc1:powergrid id="PowerGrid1" runat="server" Back_Color="#FFFFC0" MouseOver_Color="Green" SelectColor="#66CCCC" BackColor="#FFFFC0" SelectedIndex="0">
<Columns>
</Columns>
..........................................
</cc1:powergrid>
Here is my sourcecode of the control :I hope someone could resolve my problems.
Imports System.ComponentModel
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Drawing
Imports System.Drawing.Color
<DefaultProperty("Text"), ToolboxData("<{0}:PowerGrid runat=server></{0}:PowerGrid>")> _
Public Class PowerGrid
Inherits System.Web.UI.WebControls.DataGrid
Private _text As String
Private _SelectColor As String
Private _MouseOverColor As String
Private m_LastE As System.Web.UI.WebControls.DataGridItemEventArgs
Private m_intRecordsPerRow As Integer = 1
Private s_index As Integer
Sub New()
End Sub
<Bindable(True), Category("Appearance"), DefaultValue("")> Property [Text]() As String
Get
Return _text
End Get
Set(ByVal Value As String)
_text = Value
End Set
End Property
<Bindable(True), Category("Appearance"), DefaultValue("")> Property [SelectColor]() As String
Get
Return _SelectColor
End Get
Set(ByVal Value As String)
_SelectColor = Value
End Set
End Property
<Bindable(True), Category("Appearance"), DefaultValue("")> Property [MouseOver_Color]() As String
Get
Return _MouseOverColor
End Get
Set(ByVal Value As String)
_MouseOverColor = Value
End Set
End Property
Public Overrides Property SelectedIndex() As Integer
Get
Return s_index
End Get
Set(ByVal Value As Integer)
s_index = Value
End Set
End Property
Protected Overrides Sub Render(ByVal output As System.Web.UI.HtmlTextWriter)
MyBase.Render(output)
Me.Page.RegisterHiddenField("SelectItem", "")
End Sub
Private Sub WebCustomControl1_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles MyBase.ItemCreated
Select Case e.Item.ItemType
Case ListItemType.SelectedItem, ListItemType.Item, ListItemType.AlternatingItem
e.Item.Attributes.Add("onmouseup", "this.style.backgroundColor='" + _SelectColor + "'")
e.Item.Attributes.Add("onclick", "javascript:document.Form1.SelectItem.value='" & e.Item.ItemIndex & "';document.Form1.submit();")
If e.Item.ItemIndex = Me.SelectedIndex Then
e.Item.Attributes.Remove("style")
e.Item.Attributes.Add("style", "background-color:" + _SelectColor + ";")
Else
e.Item.Attributes.Add("onmouseover", "this.style.backgroundColor='" + _MouseOverColor + "';this.style.cursor='hand';")
If Me.BackColor.Name = "0" Then
e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor='#000000';")
Else
Dim color As String = Me.BackColor.Name
If color.Substring(0, 2) = "ff" Then
color = "#" + color.Substring(2, 6)
End If
e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor='" + color + "';")
End If
End If
End Select
End Sub
Protected Overrides Sub Oninit(ByVal e As System.EventArgs)
If Me.Page.Request("SelectItem") <> "" Then
s_index = CInt(Me.Page.Request("SelectItem"))
Else
s_index = -1
End If
End Sub
End Class