472,146 Members | 1,321 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,146 software developers and data experts.

Odd BUG with DropDownList in DataGrid

I've discovered a very odd bug when attempting to put a dropdown list in a
datagrid.

In the page PreRender step, the selected index of the datagrid is
successfully set during databinding. Yet, when the datagrid enters it's own
OnPreRender, the selected index has reverted to zero.

I created a debug version of the dropdown list which inherited from
dropdownlist and overrode the selected index property to trace.write
whenever it was changed. It showed no change other than the correct change
when databinding.

I have a functional work-around in the code that follows but always have
some trepidation when "patching over" a bug that I don't truly understand.
Could anyone grant me any enlightenment?

Public Class FixedDropDown
Inherits DropDownList

Private FixedSI

Public Overrides Property SelectedIndex() As Integer
Get
Return FixedSI
End Get
Set(ByVal Value As Integer)
MyBase.SelectedIndex = Value
FixedSI = Value
End Set
End Property

Protected Overrides Sub OnPreRender(ByVal e As System.EventArgs)
MyBase.SelectedIndex = FixedSI
MyBase.OnPreRender(e)
End Sub
End Class
Nov 20 '05 #1
4 1881
Hi Mark,

Based on my understanding you are developing an ASP.NET application, and on
the webform you add a datagrid and add the dropdownlist as the datagrid's
template item.
But the value of datagrid's selectedindex is different between the Page's
PreRender and the datagrid's PreRender event.
If so,
Here is my test code that I can not reproduce the problem.

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
SqlDataAdapter1.Fill(DataSet11)
DataGrid1.DataBind()
Me.DataGrid1.SelectedIndex = 10
End Sub

Private Sub DataGrid1_PreRender(ByVal sender As Object, ByVal e As
System.EventArgs) Handles DataGrid1.PreRender
Response.Write(Me.DataGrid1.SelectedIndex.ToString ()) 'the value is
10
End Sub

Private Sub Page_PreRender(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.PreRender
Response.Write(Me.DataGrid1.SelectedIndex.ToString ()) 'the value is
10
End Sub

And I am strange why you will mention the DropDownList here.
If I misunderstanding your meaning, please post here.
Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
--------------------
From: "Mark Waser" <mw****@mwaser.com>
Subject: Odd BUG with DropDownList in DataGrid
Date: Fri, 7 Nov 2003 11:06:48 -0500
Lines: 38
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
Message-ID: <OE**************@tk2msftngp13.phx.gbl>
Newsgroups: microsoft.public.dotnet.languages.vb
NNTP-Posting-Host: 12.154.72.87
Path: cpmsftngxa06.phx.gbl!cpmsftngxa09.phx.gbl!TK2MSFTN GP08.phx.gbl!tk2msftngp13.
phx.gblXref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.vb:154693
X-Tomcat-NG: microsoft.public.dotnet.languages.vb

I've discovered a very odd bug when attempting to put a dropdown list in a
datagrid.

In the page PreRender step, the selected index of the datagrid is
successfully set during databinding. Yet, when the datagrid enters it's ownOnPreRender, the selected index has reverted to zero.

I created a debug version of the dropdown list which inherited from
dropdownlist and overrode the selected index property to trace.write
whenever it was changed. It showed no change other than the correct change
when databinding.

I have a functional work-around in the code that follows but always have
some trepidation when "patching over" a bug that I don't truly understand.
Could anyone grant me any enlightenment?

Public Class FixedDropDown
Inherits DropDownList

Private FixedSI

Public Overrides Property SelectedIndex() As Integer
Get
Return FixedSI
End Get
Set(ByVal Value As Integer)
MyBase.SelectedIndex = Value
FixedSI = Value
End Set
End Property

Protected Overrides Sub OnPreRender(ByVal e As System.EventArgs)
MyBase.SelectedIndex = FixedSI
MyBase.OnPreRender(e)
End Sub
End Class


Nov 20 '05 #2
> But the value of datagrid's selectedindex is different between the Page's
PreRender and the datagrid's PreRender event.
I'm sorry, I wrote the wrong thing. I should have said that the
DROPDOWNLIST's selectedindex is different between the Page's PreRender and
the List's PreRender. The problem here being that the dropdownlist is
supposed to be set to a specific value (according to the data for a specific
item) during databinding (and it is) but that value is subsequently lost
before the user sees it.

Thanks.

Mark
"Peter Huang" <v-******@online.microsoft.com> wrote in message
news:sQ**************@cpmsftngxa06.phx.gbl... Hi Mark,

Based on my understanding you are developing an ASP.NET application, and on the webform you add a datagrid and add the dropdownlist as the datagrid's
template item.
But the value of datagrid's selectedindex is different between the Page's
PreRender and the datagrid's PreRender event.
If so,
Here is my test code that I can not reproduce the problem.

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
SqlDataAdapter1.Fill(DataSet11)
DataGrid1.DataBind()
Me.DataGrid1.SelectedIndex = 10
End Sub

Private Sub DataGrid1_PreRender(ByVal sender As Object, ByVal e As
System.EventArgs) Handles DataGrid1.PreRender
Response.Write(Me.DataGrid1.SelectedIndex.ToString ()) 'the value is 10
End Sub

Private Sub Page_PreRender(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.PreRender
Response.Write(Me.DataGrid1.SelectedIndex.ToString ()) 'the value is 10
End Sub

And I am strange why you will mention the DropDownList here.
If I misunderstanding your meaning, please post here.
Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
--------------------
From: "Mark Waser" <mw****@mwaser.com>
Subject: Odd BUG with DropDownList in DataGrid
Date: Fri, 7 Nov 2003 11:06:48 -0500
Lines: 38
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
Message-ID: <OE**************@tk2msftngp13.phx.gbl>
Newsgroups: microsoft.public.dotnet.languages.vb
NNTP-Posting-Host: 12.154.72.87
Path:

cpmsftngxa06.phx.gbl!cpmsftngxa09.phx.gbl!TK2MSFTN GP08.phx.gbl!tk2msftngp13. phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.vb:154693
X-Tomcat-NG: microsoft.public.dotnet.languages.vb

I've discovered a very odd bug when attempting to put a dropdown list in adatagrid.

In the page PreRender step, the selected index of the datagrid is
successfully set during databinding. Yet, when the datagrid enters it's

own
OnPreRender, the selected index has reverted to zero.

I created a debug version of the dropdown list which inherited from
dropdownlist and overrode the selected index property to trace.write
whenever it was changed. It showed no change other than the correct changewhen databinding.

I have a functional work-around in the code that follows but always have
some trepidation when "patching over" a bug that I don't truly understand.Could anyone grant me any enlightenment?

Public Class FixedDropDown
Inherits DropDownList

Private FixedSI

Public Overrides Property SelectedIndex() As Integer
Get
Return FixedSI
End Get
Set(ByVal Value As Integer)
MyBase.SelectedIndex = Value
FixedSI = Value
End Set
End Property

Protected Overrides Sub OnPreRender(ByVal e As System.EventArgs)
MyBase.SelectedIndex = FixedSI
MyBase.OnPreRender(e)
End Sub
End Class

Nov 20 '05 #3
Hi Mark,

I can not reproduce the problem. Here is my code.
After the page is displayed in IE,all the DropDownList in the datagrid
displayed the 10th item.

Public WithEvents dl As DropDownList
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
SqlDataAdapter1.Fill(DataSet11)
DataGrid1.DataBind()
End Sub

Private Sub DataGrid1_PreRender(ByVal sender As Object, ByVal e As
System.EventArgs) Handles DataGrid1.PreRender
Response.Write(dl.SelectedIndex)
End Sub

Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs) Handles
DataGrid1.ItemDataBound
Dim o As Object
o = CType(e.Item.FindControl("DropDownList1"), DropDownList)
If Not o Is Nothing Then
dl = o
dl.SelectedIndex = 10
End If
End Sub

Private Sub Page_PreRender(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.PreRender
Response.Write(dl.SelectedIndex)
End Sub

Private Sub dl_PreRender(ByVal sender As Object, ByVal e As
System.EventArgs) Handles dl.PreRender
Response.Write(dl.SelectedIndex)
End Sub

Can you post your code for me to reproduce the problem?
Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
--------------------
From: "Mark Waser" <mw****@mwaser.com>
References: <OE**************@tk2msftngp13.phx.gbl> <sQ**************@cpmsftngxa06.phx.gbl>Subject: Re: Odd BUG with DropDownList in DataGrid
Date: Mon, 10 Nov 2003 09:13:06 -0500
Lines: 124
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
Message-ID: <OZ**************@TK2MSFTNGP12.phx.gbl>
Newsgroups: microsoft.public.dotnet.languages.vb
NNTP-Posting-Host: 12.154.72.87
Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP12.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.vb:155524
X-Tomcat-NG: microsoft.public.dotnet.languages.vb
But the value of datagrid's selectedindex is different between the Page's PreRender and the datagrid's PreRender event.
I'm sorry, I wrote the wrong thing. I should have said that the
DROPDOWNLIST's selectedindex is different between the Page's PreRender and
the List's PreRender. The problem here being that the dropdownlist is
supposed to be set to a specific value (according to the data for a

specificitem) during databinding (and it is) but that value is subsequently lost
before the user sees it.

Thanks.

Mark
"Peter Huang" <v-******@online.microsoft.com> wrote in message
news:sQ**************@cpmsftngxa06.phx.gbl...
Hi Mark,

Based on my understanding you are developing an ASP.NET application, and

on
the webform you add a datagrid and add the dropdownlist as the datagrid's
template item.
But the value of datagrid's selectedindex is different between the Page's PreRender and the datagrid's PreRender event.
If so,
Here is my test code that I can not reproduce the problem.

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
SqlDataAdapter1.Fill(DataSet11)
DataGrid1.DataBind()
Me.DataGrid1.SelectedIndex = 10
End Sub

Private Sub DataGrid1_PreRender(ByVal sender As Object, ByVal e As
System.EventArgs) Handles DataGrid1.PreRender
Response.Write(Me.DataGrid1.SelectedIndex.ToString ()) 'the value

is
10
End Sub

Private Sub Page_PreRender(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.PreRender
Response.Write(Me.DataGrid1.SelectedIndex.ToString ()) 'the value

is
10
End Sub

And I am strange why you will mention the DropDownList here.
If I misunderstanding your meaning, please post here.
Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights. --------------------
>From: "Mark Waser" <mw****@mwaser.com>
>Subject: Odd BUG with DropDownList in DataGrid
>Date: Fri, 7 Nov 2003 11:06:48 -0500
>Lines: 38
>X-Priority: 3
>X-MSMail-Priority: Normal
>X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
>X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
>Message-ID: <OE**************@tk2msftngp13.phx.gbl>
>Newsgroups: microsoft.public.dotnet.languages.vb
>NNTP-Posting-Host: 12.154.72.87
>Path:

cpmsftngxa06.phx.gbl!cpmsftngxa09.phx.gbl!TK2MSFT NGP08.phx.gbl!tk2msftngp13

Nov 20 '05 #4
Hi Peter,

I've been dramatically refactoring my code and have now found that I can
get rid of the FixedDropDown class. I don't know what got rid of the
problem and really don't have time to trace it back now that it's solved.
Thank you for your assistance.

Mark

"Peter Huang" <v-******@online.microsoft.com> wrote in message
news:Wk*************@cpmsftngxa06.phx.gbl...
Hi Mark,

I can not reproduce the problem. Here is my code.
After the page is displayed in IE,all the DropDownList in the datagrid
displayed the 10th item.

Public WithEvents dl As DropDownList
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
SqlDataAdapter1.Fill(DataSet11)
DataGrid1.DataBind()
End Sub

Private Sub DataGrid1_PreRender(ByVal sender As Object, ByVal e As
System.EventArgs) Handles DataGrid1.PreRender
Response.Write(dl.SelectedIndex)
End Sub

Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs) Handles
DataGrid1.ItemDataBound
Dim o As Object
o = CType(e.Item.FindControl("DropDownList1"), DropDownList)
If Not o Is Nothing Then
dl = o
dl.SelectedIndex = 10
End If
End Sub

Private Sub Page_PreRender(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.PreRender
Response.Write(dl.SelectedIndex)
End Sub

Private Sub dl_PreRender(ByVal sender As Object, ByVal e As
System.EventArgs) Handles dl.PreRender
Response.Write(dl.SelectedIndex)
End Sub

Can you post your code for me to reproduce the problem?
Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
--------------------
From: "Mark Waser" <mw****@mwaser.com>
References: <OE**************@tk2msftngp13.phx.gbl>

<sQ**************@cpmsftngxa06.phx.gbl>
Subject: Re: Odd BUG with DropDownList in DataGrid
Date: Mon, 10 Nov 2003 09:13:06 -0500
Lines: 124
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
Message-ID: <OZ**************@TK2MSFTNGP12.phx.gbl>
Newsgroups: microsoft.public.dotnet.languages.vb
NNTP-Posting-Host: 12.154.72.87
Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP12.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.vb:155524
X-Tomcat-NG: microsoft.public.dotnet.languages.vb
But the value of datagrid's selectedindex is different between the Page's PreRender and the datagrid's PreRender event.


I'm sorry, I wrote the wrong thing. I should have said that the
DROPDOWNLIST's selectedindex is different between the Page's PreRender and
the List's PreRender. The problem here being that the dropdownlist is
supposed to be set to a specific value (according to the data for a

specific
item) during databinding (and it is) but that value is subsequently lost
before the user sees it.

Thanks.

Mark
"Peter Huang" <v-******@online.microsoft.com> wrote in message
news:sQ**************@cpmsftngxa06.phx.gbl...
Hi Mark,

Based on my understanding you are developing an ASP.NET application, and
on
the webform you add a datagrid and add the dropdownlist as the
datagrid's template item.
But the value of datagrid's selectedindex is different between the

Page's PreRender and the datagrid's PreRender event.
If so,
Here is my test code that I can not reproduce the problem.

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
SqlDataAdapter1.Fill(DataSet11)
DataGrid1.DataBind()
Me.DataGrid1.SelectedIndex = 10
End Sub

Private Sub DataGrid1_PreRender(ByVal sender As Object, ByVal e As
System.EventArgs) Handles DataGrid1.PreRender
Response.Write(Me.DataGrid1.SelectedIndex.ToString ()) 'the
valueis
10
End Sub

Private Sub Page_PreRender(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.PreRender
Response.Write(Me.DataGrid1.SelectedIndex.ToString ()) 'the
valueis
10
End Sub

And I am strange why you will mention the DropDownList here.
If I misunderstanding your meaning, please post here.
Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no

rights. --------------------
>From: "Mark Waser" <mw****@mwaser.com>
>Subject: Odd BUG with DropDownList in DataGrid
>Date: Fri, 7 Nov 2003 11:06:48 -0500
>Lines: 38
>X-Priority: 3
>X-MSMail-Priority: Normal
>X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
>X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
>Message-ID: <OE**************@tk2msftngp13.phx.gbl>
>Newsgroups: microsoft.public.dotnet.languages.vb
>NNTP-Posting-Host: 12.154.72.87
>Path:


cpmsftngxa06.phx.gbl!cpmsftngxa09.phx.gbl!TK2MSFT NGP08.phx.gbl!tk2msftngp13

Nov 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Shiju Poyilil | last post: by
1 post views Thread by m3ckon | last post: by
3 posts views Thread by Tim::.. | last post: by
reply views Thread by Daniel Doyle | last post: by
reply views Thread by Saiars | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.