Hello Ana... if u can send the complete page to me. i wl try to handle it out and revert back to u as soon as i can.... i am mostly linked to C# code and not that good in vb code but still know a bit of it. so u can mail me the page at [email protected] and i wl revert back to u soon...
Have a nice time...... Regards Mujhtaba On 5/26/10, Ana <[email protected]> wrote: > Hi Cerebrus, > > This is how my code looks like right now: > > <asp:DropDownList ID="myDropDownList" runat="server" > OnDataBinding="myDropDownList_DataBinding" > OnDataBound="myDropDownList_DataBound" > SelectedValue='<%# Bind("myID") %>' > DataTextField="Name" > DataValueField="myID" /> > > And in the code behind: > > Dim ddlBound As Boolean = False > Protected Sub myDropDownList_DataBinding(ByVal sender As Object, > ByVal e As EventArgs) > Dim myDropDownList As DropDownList = sender > Dim myTable As New DataTable > > If Not ddlBound Then > ddlBound = True > Dim DBConnection As New > SqlConnection(ConfigurationManager.ConnectionStrings("myServer").ConnectionString) > '--- QUERY MODIFIED --- > Dim query As String = "SELECT ... FROM ... WHERE ..." > Dim cmd As New SqlCommand(query , DBConnection) > > myTable .Columns.Add("myID") > myTable .Columns.Add("Name") > Dim adapter As New SqlDataAdapter > > adapter.SelectCommand = photographersCMD > adapter.Fill(myTable ) > Dim newRow As DataRow = myTable .NewRow > newRow("myID") = "" > newRow("Name") = "-- Select a photographer --" > myTable .Rows.InsertAt(newRow, 0) > > myDropDownList.DataSource = myTable > myDropDownList.DataBind() ---> At this point, before > handling the DataBound event, an error message is thrown saying the > DDL has a value that is not in the list of items > End If > End Sub > > 'If I remove SelectedValue='<%# Bind("myID") %>' in the design page, > there will be no error message being thrown in the DataBind event and > it will handle the DataBound event. > Protected Sub myDropDownList_DataBound(ByVal sender As Object, > ByVal e As System.EventArgs) > Dim drv As DataRowView = myFormView.DataItem > Dim myID As String = drv("myID").ToString.TrimEnd() > Dim ddl As DropDownList = sender > > If Not IsDBNull(myID ) Then > ddl.SelectedValue = myID > Dim selectedItem As ListItem = > ddl.Items.FindByValue(myID ) > If selectedItem Is Nothing Then > ddl.SelectedIndex = 0 > Else > selectedItem.Selected = True > End If > ddl.SelectedValue = myID ----> when checking the selected > value after this line, the new value is not set; ddl.SelectedValue = > "" > End If > End Sub > > I checked the DropDownList and it's being bound correctly. I used test > values for myID, choosing these values from the values I know that are > in the DDL (I made an iteration and double checked if the value was > there - it was). Even with these test values, ddl.SelectedValue = myID > doesn't work. After leaving the DataBound routine, it goes to my > routine for binding the FormView and an exception in thrown in the > line: myFormView.DataBind() saying that the selected value in the DDL > doesn't exist in the list of items. > > Any suggestions of what is happening? > > Thanks, > > Ana > > On May 25, 11:30 pm, Cerebrus <[email protected]> wrote: >> I tried to reply yesterday, but the reply could not be posted due to >> network issues... so here goes again. >> >> I think that setting the selected value within the control's databound >> event may not be a good place. Instead, try to do it in the Parent >> control's (a FormView in this case) databound event. I haven't had a >> chance to try a sample, but this is one possibility that comes to >> mind. >> >> If you post a complete sample (athttp://dotnetdevelopment.pastebin.com, >> for instance), maybe we can test and determine the exact cause of the >> problem. > -- Mujhtaba Here. May Allah bless you.
