| Home | Forums | Reviews | Guides | Newsgroups | Register | Search |
![]() |
| Thread Tools |
| Erik |
|
|
|
| |
|
Phillip Williams
Guest
Posts: n/a
|
Hi Erik,
The nested datalist that you described is only defined within the RepeaterItem. You can access it through the RepeaterItem.Controls collection, e.g., private void repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item ) { DataList dl = e.Item.FindControl ("DataList1"); if (dl !=null) { //write code to assign the datasource for the datalist } } } If you want to look at demos of using nested templated databound controls, I have several samples: http://www.societopia.net/Samples/ -- HTH, Phillip Williams http://www.societopia.net http://www.webswapp.com "Erik" wrote: > Good Afternoon, I am trying to nest a DataList in a Repeater ItemTemplate, > but am receiving a error stating that the datalist is not defined. The id is > set for the DataList. Is this possible? > > The reason for this architecture is to dynamically create the column header > labels and items for the Repeater / DataList. I am trying to use the same > Repeater / DataList for several different dataset, all with different fields > and different number of fields. Any other ideas are also greatly appreciated. |
|
|
|
|
|||
|
|||
| Phillip Williams |
|
|
|
| |
|
Erik
Guest
Posts: n/a
|
Thank you for the response Phillip. I am new to Repeaters and DataLists, and
am encountering an error when using the code provided. The FindControl is failing. Do you see something wrong with what I am doing? Below is the code. Thanks again, Erik HTML: <asp:Repeater id="DetailRepeater" runat="server"> <ItemTemplate> <asp </ItemTemplate> </asp:Repeater> VB.Net: Private Sub DetailRepeater_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles DetailRepeater.ItemDataBound If e.Item.ItemType = ListItemType.Item Then Dim dl As DataList dl = New DataList dl = e.Item.FindControl("dlstDetail") If Not dl Is Nothing Then 'write code to assign the datasource for the datalist dl.ItemTemplate = New DatalistLabelColumn dl.DataSource = oData.Tables(0).DefaultView dl.DataBind() End If End If End Sub Public Class DatalistLabelColumn Implements ITemplate Public Sub InstantiateIn(ByVal container As Control) Implements System.Web.UI.ITemplate.InstantiateIn Dim label1 As New Label AddHandler label1.DataBinding, AddressOf Me.BindLabelColumn container.Controls.Add(label1) End Sub 'InstantiateIn Public Sub BindLabelColumn(ByVal sender As Object, ByVal e As EventArgs) Dim lbl As Label = CType(sender, Label) Dim container As DataListItem = CType(lbl.NamingContainer, DataListItem) Dim strVals As String = "<tr><td>" + Convert.ToString(DataBinder.Eval(CType(container, DataListItem).DataItem, "CustomerNo")) + "</td>" + _ "<td>" + Convert.ToString(DataBinder.Eval(CType(container, DataListItem).DataItem, "ContactNo")) + "</td>" + _ "<td>" + Convert.ToString(DataBinder.Eval(CType(container, DataListItem).DataItem, "CompanyName")) + "</td>" + _ "<td>" + Convert.ToString(DataBinder.Eval(CType(container, DataListItem).DataItem, "ContactName")) + "</td>" + _ "<td>" + Convert.ToString(DataBinder.Eval(CType(container, DataListItem).DataItem, "ACTContact.CustomerClass")) + "</td>" + _ "<td>" + Convert.ToString(DataBinder.Eval(CType(container, DataListItem).DataItem, "Phone")) + "</td>" + _ "<td>" + Convert.ToString(DataBinder.Eval(CType(container, DataListItem).DataItem, "CallFrequency")) + "</td></tr>" lbl.Text = strVals End Sub 'BindLabelColumn End Class 'DatalistLabelColumn "Phillip Williams" wrote: > Hi Erik, > > The nested datalist that you described is only defined within the > RepeaterItem. You can access it through the RepeaterItem.Controls > collection, e.g., > > private void repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e) > { > if (e.Item.ItemType == ListItemType.Item ) > { > DataList dl = e.Item.FindControl ("DataList1"); > if (dl !=null) > { > //write code to assign the datasource for the datalist > } > } > } > > If you want to look at demos of using nested templated databound controls, I > have several samples: http://www.societopia.net/Samples/ > -- > HTH, > Phillip Williams > http://www.societopia.net > http://www.webswapp.com > > > "Erik" wrote: > > > Good Afternoon, I am trying to nest a DataList in a Repeater ItemTemplate, > > but am receiving a error stating that the datalist is not defined. The id is > > set for the DataList. Is this possible? > > > > The reason for this architecture is to dynamically create the column header > > labels and items for the Repeater / DataList. I am trying to use the same > > Repeater / DataList for several different dataset, all with different fields > > and different number of fields. Any other ideas are also greatly appreciated. |
|
|
|
|
|||
|
|||
| Erik |
|
Erik
Guest
Posts: n/a
|
I accidentally posted before I could ask if there is a better way to have a
dynamic repeater / datalist, headers and items? "Phillip Williams" wrote: > Hi Erik, > > The nested datalist that you described is only defined within the > RepeaterItem. You can access it through the RepeaterItem.Controls > collection, e.g., > > private void repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e) > { > if (e.Item.ItemType == ListItemType.Item ) > { > DataList dl = e.Item.FindControl ("DataList1"); > if (dl !=null) > { > //write code to assign the datasource for the datalist > } > } > } > > If you want to look at demos of using nested templated databound controls, I > have several samples: http://www.societopia.net/Samples/ > -- > HTH, > Phillip Williams > http://www.societopia.net > http://www.webswapp.com > > > "Erik" wrote: > > > Good Afternoon, I am trying to nest a DataList in a Repeater ItemTemplate, > > but am receiving a error stating that the datalist is not defined. The id is > > set for the DataList. Is this possible? > > > > The reason for this architecture is to dynamically create the column header > > labels and items for the Repeater / DataList. I am trying to use the same > > Repeater / DataList for several different dataset, all with different fields > > and different number of fields. Any other ideas are also greatly appreciated. |
|
|
|
|
|||
|
|||
| Erik |
|
Phillip Williams
Guest
Posts: n/a
|
I have to run now but I put it on my website after a few adjustments:
http://www.societopia.net/samples/da..._datalist.aspx Take to you later. -- HTH, Phillip Williams http://www.societopia.net http://www.webswapp.com "Erik" wrote: > I accidentally posted before I could ask if there is a better way to have a > dynamic repeater / datalist, headers and items? > > "Phillip Williams" wrote: > > > Hi Erik, > > > > The nested datalist that you described is only defined within the > > RepeaterItem. You can access it through the RepeaterItem.Controls > > collection, e.g., > > > > private void repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e) > > { > > if (e.Item.ItemType == ListItemType.Item ) > > { > > DataList dl = e.Item.FindControl ("DataList1"); > > if (dl !=null) > > { > > //write code to assign the datasource for the datalist > > } > > } > > } > > > > If you want to look at demos of using nested templated databound controls, I > > have several samples: http://www.societopia.net/Samples/ > > -- > > HTH, > > Phillip Williams > > http://www.societopia.net > > http://www.webswapp.com > > > > > > "Erik" wrote: > > > > > Good Afternoon, I am trying to nest a DataList in a Repeater ItemTemplate, > > > but am receiving a error stating that the datalist is not defined. The id is > > > set for the DataList. Is this possible? > > > > > > The reason for this architecture is to dynamically create the column header > > > labels and items for the Repeater / DataList. I am trying to use the same > > > Repeater / DataList for several different dataset, all with different fields > > > and different number of fields. Any other ideas are also greatly appreciated. |
|
|
|
|
|||
|
|||
| Phillip Williams |
|
Phillip Williams
Guest
Posts: n/a
|
Hi Erik,
I adjusted the code that you gave to me earlier As for the question of "a better way", it really depends on the complexity of the business case you are trying to implement. If the case is simple, I would prepare my datasets to represent the structure I wish to display and then use nested datagrids as I did in these 2 examples: http://www.societopia.net/Samples/Da...Hierarchy.aspx http://www.societopia.net/Samples/Da...Hierarchy.aspx In more complex scenarios where there are ItemTemplates containing other server controls, I usually have a certain number of possible scenarios for which I design separate Templates, e.g. case 1: dl.ItemTemplate = New Template1(ListItemType.Item) case 2: dl.ItemTemplate = New Template2(ListItemType.Item) and so on. -- HTH, Phillip Williams http://www.societopia.net http://www.webswapp.com "Erik" wrote: > I accidentally posted before I could ask if there is a better way to have a > dynamic repeater / datalist, headers and items? > > "Phillip Williams" wrote: > > > Hi Erik, > > > > The nested datalist that you described is only defined within the > > RepeaterItem. You can access it through the RepeaterItem.Controls > > collection, e.g., > > > > private void repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e) > > { > > if (e.Item.ItemType == ListItemType.Item ) > > { > > DataList dl = e.Item.FindControl ("DataList1"); > > if (dl !=null) > > { > > //write code to assign the datasource for the datalist > > } > > } > > } > > > > If you want to look at demos of using nested templated databound controls, I > > have several samples: http://www.societopia.net/Samples/ > > -- > > HTH, > > Phillip Williams > > http://www.societopia.net > > http://www.webswapp.com > > > > > > "Erik" wrote: > > > > > Good Afternoon, I am trying to nest a DataList in a Repeater ItemTemplate, > > > but am receiving a error stating that the datalist is not defined. The id is > > > set for the DataList. Is this possible? > > > > > > The reason for this architecture is to dynamically create the column header > > > labels and items for the Repeater / DataList. I am trying to use the same > > > Repeater / DataList for several different dataset, all with different fields > > > and different number of fields. Any other ideas are also greatly appreciated. |
|
|
|
|
|||
|
|||
| Phillip Williams |
|
Erik
Guest
Posts: n/a
|
Thank you again for the post and willingness to help me out. I keep running
into errors when trying to adapt the sample provided. I am new to Repeaters / DataLists, and can't use a DataGrid because of ViewState. Unfortunately, I also don't have experience with custom classes. Here is my adapted code. As you can see, everything for the datalist is driven by control / screen values and the dataset. The number of Columns, Column Names, and record count will vary. When I trace through in debug, everything appears to be assigned properly. However, when the page is rendered, an error is presented. The current error is that the sql connection sqlconctxcontrol has not been initialized in BindLabelColumn. HTML: As provided. VB.Net: Public Function BuildContacts(ByRef sRMWCUniqueId As String, ByRef sCustomerClass As String, ByRef sRecordType As String) As String Dim strSQL As String strSQL = "SELECT CustomerNo, ContactNo, CompanyName, ContactName, ACTContact.CustomerClass, Phone, CallFrequency FROM ACTContact " & _ "WHERE RecordManagerWCUniqueID = '" & sRMWCUniqueId.Trim & "' " & _ "AND RecordType = '" & sRecordType.Trim & "' " If sCustomerClass.Trim <> "TOTAL" Then strSQL &= "AND ACTContact.CustomerClass = '" & sCustomerClass.Trim & "' " End If strSQL &= "ORDER BY ACTContact.CustomerClass, ACTContact.CompanyName " Return strSQL End Function Private Function Data_Table() As DataTable Dim dt As DataTable If Session("DataGrid_ChildControls") Is Nothing Then Dim dr As DataRow Dim i As Integer 'create a DataTable dt = New DataTable Dim sSQL As String sSQL = BuildContacts(lblHiddenRMWCUniqueId.Text, lblHiddenCustomerClass.Text, lblHiddenRecordType.Text) Dim oAdpt As SqlDataAdapter oData = New DataSet oAdpt = New SqlDataAdapter(sSQL, sqlconCTXControl) oAdpt.Fill(oData, "ACTContact") Dim dc As DataColumn For Each dc In oData.Tables(0).Columns dt.Columns.Add(New DataColumn(dc.ColumnName)) Next 'Make some rows and put some sample data in Dim currRows() As DataRow = oData.Tables(0).Select(Nothing, Nothing, DataViewRowState.CurrentRows) Dim drDataRow As DataRow For Each dr In currRows drDataRow = dt.NewRow() For i = 0 To oData.Tables(0).Columns.Count - 1 drDataRow(i) = dr(i).ToString.Trim() Next dt.Rows.Add(drDataRow) Next Session("DataGrid_ChildControls") = dt Else dt = CType(Session("DataGrid_ChildControls"), DataTable) End If Return dt End Function Public Class DatalistLabelColumn Inherits frmCTXDrillDownRptGrid Implements ITemplate Dim TemplateType As ListItemType Sub New(ByVal type As ListItemType) TemplateType = type End Sub Public Sub InstantiateIn(ByVal container As Control) Implements System.Web.UI.ITemplate.InstantiateIn Select Case TemplateType Case ListItemType.Header Dim lc As New Literal lc.Text = "This the table header" container.Controls.Add(lc) Case ListItemType.Item Dim label1 As New Label AddHandler label1.DataBinding, AddressOf Me.BindLabelColumn container.Controls.Add(label1) Case ListItemType.Footer Dim lc As New Literal lc.Text = "This is the table footer" container.Controls.Add(lc) End Select End Sub 'InstantiateIn Public Sub BindLabelColumn(ByVal sender As Object, ByVal e As EventArgs) Dim lbl As Label = CType(sender, Label) Dim container As DataListItem = CType(lbl.NamingContainer, DataListItem) Dim sSQL As String 'sSQL = BuildContacts(lblHiddenCustomerClass.Text, lblHiddenCustomerClass.Text, lblHiddenRecordType.Text) sSQL = BuildContacts("eMfmaapV_cac", "D", "Account") Dim oAdpt As SqlDataAdapter oData = New DataSet oAdpt = New SqlDataAdapter(sSQL, sqlconCTXControl) oAdpt.Fill(oData, "ACTContact") Dim dc As DataColumn Dim strvals As String strvals = "<tr>" For Each dc In oData.Tables(0).Columns strvals &= "<td>" + Convert.ToString(DataBinder.Eval(CType(container, DataListItem).DataItem, dc.ColumnName)) + "</td>" Next strvals &= "</tr>" lbl.Text = strvals oAdpt.Dispose() oData.Dispose() dc.Dispose() End Sub 'BindLabelColumn End Class 'DatalistLabelColumn Protected Sub DetailRepeater_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) If e.Item.ItemType = ListItemType.Item Then Dim dl As DataList dl = New DataList dl = e.Item.FindControl("dlstDetail") If Not dl Is Nothing Then 'write code to assign the datasource for the datalist 'dl.HeaderTemplate = New DatalistLabelColumn(ListItemType.Header) dl.ItemTemplate = New DatalistLabelColumn(ListItemType.Item) 'dl.FooterTemplate = New DatalistLabelColumn(ListItemType.Footer) dl.DataSource = New DataView(Data_Table) dl.DataBind() End If End If End Sub "Phillip Williams" wrote: > Hi Erik, > > I adjusted the code that you gave to me earlier > > As for the question of "a better way", it really depends on the complexity > of the business case you are trying to implement. If the case is simple, I > would prepare my datasets to represent the structure I wish to display and > then use nested datagrids as I did in these 2 examples: > http://www.societopia.net/Samples/Da...Hierarchy.aspx > http://www.societopia.net/Samples/Da...Hierarchy.aspx > > In more complex scenarios where there are ItemTemplates containing other > server controls, I usually have a certain number of possible scenarios for > which I design separate Templates, e.g. > case 1: > dl.ItemTemplate = New Template1(ListItemType.Item) > case 2: > dl.ItemTemplate = New Template2(ListItemType.Item) > and so on. > > -- > HTH, > Phillip Williams > http://www.societopia.net > http://www.webswapp.com > > > "Erik" wrote: > > > I accidentally posted before I could ask if there is a better way to have a > > dynamic repeater / datalist, headers and items? > > > > "Phillip Williams" wrote: > > > > > Hi Erik, > > > > > > The nested datalist that you described is only defined within the > > > RepeaterItem. You can access it through the RepeaterItem.Controls > > > collection, e.g., > > > > > > private void repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e) > > > { > > > if (e.Item.ItemType == ListItemType.Item ) > > > { > > > DataList dl = e.Item.FindControl ("DataList1"); > > > if (dl !=null) > > > { > > > //write code to assign the datasource for the datalist > > > } > > > } > > > } > > > > > > If you want to look at demos of using nested templated databound controls, I > > > have several samples: http://www.societopia.net/Samples/ > > > -- > > > HTH, > > > Phillip Williams > > > http://www.societopia.net > > > http://www.webswapp.com > > > > > > > > > "Erik" wrote: > > > > > > > Good Afternoon, I am trying to nest a DataList in a Repeater ItemTemplate, > > > > but am receiving a error stating that the datalist is not defined. The id is > > > > set for the DataList. Is this possible? > > > > > > > > The reason for this architecture is to dynamically create the column header > > > > labels and items for the Repeater / DataList. I am trying to use the same > > > > Repeater / DataList for several different dataset, all with different fields > > > > and different number of fields. Any other ideas are also greatly appreciated. |
|
|
|
|
|||
|
|||
| Erik |
|
Phillip Williams
Guest
Posts: n/a
|
You are welcome Erik. The error you get though has nothing to do with the
repeater/datalist. The error is saying the connection sqlconCTXControl has not been initialized. By looking at the code you posted I do not see the step where you initialized the connection object. Usually one would do something like this: Dim sqlconCTXControl As New SqlConnection(connStr) For further detail on the connection string refer to this link http://msdn.microsoft.com/library/de...classtopic.asp -- HTH, Phillip Williams http://www.societopia.net http://www.webswapp.com "Erik" wrote: > Thank you again for the post and willingness to help me out. I keep running > into errors when trying to adapt the sample provided. I am new to Repeaters > / DataLists, and can't use a DataGrid because of ViewState. Unfortunately, I > also don't have experience with custom classes. > > Here is my adapted code. As you can see, everything for the datalist is > driven by control / screen values and the dataset. The number of Columns, > Column Names, and record count will vary. > > When I trace through in debug, everything appears to be assigned properly. > However, when the page is rendered, an error is presented. The current error > is that the sql connection sqlconctxcontrol has not been initialized in > BindLabelColumn. > > HTML: As provided. > > VB.Net: > Public Function BuildContacts(ByRef sRMWCUniqueId As String, ByRef > sCustomerClass As String, ByRef sRecordType As String) As String > Dim strSQL As String > > strSQL = "SELECT CustomerNo, ContactNo, CompanyName, ContactName, > ACTContact.CustomerClass, Phone, CallFrequency FROM ACTContact " & _ > "WHERE RecordManagerWCUniqueID = '" & sRMWCUniqueId.Trim & "' " > & _ > "AND RecordType = '" & sRecordType.Trim & "' " > > If sCustomerClass.Trim <> "TOTAL" Then > strSQL &= "AND ACTContact.CustomerClass = '" & > sCustomerClass.Trim & "' " > End If > > strSQL &= "ORDER BY ACTContact.CustomerClass, ACTContact.CompanyName " > > Return strSQL > > End Function > > Private Function Data_Table() As DataTable > Dim dt As DataTable > If Session("DataGrid_ChildControls") Is Nothing Then > Dim dr As DataRow > Dim i As Integer > > 'create a DataTable > dt = New DataTable > > Dim sSQL As String > > sSQL = BuildContacts(lblHiddenRMWCUniqueId.Text, > lblHiddenCustomerClass.Text, lblHiddenRecordType.Text) > > Dim oAdpt As SqlDataAdapter > oData = New DataSet > > oAdpt = New SqlDataAdapter(sSQL, sqlconCTXControl) > oAdpt.Fill(oData, "ACTContact") > > Dim dc As DataColumn > > For Each dc In oData.Tables(0).Columns > dt.Columns.Add(New DataColumn(dc.ColumnName)) > Next > > 'Make some rows and put some sample data in > Dim currRows() As DataRow = oData.Tables(0).Select(Nothing, > Nothing, DataViewRowState.CurrentRows) > Dim drDataRow As DataRow > > For Each dr In currRows > drDataRow = dt.NewRow() > For i = 0 To oData.Tables(0).Columns.Count - 1 > drDataRow(i) = dr(i).ToString.Trim() > Next > dt.Rows.Add(drDataRow) > Next > > Session("DataGrid_ChildControls") = dt > Else > dt = CType(Session("DataGrid_ChildControls"), DataTable) > End If > > Return dt > End Function > > Public Class DatalistLabelColumn > Inherits frmCTXDrillDownRptGrid > Implements ITemplate > > Dim TemplateType As ListItemType > > Sub New(ByVal type As ListItemType) > TemplateType = type > End Sub > Public Sub InstantiateIn(ByVal container As Control) Implements > System.Web.UI.ITemplate.InstantiateIn > > Select Case TemplateType > Case ListItemType.Header > Dim lc As New Literal > lc.Text = "This the table header" > container.Controls.Add(lc) > Case ListItemType.Item > Dim label1 As New Label > > AddHandler label1.DataBinding, AddressOf > Me.BindLabelColumn > > container.Controls.Add(label1) > Case ListItemType.Footer > Dim lc As New Literal > lc.Text = "This is the table footer" > container.Controls.Add(lc) > End Select > > End Sub 'InstantiateIn > > > Public Sub BindLabelColumn(ByVal sender As Object, ByVal e As > EventArgs) > > Dim lbl As Label = CType(sender, Label) > > Dim container As DataListItem = CType(lbl.NamingContainer, > DataListItem) > > Dim sSQL As String > > 'sSQL = BuildContacts(lblHiddenCustomerClass.Text, > lblHiddenCustomerClass.Text, lblHiddenRecordType.Text) > sSQL = BuildContacts("eMfmaapV_cac", "D", "Account") > > Dim oAdpt As SqlDataAdapter > oData = New DataSet > > oAdpt = New SqlDataAdapter(sSQL, sqlconCTXControl) > oAdpt.Fill(oData, "ACTContact") > > Dim dc As DataColumn > Dim strvals As String > > strvals = "<tr>" > > For Each dc In oData.Tables(0).Columns > strvals &= "<td>" + > Convert.ToString(DataBinder.Eval(CType(container, DataListItem).DataItem, > dc.ColumnName)) + "</td>" > Next > > strvals &= "</tr>" > > lbl.Text = strvals > > oAdpt.Dispose() > oData.Dispose() > dc.Dispose() > > End Sub 'BindLabelColumn > > End Class 'DatalistLabelColumn > > Protected Sub DetailRepeater_ItemDataBound(ByVal sender As Object, ByVal > e As System.Web.UI.WebControls.RepeaterItemEventArgs) > If e.Item.ItemType = ListItemType.Item Then > Dim dl As DataList > > dl = New DataList > > dl = e.Item.FindControl("dlstDetail") > If Not dl Is Nothing Then > > 'write code to assign the datasource for the datalist > 'dl.HeaderTemplate = New > DatalistLabelColumn(ListItemType.Header) > dl.ItemTemplate = New DatalistLabelColumn(ListItemType.Item) > 'dl.FooterTemplate = New > DatalistLabelColumn(ListItemType.Footer) > dl.DataSource = New DataView(Data_Table) > > dl.DataBind() > > End If > End If > > End Sub > > > "Phillip Williams" wrote: > > > Hi Erik, > > > > I adjusted the code that you gave to me earlier > > > > As for the question of "a better way", it really depends on the complexity > > of the business case you are trying to implement. If the case is simple, I > > would prepare my datasets to represent the structure I wish to display and > > then use nested datagrids as I did in these 2 examples: > > http://www.societopia.net/Samples/Da...Hierarchy.aspx > > http://www.societopia.net/Samples/Da...Hierarchy.aspx > > > > In more complex scenarios where there are ItemTemplates containing other > > server controls, I usually have a certain number of possible scenarios for > > which I design separate Templates, e.g. > > case 1: > > dl.ItemTemplate = New Template1(ListItemType.Item) > > case 2: > > dl.ItemTemplate = New Template2(ListItemType.Item) > > and so on. > > > > -- > > HTH, > > Phillip Williams > > http://www.societopia.net > > http://www.webswapp.com > > > > > > "Erik" wrote: > > > > > I accidentally posted before I could ask if there is a better way to have a > > > dynamic repeater / datalist, headers and items? > > > > > > "Phillip Williams" wrote: > > > > > > > Hi Erik, > > > > > > > > The nested datalist that you described is only defined within the > > > > RepeaterItem. You can access it through the RepeaterItem.Controls > > > > collection, e.g., > > > > > > > > private void repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e) > > > > { > > > > if (e.Item.ItemType == ListItemType.Item ) > > > > { > > > > DataList dl = e.Item.FindControl ("DataList1"); > > > > if (dl !=null) > > > > { > > > > //write code to assign the datasource for the datalist > > > > } > > > > } > > > > } > > > > > > > > If you want to look at demos of using nested templated databound controls, I > > > > have several samples: http://www.societopia.net/Samples/ > > > > -- > > > > HTH, > > > > Phillip Williams > > > > http://www.societopia.net > > > > http://www.webswapp.com > > > > > > > > > > > > "Erik" wrote: > > > > > > > > > Good Afternoon, I am trying to nest a DataList in a Repeater ItemTemplate, > > > > > but am receiving a error stating that the datalist is not defined. The id is > > > > > set for the DataList. Is this possible? > > > > > > > > > > The reason for this architecture is to dynamically create the column header > > > > > labels and items for the Repeater / DataList. I am trying to use the same > > > > > Repeater / DataList for several different dataset, all with different fields > > > > > and different number of fields. Any other ideas are also greatly appreciated. |
|
|
|
|
|||
|
|||
| Phillip Williams |
|
Erik
Guest
Posts: n/a
|
Shouldn't the connection be inherited from frmCTXDrillDownRptGrid?
Public Class DatalistLabelColumn Inherits frmCTXDrillDownRptGrid The connection is initialized in InitializeComponent for class frmCTXDrillDownRptGrid. Thanks, Erik "Phillip Williams" wrote: > You are welcome Erik. The error you get though has nothing to do with the > repeater/datalist. The error is saying the connection sqlconCTXControl has > not been initialized. By looking at the code you posted I do not see the > step where you initialized the connection object. Usually one would do > something like this: > Dim sqlconCTXControl As New SqlConnection(connStr) > > For further detail on the connection string refer to this link > http://msdn.microsoft.com/library/de...classtopic.asp > > -- > HTH, > Phillip Williams > http://www.societopia.net > http://www.webswapp.com > > > "Erik" wrote: > > > Thank you again for the post and willingness to help me out. I keep running > > into errors when trying to adapt the sample provided. I am new to Repeaters > > / DataLists, and can't use a DataGrid because of ViewState. Unfortunately, I > > also don't have experience with custom classes. > > > > Here is my adapted code. As you can see, everything for the datalist is > > driven by control / screen values and the dataset. The number of Columns, > > Column Names, and record count will vary. > > > > When I trace through in debug, everything appears to be assigned properly. > > However, when the page is rendered, an error is presented. The current error > > is that the sql connection sqlconctxcontrol has not been initialized in > > BindLabelColumn. > > > > HTML: As provided. > > > > VB.Net: > > Public Function BuildContacts(ByRef sRMWCUniqueId As String, ByRef > > sCustomerClass As String, ByRef sRecordType As String) As String > > Dim strSQL As String > > > > strSQL = "SELECT CustomerNo, ContactNo, CompanyName, ContactName, > > ACTContact.CustomerClass, Phone, CallFrequency FROM ACTContact " & _ > > "WHERE RecordManagerWCUniqueID = '" & sRMWCUniqueId.Trim & "' " > > & _ > > "AND RecordType = '" & sRecordType.Trim & "' " > > > > If sCustomerClass.Trim <> "TOTAL" Then > > strSQL &= "AND ACTContact.CustomerClass = '" & > > sCustomerClass.Trim & "' " > > End If > > > > strSQL &= "ORDER BY ACTContact.CustomerClass, ACTContact.CompanyName " > > > > Return strSQL > > > > End Function > > > > Private Function Data_Table() As DataTable > > Dim dt As DataTable > > If Session("DataGrid_ChildControls") Is Nothing Then > > Dim dr As DataRow > > Dim i As Integer > > > > 'create a DataTable > > dt = New DataTable > > > > Dim sSQL As String > > > > sSQL = BuildContacts(lblHiddenRMWCUniqueId.Text, > > lblHiddenCustomerClass.Text, lblHiddenRecordType.Text) > > > > Dim oAdpt As SqlDataAdapter > > oData = New DataSet > > > > oAdpt = New SqlDataAdapter(sSQL, sqlconCTXControl) > > oAdpt.Fill(oData, "ACTContact") > > > > Dim dc As DataColumn > > > > For Each dc In oData.Tables(0).Columns > > dt.Columns.Add(New DataColumn(dc.ColumnName)) > > Next > > > > 'Make some rows and put some sample data in > > Dim currRows() As DataRow = oData.Tables(0).Select(Nothing, > > Nothing, DataViewRowState.CurrentRows) > > Dim drDataRow As DataRow > > > > For Each dr In currRows > > drDataRow = dt.NewRow() > > For i = 0 To oData.Tables(0).Columns.Count - 1 > > drDataRow(i) = dr(i).ToString.Trim() > > Next > > dt.Rows.Add(drDataRow) > > Next > > > > Session("DataGrid_ChildControls") = dt > > Else > > dt = CType(Session("DataGrid_ChildControls"), DataTable) > > End If > > > > Return dt > > End Function > > > > Public Class DatalistLabelColumn > > Inherits frmCTXDrillDownRptGrid > > Implements ITemplate > > > > Dim TemplateType As ListItemType > > > > Sub New(ByVal type As ListItemType) > > TemplateType = type > > End Sub > > Public Sub InstantiateIn(ByVal container As Control) Implements > > System.Web.UI.ITemplate.InstantiateIn > > > > Select Case TemplateType > > Case ListItemType.Header > > Dim lc As New Literal > > lc.Text = "This the table header" > > container.Controls.Add(lc) > > Case ListItemType.Item > > Dim label1 As New Label > > > > AddHandler label1.DataBinding, AddressOf > > Me.BindLabelColumn > > > > container.Controls.Add(label1) > > Case ListItemType.Footer > > Dim lc As New Literal > > lc.Text = "This is the table footer" > > container.Controls.Add(lc) > > End Select > > > > End Sub 'InstantiateIn > > > > > > Public Sub BindLabelColumn(ByVal sender As Object, ByVal e As > > EventArgs) > > > > Dim lbl As Label = CType(sender, Label) > > > > Dim container As DataListItem = CType(lbl.NamingContainer, > > DataListItem) > > > > Dim sSQL As String > > > > 'sSQL = BuildContacts(lblHiddenCustomerClass.Text, > > lblHiddenCustomerClass.Text, lblHiddenRecordType.Text) > > sSQL = BuildContacts("eMfmaapV_cac", "D", "Account") > > > > Dim oAdpt As SqlDataAdapter > > oData = New DataSet > > > > oAdpt = New SqlDataAdapter(sSQL, sqlconCTXControl) > > oAdpt.Fill(oData, "ACTContact") > > > > Dim dc As DataColumn > > Dim strvals As String > > > > strvals = "<tr>" > > > > For Each dc In oData.Tables(0).Columns > > strvals &= "<td>" + > > Convert.ToString(DataBinder.Eval(CType(container, DataListItem).DataItem, > > dc.ColumnName)) + "</td>" > > Next > > > > strvals &= "</tr>" > > > > lbl.Text = strvals > > > > oAdpt.Dispose() > > oData.Dispose() > > dc.Dispose() > > > > End Sub 'BindLabelColumn > > > > End Class 'DatalistLabelColumn > > > > Protected Sub DetailRepeater_ItemDataBound(ByVal sender As Object, ByVal > > e As System.Web.UI.WebControls.RepeaterItemEventArgs) > > If e.Item.ItemType = ListItemType.Item Then > > Dim dl As DataList > > > > dl = New DataList > > > > dl = e.Item.FindControl("dlstDetail") > > If Not dl Is Nothing Then > > > > 'write code to assign the datasource for the datalist > > 'dl.HeaderTemplate = New > > DatalistLabelColumn(ListItemType.Header) > > dl.ItemTemplate = New DatalistLabelColumn(ListItemType.Item) > > 'dl.FooterTemplate = New > > DatalistLabelColumn(ListItemType.Footer) > > dl.DataSource = New DataView(Data_Table) > > > > dl.DataBind() > > > > End If > > End If > > > > End Sub > > > > > > "Phillip Williams" wrote: > > > > > Hi Erik, > > > > > > I adjusted the code that you gave to me earlier > > > > > > As for the question of "a better way", it really depends on the complexity > > > of the business case you are trying to implement. If the case is simple, I > > > would prepare my datasets to represent the structure I wish to display and > > > then use nested datagrids as I did in these 2 examples: > > > http://www.societopia.net/Samples/Da...Hierarchy.aspx > > > http://www.societopia.net/Samples/Da...Hierarchy.aspx > > > > > > In more complex scenarios where there are ItemTemplates containing other > > > server controls, I usually have a certain number of possible scenarios for > > > which I design separate Templates, e.g. > > > case 1: > > > dl.ItemTemplate = New Template1(ListItemType.Item) > > > case 2: > > > dl.ItemTemplate = New Template2(ListItemType.Item) > > > and so on. > > > > > > -- > > > HTH, > > > Phillip Williams > > > http://www.societopia.net > > > http://www.webswapp.com > > > > > > > > > "Erik" wrote: > > > > > > > I accidentally posted before I could ask if there is a better way to have a > > > > dynamic repeater / datalist, headers and items? > > > > > > > > "Phillip Williams" wrote: > > > > > > > > > Hi Erik, > > > > > > > > > > The nested datalist that you described is only defined within the > > > > > RepeaterItem. You can access it through the RepeaterItem.Controls > > > > > collection, e.g., > > > > > > > > > > private void repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e) > > > > > { > > > > > if (e.Item.ItemType == ListItemType.Item ) > > > > > { > > > > > DataList dl = e.Item.FindControl ("DataList1"); > > > > > if (dl !=null) > > > > > { > > > > > //write code to assign the datasource for the datalist > > > > > } > > > > > } > > > > > } > > > > > > > > > > If you want to look at demos of using nested templated databound controls, I > > > > > have several samples: http://www.societopia.net/Samples/ > > > > > -- > > > > > HTH, > > > > > Phillip Williams > > > > > http://www.societopia.net > > > > > http://www.webswapp.com > > > > > > > > > > > > > > > "Erik" wrote: > > > > > > > > > > > Good Afternoon, I am trying to nest a DataList in a Repeater ItemTemplate, > > > > > > but am receiving a error stating that the datalist is not defined. The id is > > > > > > set for the DataList. Is this possible? > > > > > > > > > > > > The reason for this architecture is to dynamically create the column header > > > > > > labels and items for the Repeater / DataList. I am trying to use the same > > > > > > Repeater / DataList for several different dataset, all with different fields > > > > > > and different number of fields. Any other ideas are also greatly appreciated. |
|
|
|
|
|||
|
|||
| Erik |
|
Phillip Williams
Guest
Posts: n/a
|
BTW, one more note about the way you created your data. You do not need to
create a new datatable and copy to it the data that you got from the dataadapter. You could have simply replaced all that code with 'create a DataTable Dim sSQL As String sSQL = BuildContacts(lblHiddenRMWCUniqueId.Text, lblHiddenCustomerClass.Text, lblHiddenRecordType.Text) Dim oAdpt As SqlDataAdapter oData = New DataSet oAdpt = New SqlDataAdapter(sSQL, sqlconCTXControl) oAdpt.Fill(oData, "ACTContact") dt = oData.Tables(0) Session("DataGrid_ChildControls") = dt -- HTH, Phillip Williams http://www.societopia.net http://www.webswapp.com "Phillip Williams" wrote: > You are welcome Erik. The error you get though has nothing to do with the > repeater/datalist. The error is saying the connection sqlconCTXControl has > not been initialized. By looking at the code you posted I do not see the > step where you initialized the connection object. Usually one would do > something like this: > Dim sqlconCTXControl As New SqlConnection(connStr) > > For further detail on the connection string refer to this link > http://msdn.microsoft.com/library/de...classtopic.asp > > -- > HTH, > Phillip Williams > http://www.societopia.net > http://www.webswapp.com > > > "Erik" wrote: > > > Thank you again for the post and willingness to help me out. I keep running > > into errors when trying to adapt the sample provided. I am new to Repeaters > > / DataLists, and can't use a DataGrid because of ViewState. Unfortunately, I > > also don't have experience with custom classes. > > > > Here is my adapted code. As you can see, everything for the datalist is > > driven by control / screen values and the dataset. The number of Columns, > > Column Names, and record count will vary. > > > > When I trace through in debug, everything appears to be assigned properly. > > However, when the page is rendered, an error is presented. The current error > > is that the sql connection sqlconctxcontrol has not been initialized in > > BindLabelColumn. > > > > HTML: As provided. > > > > VB.Net: > > Public Function BuildContacts(ByRef sRMWCUniqueId As String, ByRef > > sCustomerClass As String, ByRef sRecordType As String) As String > > Dim strSQL As String > > > > strSQL = "SELECT CustomerNo, ContactNo, CompanyName, ContactName, > > ACTContact.CustomerClass, Phone, CallFrequency FROM ACTContact " & _ > > "WHERE RecordManagerWCUniqueID = '" & sRMWCUniqueId.Trim & "' " > > & _ > > "AND RecordType = '" & sRecordType.Trim & "' " > > > > If sCustomerClass.Trim <> "TOTAL" Then > > strSQL &= "AND ACTContact.CustomerClass = '" & > > sCustomerClass.Trim & "' " > > End If > > > > strSQL &= "ORDER BY ACTContact.CustomerClass, ACTContact.CompanyName " > > > > Return strSQL > > > > End Function > > > > Private Function Data_Table() As DataTable > > Dim dt As DataTable > > If Session("DataGrid_ChildControls") Is Nothing Then > > Dim dr As DataRow > > Dim i As Integer > > > > 'create a DataTable > > dt = New DataTable > > > > Dim sSQL As String > > > > sSQL = BuildContacts(lblHiddenRMWCUniqueId.Text, > > lblHiddenCustomerClass.Text, lblHiddenRecordType.Text) > > > > Dim oAdpt As SqlDataAdapter > > oData = New DataSet > > > > oAdpt = New SqlDataAdapter(sSQL, sqlconCTXControl) > > oAdpt.Fill(oData, "ACTContact") > > > > Dim dc As DataColumn > > > > For Each dc In oData.Tables(0).Columns > > dt.Columns.Add(New DataColumn(dc.ColumnName)) > > Next > > > > 'Make some rows and put some sample data in > > Dim currRows() As DataRow = oData.Tables(0).Select(Nothing, > > Nothing, DataViewRowState.CurrentRows) > > Dim drDataRow As DataRow > > > > For Each dr In currRows > > drDataRow = dt.NewRow() > > For i = 0 To oData.Tables(0).Columns.Count - 1 > > drDataRow(i) = dr(i).ToString.Trim() > > Next > > dt.Rows.Add(drDataRow) > > Next > > > > Session("DataGrid_ChildControls") = dt > > Else > > dt = CType(Session("DataGrid_ChildControls"), DataTable) > > End If > > > > Return dt > > End Function > > > > Public Class DatalistLabelColumn > > Inherits frmCTXDrillDownRptGrid > > Implements ITemplate > > > > Dim TemplateType As ListItemType > > > > Sub New(ByVal type As ListItemType) > > TemplateType = type > > End Sub > > Public Sub InstantiateIn(ByVal container As Control) Implements > > System.Web.UI.ITemplate.InstantiateIn > > > > Select Case TemplateType > > Case ListItemType.Header > > Dim lc As New Literal > > lc.Text = "This the table header" > > container.Controls.Add(lc) > > Case ListItemType.Item > > Dim label1 As New Label > > > > AddHandler label1.DataBinding, AddressOf > > Me.BindLabelColumn > > > > container.Controls.Add(label1) > > Case ListItemType.Footer > > Dim lc As New Literal > > lc.Text = "This is the table footer" > > container.Controls.Add(lc) > > End Select > > > > End Sub 'InstantiateIn > > > > > > Public Sub BindLabelColumn(ByVal sender As Object, ByVal e As > > EventArgs) > > > > Dim lbl As Label = CType(sender, Label) > > > > Dim container As DataListItem = CType(lbl.NamingContainer, > > DataListItem) > > > > Dim sSQL As String > > > > 'sSQL = BuildContacts(lblHiddenCustomerClass.Text, > > lblHiddenCustomerClass.Text, lblHiddenRecordType.Text) > > sSQL = BuildContacts("eMfmaapV_cac", "D", "Account") > > > > Dim oAdpt As SqlDataAdapter > > oData = New DataSet > > > > oAdpt = New SqlDataAdapter(sSQL, sqlconCTXControl) > > oAdpt.Fill(oData, "ACTContact") > > > > Dim dc As DataColumn > > Dim strvals As String > > > > strvals = "<tr>" > > > > For Each dc In oData.Tables(0).Columns > > strvals &= "<td>" + > > Convert.ToString(DataBinder.Eval(CType(container, DataListItem).DataItem, > > dc.ColumnName)) + "</td>" > > Next > > > > strvals &= "</tr>" > > > > lbl.Text = strvals > > > > oAdpt.Dispose() > > oData.Dispose() > > dc.Dispose() > > > > End Sub 'BindLabelColumn > > > > End Class 'DatalistLabelColumn > > > > Protected Sub DetailRepeater_ItemDataBound(ByVal sender As Object, ByVal > > e As System.Web.UI.WebControls.RepeaterItemEventArgs) > > If e.Item.ItemType = ListItemType.Item Then > > Dim dl As DataList > > > > dl = New DataList > > > > dl = e.Item.FindControl("dlstDetail") > > If Not dl Is Nothing Then > > > > 'write code to assign the datasource for the datalist > > 'dl.HeaderTemplate = New > > DatalistLabelColumn(ListItemType.Header) > > dl.ItemTemplate = New DatalistLabelColumn(ListItemType.Item) > > 'dl.FooterTemplate = New > > DatalistLabelColumn(ListItemType.Footer) > > dl.DataSource = New DataView(Data_Table) > > > > dl.DataBind() > > > > End If > > End If > > > > End Sub > > > > > > "Phillip Williams" wrote: > > > > > Hi Erik, > > > > > > I adjusted the code that you gave to me earlier > > > > > > As for the question of "a better way", it really depends on the complexity > > > of the business case you are trying to implement. If the case is simple, I > > > would prepare my datasets to represent the structure I wish to display and > > > then use nested datagrids as I did in these 2 examples: > > > http://www.societopia.net/Samples/Da...Hierarchy.aspx > > > http://www.societopia.net/Samples/Da...Hierarchy.aspx > > > > > > In more complex scenarios where there are ItemTemplates containing other > > > server controls, I usually have a certain number of possible scenarios for > > > which I design separate Templates, e.g. > > > case 1: > > > dl.ItemTemplate = New Template1(ListItemType.Item) > > > case 2: > > > dl.ItemTemplate = New Template2(ListItemType.Item) > > > and so on. > > > > > > -- > > > HTH, > > > Phillip Williams > > > http://www.societopia.net > > > http://www.webswapp.com > > > > > > > > > "Erik" wrote: > > > > > > > I accidentally posted before I could ask if there is a better way to have a > > > > dynamic repeater / datalist, headers and items? > > > > > > > > "Phillip Williams" wrote: > > > > > > > > > Hi Erik, > > > > > > > > > > The nested datalist that you described is only defined within the > > > > > RepeaterItem. You can access it through the RepeaterItem.Controls > > > > > collection, e.g., > > > > > > > > > > private void repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e) > > > > > { > > > > > if (e.Item.ItemType == ListItemType.Item ) > > > > > { > > > > > DataList dl = e.Item.FindControl ("DataList1"); > > > > > if (dl !=null) > > > > > { > > > > > //write code to assign the datasource for the datalist > > > > > } > > > > > } > > > > > } > > > > > > > > > > If you want to look at demos of using nested templated databound controls, I > > > > > have several samples: http://www.societopia.net/Samples/ > > > > > -- > > > > > HTH, > > > > > Phillip Williams > > > > > http://www.societopia.net > > > > > http://www.webswapp.com > > > > > > > > > > > > > > > "Erik" wrote: > > > > > > > > > > > Good Afternoon, I am trying to nest a DataList in a Repeater ItemTemplate, > > > > > > but am receiving a error stating that the datalist is not defined. The id is > > > > > > set for the DataList. Is this possible? > > > > > > > > > > > > The reason for this architecture is to dynamically create the column header > > > > > > labels and items for the Repeater / DataList. I am trying to use the same > > > > > > Repeater / DataList for several different dataset, all with different fields > > > > > > and different number of fields. Any other ideas are also greatly appreciated. |
|
|
|
|
|||
|
|||
| Phillip Williams |
|
|
|
| |
![]() |
| Thread Tools | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| setting SelectedIndex of DataList nested inside of DataList? | Les Caudle | ASP .Net | 0 | 05-02-2006 10:44 PM |
| GridView nested in DataList - refreshing corresponding DataList row after updating GridView row | H5N1 | ASP .Net | 0 | 04-26-2006 11:41 PM |
| Nested datalist - parent datalist refresh | schapopa | ASP .Net | 1 | 01-12-2006 03:56 PM |
| Repeater control with nested repeater | chris.reed@digus.com | ASP .Net | 0 | 02-11-2005 05:10 PM |
| Setting up a datalist control - Item_DataBound for a datalist in a datalist | Nevyn Twyll | ASP .Net | 8 | 09-09-2004 10:13 PM |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc..
SEO by vBSEO ©2010, Crawlability, Inc. |




