Link to home
Start Free TrialLog in
Avatar of lynnton
lynnton

asked on

two DDL -follow the leader

Hi,

Two drop down list,

ddloperatorid-contains id
ddloperatorname-contains name

How can we change the ddl selected value to the correct name or id on the other dll? (follow the leader)

i.e.
001 abc
002 efg

ddloperatorid-selected value is 002
then
ddloperatorname- should move to efg (viseversa)

Thanks.

                  Private Function GenerateSourceOper() As DataTable
                      Dim con As New SqlConnection("......")
                      Dim cmd As SqlCommand = con.CreateCommand()
                      cmd.CommandType = CommandType.StoredProcedure
                      cmd.CommandText = "SP_operator_id_and_name"
                      cmd.Parameters.Add(New SqlParameter("@RETURN_VALUE", SqlDbType.Int, 4, ParameterDirection.ReturnValue, False, CType(0, Byte), CType(0, Byte), "", DataRowVersion.Current, Nothing))
                      Dim da As New SqlDataAdapter(cmd)
                      Dim ds As New DataSet()
                      da.Fill(ds,"DataSetName")
                      Return ds.Tables("DataSetName")
                      cmd.dispose
                      cmd.parameters.clear
                      con.close
                  End function

                   Sub Page_Load(Sender As Object, E As EventArgs)
                     If Not Page.IsPostBack Then

                      ddloperatorid.Datasource=GenerateSourceOper().DefaultView
                      ddloperatorid.DataTextField = "supervisor id"
                      ddloperatorid.DataValueField = "supervisor name"

                      ddloperatorname.Datasource=GenerateSourceOper().DefaultView
                      ddloperatorname.DataTextField = "supervisor name"
                      ddloperatorname.DataValueField = "supervisor id"

                      Page.DataBind()
                      ddloperatorid.Items.Insert(0, "Select id")
                      ddloperatorname.Items.Insert(0, "Select name")
                      end if
                  End Sub


SOLUTION
Avatar of vladimir_kalashnikov
vladimir_kalashnikov

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lynnton
lynnton

ASKER

vladimir_kalashnikov,

auto post back is now set, can you kindly guide me in vb code ?

Thanks.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lynnton

ASKER

sriggumma,

Thanks so much.
lynnton,

If you do not want the page getting posted for each selection, you may use javascript like below:

1. Set AutoPostBack="False"  for the dropdownlists.
2. Define a Javascript block in aspx page like below:
----->
<script language="javascript">
   function SetNameDdlById()
   {
      document.getElementById("ddloperatorname").selectedIndex=document.getElementById("ddloperatorid").selectedIndex;
   }
</script>

3. In the code behind of the page, add this line to use the Javascript function.
----->
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        '..... YOUR REGULAR CODE .......
        ddloperatorid.Attributes.Add("onchange", "JavaScript:SetNameDdlById()")
    End Sub


Hope this helps you.
Avatar of lynnton

ASKER

sriggumma,

Will it affect my project? once i place the javascript?

where do I place the javascript code? top page?

Thanks.


<%@ Page Language="VB" Debug="true" %>
<%@ Register TagPrefix="wmx" Namespace="Microsoft.Matrix.Framework.Web.UI" Assembly="Microsoft.Matrix.Framework, Version=0.6.0.0, Culture=neutral, PublicKeyToken=6f763c9966660626" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClient" %>

Avatar of lynnton

ASKER

sriggumma,

When I change the order of the operatorid DDL to ASC

it was matching the index value for index value

ddloperatorname.SelectedIndex = ddloperatorid.SelectedIndex

how can we change it to select the proper name for that id ?


                      ddloperatorid.Datasource=GenerateSourceOperid().DefaultView <---------arrange by name
                      ddloperatorid.DataTextField = "supervisor id"
                      ddloperatorid.DataValueField = "supervisor name"
                      ddloperatorname.Datasource=GenerateSourceOpername().DefaultView <------arrange by id
                      ddloperatorname.DataTextField = "supervisor name"
                      ddloperatorname.DataValueField = "supervisor id"
   
                      Page.DataBind()

Thanks.
lynnton
>>> where do I place the javascript code? top page?
You can place the javascript anywhere in the aspx page. Ideally, place the code within the <head> and </head> tags.

>>> When I change the order of the operatorid DDL to ASC
Then replace
-----> ddloperatorname.SelectedIndex = ddloperatorid.SelectedIndex

with
-----> ddloperatorname.SelectedIndex = ddloperatorname.Items.IndexOf(ddloperatorname.Items.FindByText(ddloperatorid.SelectedValue))


Hope this gives the idea.