User Control inside a DataGrid

Loosey

New Member
Hi,<BR><BR>I am having problems accessing the properties of a UserControl inside a DataGrid. The property of the UserControl is "PostedBy", and I am getting the error message:<BR><BR> 'PostedBy' is not a member of 'System.Web.UI.UserControl'.<BR><BR>The strange this is that I ONLY get this error message when the UserControl is INSIDE a DataGrid - when the identical UserControl, on the same page, is OUTSIDE the DataGrid, everything is working perfectly.<BR><BR>Hope someone can help!<BR><BR>JON<BR><BR><BR><BR><BR><BR>++++++++++++++++++++++++++++++++<BR> USERCONTROL.ascx<BR>++++++++++++++++++++++++++++++++<BR><BR><%@ Control Language="VB" %><BR><%@ Import Namespace="System.Data" %><BR><%@ Import Namespace="System.Data.Oledb" %><BR><BR><script language="VB" runat="server"><BR><BR>Dim ds as Dataset = New DataSet() <BR><BR>Sub Page_Load(obj as object, e as EventArgs)<BR><BR> Dim objConn as New OleDbConnection ( ConfigurationSettings.AppSettings("ConnectionString") )<BR> Dim objAdapter as New OleDbDataAdapter ( "SELECT tblPeople.PersonID, tblPeople.FirstName AS People FROM tblPeople ORDER BY LastName;", objConn )<BR> objAdapter.Fill(ds, "tblPostedBy")<BR> ddDropDown.DataSource = ds.Tables("tblPostedBy")<BR> ddDropDown.DataTextField = "People"<BR> ddDropDown.DataValueField = "PersonID"<BR> <BR> If not Page.IsPostBack<BR> ddDropDown.DataBind() <BR> end if<BR><BR>end sub<BR><BR><BR>Public ReadOnly Property PostedBy As String<BR> Get<BR> Return ddDropDown.SelectedItem.Text<BR> End Get<BR>End Property<BR><BR><BR></script><BR><BR><BR><asp:Table CSSClass="TableStyle1" runat="server"><BR><asp:TableRow><BR><asp:TableCell><b>DropDownExample: </b></asp:TableCell><asp:TableCell><asp:DropDownList id="ddDropDown" runat="server" /></asp:TableCell><BR></asp:TableRow><BR></asp:Table><BR><BR><BR><BR><BR><BR>++++++++++++++++++++++++++++++++<BR> MAINPAGE.aspx<BR>++++++++++++++++++++++++++++++++<BR><BR><%@ Page Language="VB" Trace="false" %><BR><%@ Import Namespace="System.Data" %><BR><%@ Import Namespace="System.Data.Oledb" %><BR><BR><%@ Register TagPrefix="UserControl" TagName="ExampleUserControl" src=http://aspmessageboard.com/archive/index.php/"USERCONTROL.ascx" %><BR><BR><script language="VB" runat="server"><BR><BR>Dim objConn as New OleDbConnection ( ConfigurationSettings.AppSettings("ConnectionString") )<BR>Dim ds as Dataset = New DataSet()<BR>Dim objAdapter as New OleDbDataAdapter ( "SELECT * FROM tblItems where ItemID=1;", objConn )<BR><BR>Sub Page_Load(sender as object, e as eventargs)<BR> objConn.Open()<BR> objAdapter.Fill(ds, "tblItems")<BR> dg.DataSource = ds<BR> dg.DataMember = "tblItems"<BR> <BR> If Not Page.IsPostBack Then<BR> dg.Databind()<BR> End If<BR>End Sub<BR><BR>sub dg_edit(sender as object, e as DataGridCommandEventArgs)<BR> dg.edititemindex = e.item.itemindex<BR> dg.databind()<BR>end sub<BR><BR>sub dg_cancel(sender as object, e as DataGridCommandEventArgs)<BR> dg.edititemindex = -1<BR> dg.databind()<BR>end sub<BR><BR>sub dg_update(sender as object, e as DataGridCommandEventArgs) <BR> 'Update function here <BR>end sub<BR><BR>sub dg_command(sender as object, e as DataGridCommandEventArgs)<BR> Dim UserControlInsideDataGrid as UserControl = e.Item.FindControl("UserControlInsideDataGrid")<BR> Dim lbLabelInsideDataGrid as Label = e.Item.FindControl("lbLabelInsideDataGrid")<BR> lbLabelInsideDataGrid.Text = UserControlInsideDataGrid.Visible.ToString() '***THIS WORKS!!!!<BR> lbLabelInsideDataGrid.Text = UserControlInsideDataGrid.PostedBy '**** THIS DOESN'T WORK!!!<BR> '*** Above line produces error: BC30456: 'PostedBy' is not a member of 'System.Web.UI.UserControl'<BR>end sub<BR><BR>Sub ShowSelectedIndex(obj as Object, e as EventArgs)<BR> lbLabelOutsideDataGrid.Text = UserControlOutsideDataGrid.PostedBy<BR>End sub<BR><BR></script> <BR><BR><html><BR><head><BR><link rel="STYLESHEET" type="text/css" href="Forum.css"><BR></head><BR><body><BR><BR><h1>UserControls inside and outside a DataGrid</h1><BR><BR><form runat="server" name="form1" id="form1"><BR><BR><hr><BR><UserControl:ExampleUserControl id="UserControlOutsideDataGrid" runat="server" /><BR><BR><BR><asp:Button runat="server" Text="See who you selected" OnClick="ShowSelectedIndex" /><BR><BR><BR><asp:Label id="lbLabelOutsideDataGrid" runat="server" /><BR><hr><BR><BR><BR><BR><asp:DataGrid id="dg" runat="server"<BR> HeaderStyle-BackColor="#cccc99"<BR> FooterStyle-BackColor="#cccc99"<BR> ItemStyle-BackColor="#ffffff"<BR> AlternatingItemStyle-Backcolor="#cccccc"<BR> AutoGenerateColumns="False"<BR> OnEditCommand="dg_edit"<BR> OnCancelCommand="dg_cancel"<BR> OnUpdateCommand="dg_update"<BR> OnItemCommand="dg_command"<BR> ><BR><BR><BR><BR><Columns><BR><BR><asp:editcommandcolumn HeaderText="EDIT" edittext="Edit" UpdateText="Save" /><BR><BR><asp:TemplateColumn ItemStyle-VerticalAlign="top" HeaderText="Various"><BR> <ItemTemplate><BR> <UserControl:ExampleUserControl id="UserControlInsideDataGrid" runat="server" /> <BR> <asp:Button runat="server" Text="See who you selected" /><BR> <BR><BR> <asp:Label id="lbLabelInsideDataGrid" runat="server" /><BR> </ItemTemplate><BR></asp:TemplateColumn><BR><BR></Columns><BR><BR></asp:dataGrid><BR><BR></form> <BR></body></html>There are two possible problems. One is that your user control is called USERCONTROL. UserControl is a class (System.Web.UI.UserControl) and when you declare UserControlInsideDataGrid as UserControl, it may think you mean System.Web.UI.UserControl. The easiest fix is to rename your usercontrol. If you don't want to do that, you have to refer to it in such a way that is specific to USERCONTROL.ascx, not System.Web.UI.UserControl. That may require codebehinds, namespaces, classes, etc. and for simplicity's sake, I will not get into that.<BR><BR>Hope this helped...Yup, that was the problem.<BR><BR>To solve it, I changed the usercontrol code from:<BR><BR><%@ Control Language="VB" %><BR><BR>to:<BR><BR><%@ Control Language="VB" ClassName="Maz" %><BR><BR>... and then changed the code in the main page to:<BR><BR>Dim UserControlInsideDataGrid as Maz = e.Item.FindControl("UserControlInsideDataGrid")<BR><BR>Cheers for your help,<BR><BR>JON
 
Back
Top