retrive preselected value in second drop down list from the first drop down list

retrive preselected value in second drop down list from the first drop down list

Post by weiwe » Sun, 07 Jan 2007 04:28:37

here is my scenario, I create a drop down list in itemtemplate.(that
drop down is created from db), after user
click edit command, my ideal plan is have another drop down list in
edititemtemplate with preselected value from
the previous drop down list, so far I can only achieved with the
regular drop down list in edititemtemplate with no preselected value
from previous one. anyone can help me.
thanks in advance. below is my current sample code.

<%@ Page Language="VB" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<script language="VB" runat="server">

        Dim objConnection As SqlConnection
        Dim myDataReader As SqlDataReader

    Sub Page_Load(Sender As Object, E As EventArgs)
                ' Set up our connection.
        objConnection = New SqlConnection("Data Source=sql;Initial
Catalog=pub;User Id=xxx;Password=xxxxx;")


                If Not IsPostBack Then
        End If

    End Sub

        Sub LoadDataFromDB()
        Dim objCommand As SqlCommand

        ' Create new command object passing it our SQL query and
telling it which connection to use.
        objCommand = New SqlCommand("SELECT * FROM Educationtraining
WHERE Username = 'weifon888';", objConnection)

                ' Open the connection, execute the command, and close the connection.
                myDataReader =
        End Sub

    Sub DataBindGrid()
        DBEditDataGrid.DataSource = myDataReader
    End Sub

    Sub DBEditDataGrid_Edit(Sender As Object, E As
        DBEditDataGrid.EditItemIndex = E.Item.ItemIndex
    End Sub

    Sub DBEditDataGrid_Cancel(Sender As Object, E As
        DBEditDataGrid.EditItemIndex = -1
    End Sub

    Sub DBEditDataGrid_Update(ByVal Sender As Object, ByVal E As
        ' Since the textboxes are autogenerated we don't know their
names, but we do know their positions.
        Dim Tempdroplist As DropDownList
        Dim Tempdropvalue As String

        Dim strUser As String = E.Item.Cells(0).Text
        Dim certother As TextBox = E.Item.Cells(1).Controls(0)
        Dim strcertother = Replace(certother.Text, "'", "`")

        Tempdroplist = E.Item.FindControl("teachercert")
        Tempdropvalue = Tempdroplist.SelectedItem.Value


        ' Update the appropriate record in our database.
        Dim objCommand As SqlCommand
        Dim strSQLQuery As String

        ' Build our update command.
        strSQLQuery = "UPDATE Educationtraining SET Certother = '" &
strcertother & "' WHERE Username = 'johndoe'"

        ' Create new command object passing it our SQL query and
telling it which connection to use.
        objCommand = New SqlCommand(strSQLQuery, objConnection)

        ' Close our open DataReader

            ' Execute the command

        Catch Ex As Exception

        End Try

        ' Refresh our copy of the data

        ' Reset our current edit item and rebind the grid
        DBEditDataGrid.EditItemIndex = -1
    End Sub


<title>Modify User Profile</title>
<script language="javascript" type="text/javascript">

<form id="Form1" runat="server">

<asp:DataGrid id="DBEditDataGrid" runat="server"
        BorderWidth = "1px" CellSpacing = "2" CellPadding = "2"
        HeaderStyle-Font-Bold = "True"

        OnEditCommand   = "DBEditDataGrid_Edit"
        OnCancelCommand = "DBEditDataGrid_Cancel"
        OnUpdateCommand = "DBEditDataGrid_Update"

        AutoGenerateColumns = "False"

                <asp:BoundColumn HeaderText="Username"   DataField="Username"
ReadOnly="True" />
                <asp:BoundColumn HeaderText="Cert Other"      DataField="Certother"

            <asp:TemplateColumn FooterText="test" HeaderText="dropdown">

                    <asp:DropDownList ID="teachercert" runat="server">


                <asp:DropDownList ID="DropDownList1" runat="server"
DataValueField="Teachercert" Enabled="False" AutoPostBack="True">
ID="SqlDataSource1" runat="server" ConnectionString="<%$
ConnectionStrings:testConnectionString %>"
                    SelectCommand="SELECT [Teachercert] FROM

                        HeaderText = "Edit"
                        EditText   = "Edit &amp; Continue"
                        CancelText = "Cancel"
                        UpdateText = "Update &amp; Continue"

    <HeaderStyle Font-Bold="True" />



1. Newbee ASP.Net, generate a list of drop down list boxes!

Hi Guys,

I have started looking into ASP.Net. I have played with Web Forms a little
bit and have to admit it's quite a paradigm shift!

Nifty too, is the ease of input processing with client-side events
automatically tied to server-side handlers!

However I'm a bit at a loss when I'm trying to reproduce something I did
before ASP.Net i.e. produce a number of related drop down list boxes
dynamically in a table cell. The number ranges from zero to a maximum of say

In ASP, I would place ASP server side script to generate all the html in one

With VB 6.0 WebClasses, I would use a tag which would then be replaced when
the template is processed.

How do we achieve this in ASP.Net?
Note: I need to be able to dynamically change the number of ddlbs in the set
as well as their contents.

With ASP.Net, it seems to me we need to have a control beforehand in the
table cell.

Thanks for any pointers.


2. Blind CC

3. Adding value of drop down list from tableadapter into Guid provideruserkey for update

4. Toshiba 3601 + SB16 SCSI-2 = can't read CD-R

5. drop-down list not retaining selected value

6. Network connections not connecting at start-up

7. Reading and filling value on select index change of drop down list In DetailsView

8. App Center/MSMQ/CLB

9. Drop Down Lists & NULL values

10. Drop-down list SelectedItem has no value after selection

11. Populating a drop down list with text and value

12. Drop Down List Box SelectedItem.Value

13. Drop Down List Not Picking Up Selected Values