View Full Version : Changing values in multiple Dropdown list

11-20-2012, 09:10 PM

I have a grid view that has multiple drop down list showing rankings (ex:1-5). Users are required to adjust these ranking by selecting values from the drop down list.

Requirement is to change values in other drop down list in the gridview based on the value selected by the user.

Ex: If user, in the grid view changes drop down value at row 4 from 4 to 1, then my code should loop through all the drodown list in the gridview and change values in the dropdownlist accordingly (i.e)
1) if 4 changed to 1 then , 1-->2, 2-->3 and 3-->4
2) 5 changed to 2 then, 2-->3, 3-->4 and drop down list with value 4 becomes 5

I tried playing around with this. Below is what i could do:


<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default2.aspx.vb" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
<script language="javascript" type="text/javascript">
function showData(dropdown) {
//get dropdownlist
var myindex = dropdown.selectedIndex;
var SelValue = dropdown.options[myindex].value;

//get the row of the drop down list
var row = getParentRow(dropdown);
var newRate = 0;
//get gridview
var gridViewCtlId = '<%=GridView1.ClientID%>';
var grid = document.getElementById(gridViewCtlId);
var gridLength = grid.rows.length;
//set value in the cell of grid view

for (var i=1;i<gridLength;i++){
if (i<row.rowIndex){
var selectObject=grid.rows[i].cells[1].getElementsByTagName("*")[0];
var oldSelValue=selectObject.options[selectObject.selectedIndex].value;
if (oldSelValue=SelValue){
for (var j=0;j<selectObject.options.length;j++){
function getParentRow(obj) {
while (obj.tagName != "TR") {
if (isFireFox()) {
obj = obj.parentNode;
else {
obj = obj.parentElement;
return obj;
function isFireFox() {
return navigator.appName == "Netscape";


<form id="form1" runat="server">
<input id="hiddenVal" type="hidden" />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
<asp:BoundField DataField="Request Name" HeaderText="Request Name" />
<asp:TemplateField HeaderText="Ranking">
<asp:DropDownList ID="DropDownList1" runat="server" onchange="showData(this)" selectedvalue='<%# Bind("Ranking") %>'>
<asp:ListItem Selected="True" Text="1" Value="1"></asp:ListItem>
<asp:ListItem Text="2" Value="2"></asp:ListItem>
<asp:ListItem Text="3" Value="3"></asp:ListItem>
<asp:ListItem Text="4" Value="4"></asp:ListItem>
<asp:ListItem Text="5" Value="5"></asp:ListItem>
<asp:Label ID="lblTotalPrice" runat="server" Text="Grand Total"></asp:Label>



Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
Dim dt As New DataTable
Dim dr As DataRow

dt.Columns.Add("Request Name", System.Type.GetType("System.String"))
dt.Columns.Add("Ranking", System.Type.GetType("System.String"))

For i As Integer = 1 To 5
dr = dt.NewRow
dr("Request Name") = "Test " & i
dr("Ranking") = i
GridView1.DataSource = dt

End If
End Sub

I have been trying to do this from couple of days without any luck and any help would be appreciated.


Logic Ali
11-21-2012, 12:27 AM
I haven't analysed anything, but you could start by fixing this:

if (oldSelValue=SelValue){