Composit control with Listbox problem

S

SAL

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
 
A

Allen Chen [MSFT]

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 address removed) 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 address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#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/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
W

William Niver

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
 
A

Allen Chen [MSFT]

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(index) ------ TableCell. For
BoundField, we can use TableCell.Text to get the value.

GridView.Controls(0).Controls(index).Controls(index).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 address removed).
 
S

SAL

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
 
S

SAL

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
 
A

Allen Chen [MSFT]

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 address removed).
 
A

Allen Chen [MSFT]

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 address removed).
 
S

SAL

As it turns out, this is an IE 7 rendering issue. Firefox renders the
control fine. I very much appreciate Allen's support on this issue. However,
I must express my dissatisfaction with IE, once again. Wasn't IE 7 supposed
to solve many of these rendering issues? The issue being that I was
specifying the width in the Render routine for the listbox at 100% (of the
td tag) rather than an absolute value. I'm told that IE 8 has resolved this
issue. However, our entire company just moved to IE 7 with little to no
chance of getting us to move to IE 8 at this time. Okay, so maybe this is a
rant and maybe I'm just being lazy by not checking which browser the client
is using.
If I'm not mistaken and I remember correctly, Firefox can have issues if you
specify an abosolute value for some controls. I have not tested this last
issue on the listbox however.
Okay, I guess I'm done with my rant now and I hope this post helps someone
else down the road.

Thanks again Allen for your support.

S
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,152
Members
46,697
Latest member
AugustNabo

Latest Threads

Top