开发者

Drop Down List does not clear

I created an asp.net application that fills a table with information from a database, choose a value in a drop down list and update the database. For some reason now when I go to the next date and all of the information displays correctly, but the value in the drop down list does not. Because of this, say the first page has 3 rows and the second page has 10. The first 3 rows on the second page will have the same value in the drop down list as the previous page, everything else is changed though. Here is the code used for creating the drop开发者_如何学JAVA down list on every row:

    tc = new TableCell();
                    tc.BorderWidth = 1;
                    String rteNum = reader.GetValue(fCount - 1).ToString().TrimStart('R');

                    //this output displays the correct value
                    System.Diagnostics.Trace.WriteLine(rteNum.Trim());

                    ddl = new DropDownList();
                    ddl.ID = "route" + index;
                    ddl.Attributes["runat"] = "server";
                    ddl.Attributes["onblur"] = "refresh()";

                    ListItem item;
                    for (int i = 1; i <= 32; i++)
                    {
                        item = new ListItem();
                        item.Text = i.ToString();
                        item.Value = i.ToString();
                        if (i.ToString().Equals(rteNum.Trim()))
                        {
                            item.Selected = true;
                        }
                        ddl.Items.Add(item);

                    }
                    list.Add(ddl);

                    tc.Controls.Add(ddl);               
                    tc.ID = "tblr" + index;
                    tr.Cells.Add(tc);

                    tbl1.Rows.Add(tr);

EDIT:

The problem is I need to use the changed values if the user presses a button, however when the data is changed the information has to be dropped. So basically I need to display the information with a drop down list then update the database.


One way is to do it in mark-up like this:

<asp:DropDownList ID="ddl_1" runat="server" AutoPostBack="True" 
   DataSourceID="ds_1" DataTextField="myText" 
   DataValueField="myValue" />

   <asp:SqlDataSource ID="ds_1" runat="server" 
      ConnectionString="<%$ ConnectionStrings:myConnection %>" 
      <!--this is if you use a stored procedure-->
      SelectCommand="dbo.myProcedure" SelectCommandType="StoredProcedure">
      <!--this is if you use direct sql (less secure -- be careful with parameters)-->
      SelectCommand="SELECT field1 as myValue, field2 as myText FROM myTable">
   </asp:SqlDataSource>

So when you want to set the selected value based on a database value (in response to a button-click, etc., you might use a sub like this in your code-behind:

Private Sub SELECT_whatever()
        Dim myCommand As New SqlCommand("dbo.SELECT_whatever", myConnection)
        With myCommand
            .CommandType = CommandType.StoredProcedure
            With .Parameters
                .Clear()
                .AddWithValue("myParameter", me.txtwhateverfield.text)
            End With
        End With
        Try
            myConnection.Open()
            myReader = myCommand.ExecuteReader
            While myReader.Read
                If Not IsDBNull(myReader("field1")) Then
                    Me.ddl_instructor.SelectedValue = myReader("field1")
                End If
                Me.lbl_RecordID.Text = "Record ID: " & myReader("recordID")
               -- etc. -- for all of the textboxes, labels, etc. on your page
            End While
        Catch ex As Exception
            Response.Write(ex.Message)
        Finally
            myConnection.Close()
        End Try

As an alternative to using a datasoruce control in mark-up, you can use the sub above, but instead...

  1. create a data set
  2. create a data adapter
  3. call .fill on the data adapter for a table in the data set
  4. set the dropdown list's data source to the data table
  5. call .databind() on the dropdown list.

Here's an example: http://support.microsoft.com/kb/306574 (CTRL+F for "databind" -- it's down in item 4)


It sounds like you're expecting one of your 32 option items in your DropDownList to be selected. Suspect that rteNum.Trim() doesn't have the exact value as 1 through 32.

To debug this, try to determine the value of rteNum

//pre-trim this val.
string rteNum = reader.GetValue(fCount - 1).ToString().TrimStart('R').Trim();
...

ddl.Attributes["name"] = rteNum;  //a random place/attr show the value of rteNum 
ddl.Attributes["runat"] = "server";
ddl.Attributes["onblur"] = "refresh()";
 ....
item.Value = i.ToString();
item.Selected = (i.ToString()==rteNum);

View your source after the page renders to find the value of your DropDownList's name attribute. Compare that against 1-32.


Not to criticize, but just as an observation...

You're doing a lot of work to populate, and then also to sync your drop-down list. You don't need to use a datareader as a middle-man. You could just go directly to the datasource. Calling databind() can populate the list, instead of doing a row-by-row insert from your datareader.

If you're interested in this approach, I can put up some sample code.

Otherwise, as @p.cambell says, as you're doing it currently, your selected property is only set when rteNum has a value.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜