Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Composit control with Listbox problem

Reply
Thread Tools

Composit control with Listbox problem

 
 
SAL
Guest
Posts: n/a
 
      09-14-2009
Hi,
I am developing a composit control (inheriting from that class) that uses
another class that inherits from ListBox.
My composite control has a value called GridViewID which takes the id of a
GridView and if it is not null, takes the columns and lists them in the
listbox so the user can select which columns to export (to a CSV file for
Excel).

When my composit control load, it's executing the code that adds the items
to the listbox but the items are not showing up on the page that hosts the
composite control. It's probably something very simple but I can't seem to
make the items display in the listbox.

P.S. there's not much error checking in the code yet as I'm just trying to
get the items in the list to display at this point. Any help is much
appreciated.

Here's my CreateChildControls override:
Protected Overrides Sub CreateChildControls()
Controls.Clear()
list = New ExportColumnsList
list.ID = "lstExport"
list.GridviewID = Me.GridviewID
list.ContainerPanelID = Me.ContainerPanelID
btnSubmit = New Button()
btnSubmit.ID = "btnSubmit"
btnSubmit.Text = ButtonText
AddHandler btnSubmit.Click, AddressOf btnSubmit_Click
Me.Controls.Add(list)
Me.Controls.Add(btnSubmit)
End Sub

Protected Overrides Sub RecreateChildControls()
EnsureChildControls()
End Sub

Private Sub ColumnExportTool_PreRender(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.PreRender
LoadList()
End Sub

Protected Sub LoadList()
If ContainerPanelID <> String.Empty Then
If GridviewID <> String.Empty Then
list.GetGVInContainer(GridviewID, ContainerPanelID)
End If
ElseIf GridviewID <> String.Empty Then
list.GetGVColumns(GridviewID)
End If
End Sub

From the ExportColumnList class:

Protected Friend Sub GetGVColumns(ByVal id As String)
Dim gv As GridView = Page.FindControl(id)
GetColumns(gv)
End Sub

Protected Friend Sub GetGVInContainer(ByVal gridId As String, ByVal panelId
As String)
Dim gv As GridView = Nothing
Dim ctl As Control = Page.FindControl(panelId)
If Not ctl Is Nothing Then
gv = ctl.FindControl(gridId)
GetColumns(gv)
End If
End Sub

Private Sub GetColumns(ByVal gv As GridView)
Dim li As ListItem
If Not gv Is Nothing Then
Dim dcfc As DataControlFieldCollection
dcfc = gv.Columns
For Each dcf As DataControlField In dcfc
If dcf.Visible Then
li = New ListItem
li.Text = dcf.HeaderText
li.Value = dcf.SortExpression
Items.Add(li)
End If
Next
End If
End Sub

S


 
Reply With Quote
 
 
 
 
Allen Chen [MSFT]
Guest
Posts: n/a
 
      09-15-2009
Hi,

>When my composit control load, it's executing the code that adds the items
>to the listbox but the items are not showing up on the page that hosts the
>composite control. It's probably something very simple but I can't seem to
>make the items display in the listbox.


I'm willing to help but I think I need a repro project to troubleshoot this
issue.

Could you send me a demo that can reproduce this problem? My email is
http://www.velocityreviews.com/forums/(E-Mail Removed)se update here after sending the project in case I
missed that email.

Regards,
Allen Chen
Microsoft Online Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(E-Mail Removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subs...#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subs.../aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

 
Reply With Quote
 
 
 
 
SAL
Guest
Posts: n/a
 
      09-15-2009
Allen,
thank you. I've send you a couple of projects that repro the issue.

SAL

"Allen Chen [MSFT]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi,
>
>>When my composit control load, it's executing the code that adds the items
>>to the listbox but the items are not showing up on the page that hosts the
>>composite control. It's probably something very simple but I can't seem to
>>make the items display in the listbox.

>
> I'm willing to help but I think I need a repro project to troubleshoot
> this
> issue.
>
> Could you send me a demo that can reproduce this problem? My email is
> (E-Mail Removed)se update here after sending the project in case
> I
> missed that email.
>
> Regards,
> Allen Chen
> Microsoft Online Support
>
> Delighting our customers is our #1 priority. We welcome your comments and
> suggestions about how we can improve the support we provide to you. Please
> feel free to let my manager know what you think of the level of service
> provided. You can send feedback directly to my manager at:
> (E-Mail Removed).
>
> ==================================================
> Get notification to my posts through email? Please refer to
> http://msdn.microsoft.com/en-us/subs...#notifications.
>
> Note: MSDN Managed Newsgroup support offering is for non-urgent issues
> where an initial response from the community or a Microsoft Support
> Engineer within 2 business day is acceptable. Please note that each follow
> up response may take approximately 2 business days as the support
> professional working with you may need further investigation to reach the
> most efficient resolution. The offering is not appropriate for situations
> that require urgent, real-time or phone-based interactions. Issues of this
> nature are best handled working with a dedicated Microsoft Support
> Engineer
> by contacting Microsoft Customer Support Services (CSS) at
> http://msdn.microsoft.com/en-us/subs.../aa948874.aspx
> ==================================================
> This posting is provided "AS IS" with no warranties, and confers no
> rights.
>



 
Reply With Quote
 
William Niver
Guest
Posts: n/a
 
      09-15-2009

Howdy!

One of the major issues is that you should be using "MyBase", not "Me" and
"MyBase.Controls.Clear()" instead of just "Controls.Clear()".

Example:

Protected Overrides Sub CreateChildControls()
MyBase.Controls.Clear()
<Other stuff>
MyBase.Controls.Add(list)
MyBase.Controls.Add(btnSubmit)
End Sub

Hope that helps!

William

"SAL" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi,
> I am developing a composit control (inheriting from that class) that uses
> another class that inherits from ListBox.
> My composite control has a value called GridViewID which takes the id of a
> GridView and if it is not null, takes the columns and lists them in the
> listbox so the user can select which columns to export (to a CSV file for
> Excel).
>
> When my composit control load, it's executing the code that adds the items
> to the listbox but the items are not showing up on the page that hosts the
> composite control. It's probably something very simple but I can't seem to
> make the items display in the listbox.
>
> P.S. there's not much error checking in the code yet as I'm just trying to
> get the items in the list to display at this point. Any help is much
> appreciated.
>
> Here's my CreateChildControls override:
> Protected Overrides Sub CreateChildControls()
> Controls.Clear()
> list = New ExportColumnsList
> list.ID = "lstExport"
> list.GridviewID = Me.GridviewID
> list.ContainerPanelID = Me.ContainerPanelID
> btnSubmit = New Button()
> btnSubmit.ID = "btnSubmit"
> btnSubmit.Text = ButtonText
> AddHandler btnSubmit.Click, AddressOf btnSubmit_Click
> Me.Controls.Add(list)
> Me.Controls.Add(btnSubmit)
> End Sub
>
> Protected Overrides Sub RecreateChildControls()
> EnsureChildControls()
> End Sub
>
> Private Sub ColumnExportTool_PreRender(ByVal sender As Object, ByVal e As
> System.EventArgs) Handles Me.PreRender
> LoadList()
> End Sub
>
> Protected Sub LoadList()
> If ContainerPanelID <> String.Empty Then
> If GridviewID <> String.Empty Then
> list.GetGVInContainer(GridviewID, ContainerPanelID)
> End If
> ElseIf GridviewID <> String.Empty Then
> list.GetGVColumns(GridviewID)
> End If
> End Sub
>
> From the ExportColumnList class:
>
> Protected Friend Sub GetGVColumns(ByVal id As String)
> Dim gv As GridView = Page.FindControl(id)
> GetColumns(gv)
> End Sub
>
> Protected Friend Sub GetGVInContainer(ByVal gridId As String, ByVal
> panelId As String)
> Dim gv As GridView = Nothing
> Dim ctl As Control = Page.FindControl(panelId)
> If Not ctl Is Nothing Then
> gv = ctl.FindControl(gridId)
> GetColumns(gv)
> End If
> End Sub
>
> Private Sub GetColumns(ByVal gv As GridView)
> Dim li As ListItem
> If Not gv Is Nothing Then
> Dim dcfc As DataControlFieldCollection
> dcfc = gv.Columns
> For Each dcf As DataControlField In dcfc
> If dcf.Visible Then
> li = New ListItem
> li.Text = dcf.HeaderText
> li.Value = dcf.SortExpression
> Items.Add(li)
> End If
> Next
> End If
> End Sub
>
> S
>


 
Reply With Quote
 
Allen Chen [MSFT]
Guest
Posts: n/a
 
      09-16-2009
Hi Steve,

>thank you. I've send you a couple of projects that repro the issue.


Thanks for your project. Please set breakpoints to debug in the following
method:

Private Sub GetColumns(ByVal gv As GridView)
Dim li As ListItem

If Not gv Is Nothing Then
Dim dcfc As DataControlFieldCollection
dcfc = gv.Columns
For Each dcf As DataControlField In dcfc
If dcf.Visible Then
li = New ListItem
li.Text = dcf.HeaderText

li.Value = dcf.SortExpression
Items.Add(li)
End If
Next
End If
End Sub

If you check the value of gv.Columns.Count, you probably will see it's 0.
This is because the auto-generated columns of GridView is not added in the
Columns collection of GridView. Only explicitly specified columns like
below are added into the collection. For example, the following code will
create two columns. However, GridView1.Columns.Count is 1.

<asp:GridView ID="GridView1" runat="server"
DataSourceID="ObjectDataSource1">
<Columns><asp:TemplateField HeaderText="Header">
<ItemTemplate>
<%#Container.DataItem %>
</ItemTemplate></asp:TemplateField></Columns>
</asp:GridView>

Another issue is the following two lines:

li.Text = dcf.HeaderText

li.Value = dcf.SortExpression

If you want to show some text in the ListBox, the dcf.HeaderText should not
be empty string. However, if you don't specify the HeaderText for, say, in
above code, TemplateField, you'll get empty string here. It's common
because developers can use HeaderTemplate to customize the Header.Therefore
you cannot see anything shown in the ListBox. The same to
dcf.SortExpression. If it's not specified, you'll get empty string.

So if you want to do so you probably need to change your code logic. For
instance you can try this way to get controls in GridView:

GridView.Controls(0) ------ Table

GridView.Controls(0).Controls(index) ------ GridViewRow

GridView.Controls(0).Controls(index).Controls(inde x) ------ TableCell. For
BoundField, we can use TableCell.Text to get the value.

GridView.Controls(0).Controls(index).Controls(inde x).Controls(index) ------
Controls in the TableCell

Regards,
Allen Chen
Microsoft Online Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(E-Mail Removed).

 
Reply With Quote
 
SAL
Guest
Posts: n/a
 
      09-16-2009
Allen,
thank you for your response. In the gridview I'm using in my test project,
the columns are not auto generated and I have set a break point in the
routine you suggested. It's showing the text of the header and sort
expression.

The other issue:
I knew that using the DatControlField to get this information could possibly
be problematic. However, I wasn't sure how to detect if I had a templated
field or not. I'll study your ideas for implementation.

Are you able to get items into the ListBox?

SAL

"Allen Chen [MSFT]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi Steve,
>
>>thank you. I've send you a couple of projects that repro the issue.

>
> Thanks for your project. Please set breakpoints to debug in the following
> method:
>
> Private Sub GetColumns(ByVal gv As GridView)
> Dim li As ListItem
>
> If Not gv Is Nothing Then
> Dim dcfc As DataControlFieldCollection
> dcfc = gv.Columns
> For Each dcf As DataControlField In dcfc
> If dcf.Visible Then
> li = New ListItem
> li.Text = dcf.HeaderText
>
> li.Value = dcf.SortExpression
> Items.Add(li)
> End If
> Next
> End If
> End Sub
>
> If you check the value of gv.Columns.Count, you probably will see it's 0.
> This is because the auto-generated columns of GridView is not added in the
> Columns collection of GridView. Only explicitly specified columns like
> below are added into the collection. For example, the following code will
> create two columns. However, GridView1.Columns.Count is 1.
>
> <asp:GridView ID="GridView1" runat="server"
> DataSourceID="ObjectDataSource1">
> <Columns><asp:TemplateField HeaderText="Header">
> <ItemTemplate>
> <%#Container.DataItem %>
> </ItemTemplate></asp:TemplateField></Columns>
> </asp:GridView>
>
> Another issue is the following two lines:
>
> li.Text = dcf.HeaderText
>
> li.Value = dcf.SortExpression
>
> If you want to show some text in the ListBox, the dcf.HeaderText should
> not
> be empty string. However, if you don't specify the HeaderText for, say, in
> above code, TemplateField, you'll get empty string here. It's common
> because developers can use HeaderTemplate to customize the
> Header.Therefore
> you cannot see anything shown in the ListBox. The same to
> dcf.SortExpression. If it's not specified, you'll get empty string.
>
> So if you want to do so you probably need to change your code logic. For
> instance you can try this way to get controls in GridView:
>
> GridView.Controls(0) ------ Table
>
> GridView.Controls(0).Controls(index) ------ GridViewRow
>
> GridView.Controls(0).Controls(index).Controls(inde x) ------ TableCell. For
> BoundField, we can use TableCell.Text to get the value.
>
> GridView.Controls(0).Controls(index).Controls(inde x).Controls(index) ------
> Controls in the TableCell
>
> Regards,
> Allen Chen
> Microsoft Online Support
>
> Delighting our customers is our #1 priority. We welcome your comments and
> suggestions about how we can improve the support we provide to you. Please
> feel free to let my manager know what you think of the level of service
> provided. You can send feedback directly to my manager at:
> (E-Mail Removed).
>



 
Reply With Quote
 
SAL
Guest
Posts: n/a
 
      09-16-2009
William,
thank you for your suggestion. I'll change the code.

SAL

"William Niver" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> Howdy!
>
> One of the major issues is that you should be using "MyBase", not "Me" and
> "MyBase.Controls.Clear()" instead of just "Controls.Clear()".
>
> Example:
>
> Protected Overrides Sub CreateChildControls()
> MyBase.Controls.Clear()
> <Other stuff>
> MyBase.Controls.Add(list)
> MyBase.Controls.Add(btnSubmit)
> End Sub
>
> Hope that helps!
>
> William
>
> "SAL" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> Hi,
>> I am developing a composit control (inheriting from that class) that uses
>> another class that inherits from ListBox.
>> My composite control has a value called GridViewID which takes the id of
>> a GridView and if it is not null, takes the columns and lists them in the
>> listbox so the user can select which columns to export (to a CSV file for
>> Excel).
>>
>> When my composit control load, it's executing the code that adds the
>> items to the listbox but the items are not showing up on the page that
>> hosts the composite control. It's probably something very simple but I
>> can't seem to make the items display in the listbox.
>>
>> P.S. there's not much error checking in the code yet as I'm just trying
>> to get the items in the list to display at this point. Any help is much
>> appreciated.
>>
>> Here's my CreateChildControls override:
>> Protected Overrides Sub CreateChildControls()
>> Controls.Clear()
>> list = New ExportColumnsList
>> list.ID = "lstExport"
>> list.GridviewID = Me.GridviewID
>> list.ContainerPanelID = Me.ContainerPanelID
>> btnSubmit = New Button()
>> btnSubmit.ID = "btnSubmit"
>> btnSubmit.Text = ButtonText
>> AddHandler btnSubmit.Click, AddressOf btnSubmit_Click
>> Me.Controls.Add(list)
>> Me.Controls.Add(btnSubmit)
>> End Sub
>>
>> Protected Overrides Sub RecreateChildControls()
>> EnsureChildControls()
>> End Sub
>>
>> Private Sub ColumnExportTool_PreRender(ByVal sender As Object, ByVal e As
>> System.EventArgs) Handles Me.PreRender
>> LoadList()
>> End Sub
>>
>> Protected Sub LoadList()
>> If ContainerPanelID <> String.Empty Then
>> If GridviewID <> String.Empty Then
>> list.GetGVInContainer(GridviewID, ContainerPanelID)
>> End If
>> ElseIf GridviewID <> String.Empty Then
>> list.GetGVColumns(GridviewID)
>> End If
>> End Sub
>>
>> From the ExportColumnList class:
>>
>> Protected Friend Sub GetGVColumns(ByVal id As String)
>> Dim gv As GridView = Page.FindControl(id)
>> GetColumns(gv)
>> End Sub
>>
>> Protected Friend Sub GetGVInContainer(ByVal gridId As String, ByVal
>> panelId As String)
>> Dim gv As GridView = Nothing
>> Dim ctl As Control = Page.FindControl(panelId)
>> If Not ctl Is Nothing Then
>> gv = ctl.FindControl(gridId)
>> GetColumns(gv)
>> End If
>> End Sub
>>
>> Private Sub GetColumns(ByVal gv As GridView)
>> Dim li As ListItem
>> If Not gv Is Nothing Then
>> Dim dcfc As DataControlFieldCollection
>> dcfc = gv.Columns
>> For Each dcf As DataControlField In dcfc
>> If dcf.Visible Then
>> li = New ListItem
>> li.Text = dcf.HeaderText
>> li.Value = dcf.SortExpression
>> Items.Add(li)
>> End If
>> Next
>> End If
>> End Sub
>>
>> S
>>

>



 
Reply With Quote
 
SAL
Guest
Posts: n/a
 
      09-16-2009
Okay, so I've made some of the suggested changes to my code, i.e., change
me.contorls.add to MyBase.Controls.Add. I have not changed the code to what
Allen suggested because my gridview's columns collection is not empty, the
HeaderText is not empty and the SortExpression is not empty. I realize that
I'll need to change the way that I'm getting the text for the column, the
way the text appears in the gridview and the actually column from the data
source differently than I am now. I'm just trying to get stuff to show up in
the ListBox right now.

I also overrode the OnPreRender method of the CompositControl but the
listbox is still not displaying the text. Any help on getting the Listbox to
display the text I'm asking it to display would be very helpful. Heck, at
this point I could even hard code some text in there and it wouldn't display
so I'm still doing something wrong on this...
Thanks in advance for any help I recieve. I truely appreciate it.

Protected Overrides Sub OnPreRender(ByVal e As System.EventArgs)
LoadList()
MyBase.OnPreRender(e)
End Sub

SAL

"SAL" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi,
> I am developing a composit control (inheriting from that class) that uses
> another class that inherits from ListBox.
> My composite control has a value called GridViewID which takes the id of a
> GridView and if it is not null, takes the columns and lists them in the
> listbox so the user can select which columns to export (to a CSV file for
> Excel).
>
> When my composit control load, it's executing the code that adds the items
> to the listbox but the items are not showing up on the page that hosts the
> composite control. It's probably something very simple but I can't seem to
> make the items display in the listbox.
>
> P.S. there's not much error checking in the code yet as I'm just trying to
> get the items in the list to display at this point. Any help is much
> appreciated.
>
> Here's my CreateChildControls override:
> Protected Overrides Sub CreateChildControls()
> Controls.Clear()
> list = New ExportColumnsList
> list.ID = "lstExport"
> list.GridviewID = Me.GridviewID
> list.ContainerPanelID = Me.ContainerPanelID
> btnSubmit = New Button()
> btnSubmit.ID = "btnSubmit"
> btnSubmit.Text = ButtonText
> AddHandler btnSubmit.Click, AddressOf btnSubmit_Click
> Me.Controls.Add(list)
> Me.Controls.Add(btnSubmit)
> End Sub
>
> Protected Overrides Sub RecreateChildControls()
> EnsureChildControls()
> End Sub
>
> Private Sub ColumnExportTool_PreRender(ByVal sender As Object, ByVal e As
> System.EventArgs) Handles Me.PreRender
> LoadList()
> End Sub
>
> Protected Sub LoadList()
> If ContainerPanelID <> String.Empty Then
> If GridviewID <> String.Empty Then
> list.GetGVInContainer(GridviewID, ContainerPanelID)
> End If
> ElseIf GridviewID <> String.Empty Then
> list.GetGVColumns(GridviewID)
> End If
> End Sub
>
> From the ExportColumnList class:
>
> Protected Friend Sub GetGVColumns(ByVal id As String)
> Dim gv As GridView = Page.FindControl(id)
> GetColumns(gv)
> End Sub
>
> Protected Friend Sub GetGVInContainer(ByVal gridId As String, ByVal
> panelId As String)
> Dim gv As GridView = Nothing
> Dim ctl As Control = Page.FindControl(panelId)
> If Not ctl Is Nothing Then
> gv = ctl.FindControl(gridId)
> GetColumns(gv)
> End If
> End Sub
>
> Private Sub GetColumns(ByVal gv As GridView)
> Dim li As ListItem
> If Not gv Is Nothing Then
> Dim dcfc As DataControlFieldCollection
> dcfc = gv.Columns
> For Each dcf As DataControlField In dcfc
> If dcf.Visible Then
> li = New ListItem
> li.Text = dcf.HeaderText
> li.Value = dcf.SortExpression
> Items.Add(li)
> End If
> Next
> End If
> End Sub
>
> S
>



 
Reply With Quote
 
Allen Chen [MSFT]
Guest
Posts: n/a
 
      09-17-2009
Hi Steve,

>Are you able to get items into the ListBox?


Yes I am. I've sent you an email with a working sample. In the ListBox you
can see the header. Could you compare your code with mine to see what the
difference is?


Regards,
Allen Chen
Microsoft Online Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(E-Mail Removed).

 
Reply With Quote
 
Allen Chen [MSFT]
Guest
Posts: n/a
 
      09-24-2009
Hi Steve,

>Are you able to get items into the ListBox?


Do you have any progress on this issue? I've sent two emails to you but got
no reply so far. Please update here if it still doesn't work.


Regards,
Allen Chen
Microsoft Online Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(E-Mail Removed).

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Composit control and property value after postback BB ASP .Net 1 11-16-2005 08:40 AM
How do I move all items in a listbox to another listbox kent ASP .Net 1 05-03-2004 12:17 AM
click listbox and refresh another listbox DC Gringo ASP .Net 0 04-06-2004 02:13 AM
Rendering non-composit Control MartinL ASP .Net Building Controls 2 11-13-2003 10:00 AM
Re: now desparate! - 1st listbox contents disappears when 2nd listbox appears? blenderdude ASP .Net 0 08-03-2003 10:18 PM



Advertisments