Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-20-2012, 08:10 PM   PM User | #1
nganesha
New to the CF scene

 
Join Date: Nov 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
nganesha is an unknown quantity at this point
Changing values in multiple Dropdown list

Hi,

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:

Default2.aspx

Code:
<%@ 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++){
                        if(selectObject.options[i].text>SelValue){
                            selectObject.options[i].selected=true;
                            break; 
                        }
                    }
                }
            }
        }
    }
    function getParentRow(obj) {
        while (obj.tagName != "TR") {
            if (isFireFox()) {
                obj = obj.parentNode;
            }
            else {
                obj = obj.parentElement;
            }
        }
        return obj;
    }
    function isFireFox() {
        return navigator.appName == "Netscape";
    }
        
</script>

</head>
<body>
    <form id="form1" runat="server">
    <div>
        <input id="hiddenVal" type="hidden" />
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
Width="492px">
<Columns>
    <asp:BoundField DataField="Request Name" HeaderText="Request Name" />
    <asp:TemplateField HeaderText="Ranking">
        <ItemTemplate>
            <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:DropDownList>
        </ItemTemplate>
        <FooterTemplate>
            <asp:Label ID="lblTotalPrice" runat="server" Text="Grand Total"></asp:Label>
        </FooterTemplate>
    </asp:TemplateField>
    </Columns>
</asp:GridView>

    </div>
    </form>
</body>
</html>
Dafault2.aspx.vb

Code:
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
                dt.Rows.Add(dr)
                dt.AcceptChanges()
            Next
            GridView1.DataSource = dt
            GridView1.DataBind()

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

Thanks,
Navin
nganesha is offline   Reply With Quote
Old 11-20-2012, 11:27 PM   PM User | #2
Logic Ali
Regular Coder

 
Logic Ali's Avatar
 
Join Date: Sep 2010
Location: London
Posts: 975
Thanks: 0
Thanked 203 Times in 198 Posts
Logic Ali will become famous soon enoughLogic Ali will become famous soon enough
I haven't analysed anything, but you could start by fixing this:

Quote:
Code:
if (oldSelValue=SelValue){
Logic Ali is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:26 AM.


Advertisement
Log in to turn off these ads.