Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 322
  • Last Modified:

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


0
lynnton
Asked:
lynnton
  • 4
  • 3
2 Solutions
 
vladimir_kalashnikovCommented:
its actually pretty easy.  first, you have to enable autopostback on both of the dropdowns (in their properties).  Then create the dropdown selecteditemchanged events for both of them (by double clicking them in visual studio).  Then in those methods take the selected text (or value if thats what you are using) and iterate through the other list to find the value that corresponds to that one, then select it.

You could probably find a way to do it without using postbacks, but using javascript instead.  It shouldn't be too difficult, but my javascript sucks, so i won't try to show you that way.
0
 
lynntonAuthor Commented:
vladimir_kalashnikov,

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

Thanks.
0
 
sriggummaCommented:
lynnton,

1. In aspx page, Set AutoPostBack="True" for first dropdownlist i.e., ddloperatorid
2. In code behind, write code like below:
----->
 Private Sub ddloperatorid_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ddloperatorid.SelectedIndexChanged
        ddloperatorname.SelectedIndex = ddloperatorid.SelectedIndex
    End Sub

Hope this gives you an idea.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
lynntonAuthor Commented:
sriggumma,

Thanks so much.
0
 
sriggummaCommented:
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.
0
 
lynntonAuthor Commented:
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" %>

0
 
lynntonAuthor Commented:
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.
0
 
sriggummaCommented:
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.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now