• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5707
  • Last Modified:

How to bind multiple Dropdownlist in Gridview?

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
silterra
Asked:
silterra
  • 5
  • 4
1 Solution
 
MrAgileCommented:
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
 
silterraAuthor Commented:
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
 
MrAgileCommented:
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
Industry Leaders: 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!

 
silterraAuthor Commented:
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
 
MrAgileCommented:
No worries. I will have a look at your data later today. I will also show you how to create an xml datasource.
0
 
MrAgileCommented:
Oh also can you post the page complete with the gridview and dropdown lists?
0
 
MrAgileCommented:
are you going to post the page laid out for me?
0
 
silterraAuthor Commented:
Hi Sean,
I had the solution. Anyway, thanks for your helps.
0
 
silterraAuthor Commented:
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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