Hi Justin,
I am not sure if you meant Datagrid1 when requesting code. I included that example as a contrast. Datagrid1 is the same datagrid, but not contained in a user control (and the postback works perfectly).
So I have posted the code for the dgCategory user control, and the instantiation of this control from from the main aspx page.
(Sorry - I changed some of the object names in previous posts to make it simpler, so hopefully the code is clear)
Thanks again for taking the time to look at this
Cheers
Chris
USER CONTROL
Public Class spec_datagrid
Inherits System.Web.UI.UserControl
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Protected WithEvents dg1Header As System.Web.UI.WebControls.Label
Protected WithEvents dgCategory As System.Web.UI.WebControls.DataGrid
'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Public Property ViewType() As Integer
Get
ViewType = pViewType
End Get
Set(ByVal Value As Integer)
pViewType = ViewType
End Set
End Property
Public Property SelectedID() As Integer
Get
SelectedID = pSelectedID
End Get
Set(ByVal Value As Integer)
pSelectedID = Value
End Set
End Property
Public Property Tier() As Integer
Get
Tier = pTier
End Get
Set(ByVal Value As Integer)
pTier = Value
End Set
End Property
Public Property HasChild() As Boolean
Get
HasChild = pHasChild
End Get
Set(ByVal Value As Boolean)
pHasChild = Value
End Set
End Property
Public Property ParentID() As Integer
Get
ParentID = pParentID
End Get
Set(ByVal Value As Integer)
pParentID = Value
End Set
End Property
Private pViewType As Integer ' 0 = Group, 1 = Vendor
Private pSelectedID As Integer ' Obtained when selecteditemchanged event fires
Private pTier As Integer ' Indicates the current tier
Private pHasChild As Boolean ' Determines whether a downstream binding is required
Private pParentID As Integer ' Obtained from parent datagrid
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If Page.IsPostBack = False Then
SelectedID = Nothing
Me.dgCategory.Columns(0).Visible = True
Me.dgCategory.Columns(2).Visible = False
End If
End Sub
Private Sub dgCategory_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgCategory.ItemDataBound
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
e.Item.Attributes.Add("onmouseover", "this.style.backgroundColor='#eeeeee'")
e.Item.Style("cursor") = "hand"
e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor='white'")
' Dim button As LinkButton = CType(e.Item.Cells(0).Controls(0), LinkButton)
' e.Item.Attributes("onclick") = Page.GetPostBackClientHyperlink(button, "")
End If
If e.Item.ItemType = ListItemType.SelectedItem Then
e.Item.Style("backgroundcolor") = "#A3C2F0"
e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor='#A3C2F0'")
End If
End Sub
Public Sub dgCategory_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dgCategory.SelectedIndexChanged
Dim dg As DataGrid = CType(sender, DataGrid)
Dim drws As DataGridItem
For Each drws In dg.Items
If drws.ItemIndex <> dg.SelectedIndex Then
drws.Attributes.Add("onmouseover", "this.style.backgroundColor='#eeeeee'")
drws.Style("cursor") = "hand"
drws.Attributes.Add("onmouseout", "this.style.backgroundColor='white'")
End If
Next
dg.SelectedItem.Attributes.Remove("onmouseout")
dg.SelectedItem.Attributes.Remove("onmouseover")
dg.SelectedItem.Style("backgroundColor") = "#A3C2F0"
dg.SelectedItemStyle.BackColor = System.Drawing.Color.FromArgb(163, 194, 240)
End Sub
Public Sub BindData()
'Check current supplier and view style
'Retrieves list of Tier1 categories/vendors based on current view
Dim wcRequest As New System.Net.WebClient
Dim xmlReader As System.Xml.XmlTextReader
Dim xmlDataSet As New DataSet
Dim bytResponse As Byte()
Dim stResponse As String
Dim nvValues As New Specialized.NameValueCollection
nvValues.Add("type", ViewType)
If Tier <> 1 Then
nvValues.Add("tier" & (Tier - 1).ToString, ParentID)
End If
wcRequest.Headers.Add("Content-Type", "application/x-www-form-urlencoded")
bytResponse = wcRequest.UploadValues(System.Configuration.ConfigurationSettings.AppSettings.Item("itqURL") & "/catalogue.tier" & Tier, "POST", nvValues)
stResponse = System.Text.Encoding.ASCII.GetString(bytResponse)
xmlReader = New System.Xml.XmlTextReader(New IO.StringReader(stResponse))
xmlDataSet.ReadXml(xmlReader)
Me.dgCategory.DataSource = xmlDataSet.Tables("tier" & Tier.ToString)
Me.dgCategory.Columns(1).HeaderText = "Level " & Tier.ToString
Me.dgCategory.DataBind()
End Sub
********** THIS IS THE INSTANTIATION WITHIN THE MAIN ASPX ***********
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If Page.IsPostBack = False Then
Me.tabStrip.SelectedItemID = "Main"
BindTier1()
GridLevel4 = Me.LoadControl("spec_datagrid.ascx")
GridLevel4.HasChild = False
GridLevel4.ParentID = 3
GridLevel4.Tier = 1
GridLevel4.SelectedID = Nothing
GridLevel4.BindData()
GridLevel4.ID = "gl4"
Me.phMainGrid.Controls.Add(GridLevel4)
End If
End Sub