Solved

How to bind multiple Dropdownlist in Gridview?

Posted on 2008-10-28
9
5,664 Views
Last Modified: 2011-10-19
I have a gridview which consists of 3 columns of dropdownlist, which these 3 dropdownlist are inter-related, means the 2nd dropdownlist will depends on the 1st dropdown value, where the 3rd dropdownlist will depends on the 2nd dropwdown value.

My problem is when editing the row, I am not able to list the 2nd dropdownlist (drpResponsibility) according to the selected drpApplication dropdown and the same for 3rd dropdownlist (drpFunction) which only listed value according to the selected value in drpResponsibility.

Thanks & regards

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="MaintUserSecurityTest.aspx.cs" Inherits="Maintenance_MaintUserSecurity" Debug="true"%>
<%@ Register TagPrefix="uc1" TagName="AdminHead" Src="../controls/AdminControl.ascx" %>
 
<!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>Register User's Security</title>
    <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1" />
    <meta name="CODE_LANGUAGE" content="C#"/>
	<meta name="vs_defaultClientScript" content="JavaScript"/>
	<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5"/>
	<link rel="stylesheet" href="../Styles/Table.css" type="text/css"/>
	<link rel="stylesheet" href="../Styles/Style.css" type="text/css"/>	
 
  <script language="javascript" type="text/javascript">
    function validate()
	{		    
	 if (document.all.drpApplication.selectedIndex == 0) {
		    alert("Please Select Application.");
			document.all.drpApplication.focus();
			return false;
		}
		
	 if (document.all.txtResponsibility.value == "") {
		    alert("Please enter Responsibility Name.");
			document.all.txtResponsibility.focus();
			return false;
		}	
		
	 if (document.all.txtFunction.value == "") {
		    alert("Please enter Function Name.");
			document.all.txtFunction.focus();
			return false;
		}			
	}	
	</script>	
</head>
<body>
    <form id="form1" runat="server">  
    <uc1:AdminHead id="AdminHead" runat="server"></uc1:AdminHead>
	    
	    <div>
            <div style="margin: auto 16%; width: 788px; height: 1px; background-color: #e7f1fb;
                text-align: center">               
                    <br />
                    <table style="width: 777px; height: 323px; background-color: #e7f1fb">
                        <tr>
                            <td colspan="3" style="height: 16px; text-align: left">
                                <strong><span style="font-size: 10pt">Maintenance - Add/Update Responsibility</span></strong></td>
                        </tr>
                        <tr>
                            <td colspan="3" style="height: 20px; background-color: #e7f1fb">
                            </td>
                        </tr>
                        <tr>
                            <td style="width: 291px; text-align: right">
                                <span style="font-size: 10pt">Organization Type :&nbsp;</span></td>
                            <td colspan="2" style="height: 22px; text-align: left">
                                <asp:RadioButtonList ID="rdblOrgType" runat="server" AutoPostBack="True" RepeatDirection="Horizontal">                                    
                                    <asp:ListItem>Customer</asp:ListItem>
                                    <asp:ListItem>Vendor</asp:ListItem>
                                </asp:RadioButtonList></td>
                        </tr>
                        <tr>
                            <td style="text-align: right; width: 291px;">
                                <span style="font-size: 10pt">Organization Name :&nbsp;</span></td>
                            <td colspan="2" style="height: 22px; text-align: left">
                                <asp:DropDownList ID="DropDownList1" runat="server">
                                </asp:DropDownList></td>
                        </tr>
                        <tr>
                            <td style="width: 291px; text-align: right">
                                <span style="font-size: 10pt">User Type :&nbsp;</span></td>
                            <td colspan="2" style="text-align: left">
                                &nbsp;<asp:RadioButtonList ID="rdblUserType" runat="server" AutoPostBack="True" RepeatDirection="Horizontal">
                                    <asp:ListItem>Person</asp:ListItem>
                                    <asp:ListItem>Group</asp:ListItem>
                                </asp:RadioButtonList></td>
                        </tr>
                        <tr>
                            <td style="width: 291px; text-align: right">
                                <span style="font-size: 10pt">User Name :&nbsp;</span></td>
                            <td colspan="2" style="text-align: left">
                                <asp:DropDownList ID="drpUser" runat="server" AutoPostBack="True">
                                </asp:DropDownList></td>
                        </tr>
                        <tr>
                            <td style="width: 291px">
                            </td>
                            <td colspan="2" style="text-align: left">
                                <asp:Button ID="btnAdd" runat="server" Font-Names="Verdana" Font-Size="9pt" OnClick="ButtonSave_Click"
                                     OnClientClick="return validate();" Text="Add" />&nbsp;
                                <asp:Label ID="lblError" runat="server" Font-Bold="True" Font-Names="Verdana" Font-Size="9pt"
                                    ForeColor="#FF0000"></asp:Label>
                                &nbsp;
                                &nbsp;
                                <asp:Label ID="lblMessage" runat="server" Style="font-size: small; color: red"></asp:Label></td>
                        </tr>
                        <tr>
                            <td colspan="3" style="text-align: center">
                             
                            </td>
                        </tr>
                        <tr>
                            <td colspan="3" style="height: 15px; text-align: center">
                                <asp:GridView ID="UserSecurity" runat="server" AutoGenerateColumns="False" BackColor="White"
                                DataKeyNames="User_ID" ShowFooter="True"
                                OnRowCommand="UserSecurity_RowCommand" 
                                OnRowCancelingEdit="UserSecurity_RowCancelingEdit" OnRowEditing="UserSecurity_RowEditing"
                                OnRowUpdating="UserSecurity_RowUpdating"
                                    BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Horizontal">
                                    <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
                                    <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
                                    <Columns>
                                        <asp:CommandField ShowEditButton="True" />
                                        <asp:TemplateField HeaderText="" InsertVisible="False" SortExpression="ProductID">                                                               
                                            <FooterTemplate>
                                            <asp:Button ID="Add" runat="server" CommandName="Insert" Text="Add" />
                                            </FooterTemplate>
                                        </asp:TemplateField>
                                        <asp:TemplateField HeaderText="APPLICATION NAME" SortExpression="APPLICATION_NAME">
                                            <EditItemTemplate>
                                                <asp:DropDownList ID="drpApplication" runat="server" DataTextField="APPLICATION_NAME" 
                                                DataValueField="APPLICATION_ID"   text='<%# Bind("APPLICATION_ID") %>'
                                                   AutoPostBack="true" DataSource='<%#getApplication()%>'
                                                    OnSelectedIndexChanged="drpApplication_SelectedIndexChanged" >                                                                                                     
                                                </asp:DropDownList>
                                            </EditItemTemplate>                                            
                                            <ItemTemplate>
                                            <asp:Label ID="lblApplicationName" runat="server" Text='<%# Bind("APPLICATION_NAME") %>'></asp:Label>
                                            </ItemTemplate>
                                            <FooterTemplate>
                                            <asp:DropDownList ID="newDrpApplication" runat="server"
                                            AutoPostBack="true" DataSource='<%#getApplication()%>'
                                            OnSelectedIndexChanged="newDrpApplication_SelectedIndexChanged" 
                                            DataTextField="APPLICATION_NAME" DataValueField="APPLICATION_ID">
                                            </asp:DropDownList>
                                            </FooterTemplate>
                                        </asp:TemplateField>
                                        <asp:TemplateField HeaderText="RESPONSIBILITY NAME" SortExpression="RESPONSIBILITY_NAME">
                                            <EditItemTemplate>
                                                <asp:DropDownList ID="drpResponsibility" runat="server"  AppendDataBoundItems=true DataTextField="RESPONSIBILITY_NAME"  
                                                DataValueField="RESPONSIBILITY_ID"  DataSource='<%#getResponsibility()%>' text='<%# Bind("RESPONSIBILITY_ID") %>'
                                                AutoPostBack="true" OnSelectedIndexChanged="drpResp_SelectedIndexChanged">                                                                                                                                                      
                                                </asp:DropDownList>
                                            </EditItemTemplate>                                                 
                                            <ItemTemplate>
                                            <asp:Label ID="lblResponsibilityName" runat="server" Text='<%# Bind("RESPONSIBILITY_NAME") %>'></asp:Label>
                                            </ItemTemplate>
                                            <FooterTemplate>
                                            <asp:DropDownList ID="newDrpResp" runat="server"
                                            AutoPostBack="true" OnSelectedIndexChanged="newDrpResp_SelectedIndexChanged"
                                            DataTextField="RESPONSIBILITY_NAME" DataValueField="RESPONSIBILITY_ID">
                                            </asp:DropDownList>
                                            </FooterTemplate>
                                            </asp:TemplateField>
                                        <asp:TemplateField HeaderText="FUNCTION" SortExpression="FUNCTION">
                                            <EditItemTemplate>
                                                <asp:DropDownList ID="drpFunction" runat="server" DataTextField="FUNCTION" DataValueField="FUNCTION"
                                                    text='<%# Bind("FUNCTION") %>'  DataSource='<%#getFunction()%>'>                                                                                                                                                      
                                                </asp:DropDownList>
                                            </EditItemTemplate> 
                                            <ItemTemplate>
                                            <asp:Label ID="lblFunction" runat="server" Text='<%# Bind("FUNCTION") %>'></asp:Label>
                                            </ItemTemplate>
                                            <FooterTemplate>
                                            <asp:DropDownList ID="newDrpFunc" runat="server"
                                            DataTextField="FUNCTION" DataValueField="FUNCTION">
                                            </asp:DropDownList>
                                            </FooterTemplate></asp:TemplateField>
                                    </Columns>
                                    <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
                                    <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
                                    <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
                                    <AlternatingRowStyle BackColor="#F7F7F7" />
                                </asp:GridView>
                                &nbsp;
                            </td>
                        </tr>
                        <tr>
                            <td colspan="3" style="font-size: 10pt; height: 20px; text-align: center">
                            </td>
                        </tr>
                    </table>
                    <br />
              
            </div>
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</div>        
    </form>
</body>
</html>

Open in new window

0
Comment
Question by:silterra
  • 5
  • 4
9 Comments
 
LVL 8

Expert Comment

by:MrAgile
ID: 22836410
Hi There,

So let me try to understand what you are doing. When you click the edit button on the gridview the dropdown lists are not linked? Have you tried setting the values when the gridview row command event is called?

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowcommand.aspx

Sean
0
 

Author Comment

by:silterra
ID: 22864380
Hi Sean,

My gridview has 3 dropdownlist, when I click the edit button, the all 3 dropdown list are must be in edit mode. For instances, the 1st dropdown list is listing all my applications in my database, the 2nd dropdown list should be listed all the user names for the selected application as selected in dropdownlist 1. My question is how to bind the 2nd dropdown list.
Thank you very much.
0
 
LVL 8

Expert Comment

by:MrAgile
ID: 22864575
HI There,

How about your post the code that you have with an xml datasource of sample data and then I will show you how to do it.

Sean
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:silterra
ID: 22874835
Hi Sean,
I had posted my code, please refer to my first comment. But I don't know how to put in the xml datasource.  Anyway, I had put in some sample data into an excel file,please read the attached q.xls.
Thanks & regards,
Joanne

q.xls
0
 
LVL 8

Expert Comment

by:MrAgile
ID: 22880917
No worries. I will have a look at your data later today. I will also show you how to create an xml datasource.
0
 
LVL 8

Expert Comment

by:MrAgile
ID: 22882473
Oh also can you post the page complete with the gridview and dropdown lists?
0
 
LVL 8

Expert Comment

by:MrAgile
ID: 22891832
are you going to post the page laid out for me?
0
 

Accepted Solution

by:
silterra earned 0 total points
ID: 22919506
Hi Sean,
I had the solution. Anyway, thanks for your helps.
0
 

Author Comment

by:silterra
ID: 22919529
I bind all the dropdownlist one by one during raowdatabound event.
 protected void UserSecurity_RowDataBound(object sender, GridViewRowEventArgs e)
    {        
        if ((e.Row.RowState == DataControlRowState.Edit) || 
       (e.Row.RowState == (DataControlRowState.Edit|DataControlRowState.Alternate)))
        {
            Label app = (Label)e.Row.FindControl("lblCurApp");
            curApplication = app.Text.ToString();
 
            DropDownList myDropDown;
            myDropDown = (DropDownList)e.Row.FindControl("drpApplication");
 
            myDropDown.DataTextField = "APPLICATION_NAME";
            myDropDown.DataValueField = "APPLICATION_ID";
            myDropDown.DataSource = getApplication();
            myDropDown.DataBind();
 
            myDropDown.SelectedIndex = myDropDown.Items.IndexOf(myDropDown.Items.FindByValue(curApplication));
 
            Label resp = (Label)e.Row.FindControl("lblCurResp");
            string curResp = resp.Text.ToString();
            DropDownList ddlResp;
            ddlResp = (DropDownList)e.Row.FindControl("drpResponsibility");
 
            ddlResp.DataTextField = "RESPONSIBILITY_NAME";
            ddlResp.DataValueField = "RESPONSIBILITY_ID";
            ddlResp.DataSource = getResponsibility(curApplication);
            ddlResp.DataBind();
 
            ddlResp.SelectedIndex = ddlResp.Items.IndexOf(ddlResp.Items.FindByValue(curResp));
 
 
            Label func = (Label)e.Row.FindControl("lblCurFunc");
            string curFunc = func.Text.ToString();
            DropDownList ddlFunc;
            ddlFunc = (DropDownList)e.Row.FindControl("drpFunction");
 
            ddlFunc.DataTextField = "FUNCTION";
            ddlFunc.DataValueField = "FUNCTION";
            ddlFunc.DataSource = getFunction(curApplication, curResp);
            ddlFunc.DataBind();
 
            ddlFunc.SelectedIndex = ddlFunc.Items.IndexOf(ddlFunc.Items.FindByValue(curFunc));
        }
    }

Open in new window

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question