locked
Dropdown event with in gridview not working. RRS feed

  • Question

  • User809322603 posted

    Hi friend i have two dropdown list in gridview i want to bind the second dropdown list when user select something from the first dropdown list

    i tried to bind the second dropdown list on selected indexchenged event of first dropdown list but this is not woking .

    Kinldy see the code below.

    this is aspx page.

    <asp:GridView ID="grdManualTask" runat="server" AutoGenerateColumns="False" 
                                                        BackColor="LightGoldenrodYellow" BorderColor="Tan" BorderWidth="1px" 
                                                        CellPadding="2" EnableModelValidation="True" ForeColor="Black" 
                                                        GridLines="None" onrowdatabound="grdManualTask_RowDataBound" 
                                                        AutoGenerateSelectButton="true">
                                                        <AlternatingRowStyle BackColor="PaleGoldenrod" />
                                                    <Columns>
                                                    <asp:TemplateField HeaderText="Project">
                                                    <ItemTemplate> 
                                                        <asp:DropDownList ID="ddlProject" runat="server" 
                                                            onselectedindexchanged="ddlProject_SelectedIndexChanged">
                                                        </asp:DropDownList>
                                                                                                   
                                                    </ItemTemplate>
                                                    </asp:TemplateField> 
    
    
                                                     <asp:TemplateField HeaderText="Task">
                                                    <ItemTemplate>  
                                                     <asp:DropDownList ID="ddlTask" runat="server">
                                                     </asp:DropDownList>                                              
                                                    </ItemTemplate>
                                                    </asp:TemplateField> 
    
    
                                                     <asp:TemplateField HeaderText="Hours" >
                                                    <ItemTemplate>  
                                                    <asp:TextBox ID="txtHours" runat="server" CssClass="Combotext"></asp:TextBox>                                              
                                                    </ItemTemplate>
                                                    </asp:TemplateField> 
    
    
                                                     <asp:TemplateField HeaderText="Description">
                                                    <ItemTemplate>     
                                                    <asp:TextBox ID="txtDescription" runat="server" TextMode="MultiLine" CssClass="Combotext" ></asp:TextBox>                                                                                         
                                                    </ItemTemplate>
                                                    </asp:TemplateField> 
    
                                                    
                                                    </Columns>
    
                                                        <FooterStyle BackColor="Tan" />
                                                        <HeaderStyle BackColor="Tan" Font-Bold="True" />
                                                        <PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" 
                                                            HorizontalAlign="Center" />
                                                        <SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
    
                                                    </asp:GridView>


    this .cs page

            protected void grdManualTask_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    var ddl = (DropDownList)e.Row.FindControl("ddlProject");
                                 
                    string qry = "SELECT PR.ProjectName,PR.ClassCode "
                              + " FROM ProjectRequest AS PR"
                              + " INNER JOIN ProjectOwnership AS PO"
                              + " ON PR.ProjectReqID=PO.ProjectReqID"
                              + " WHERE PR.Status='Opened'"
                              + " AND PO.OwnerType='Primary'";
    
                    ddl.DataSource = objTimeSheet.returnDatatable(qry); 
                    ddl.DataTextField = "ProjectName";
                    ddl.DataValueField = "ClassCode";
                    ddl.DataBind();
                    ddl.Items.Insert(0, new ListItem("--Select--", "0"));
                   
    
                }
    
            }
    
            protected void ddlProject_SelectedIndexChanged(object sender, EventArgs e)
            {
                GridViewRow row = (GridViewRow)((DropDownList)sender).Parent.Parent;
                DropDownList ddlProject = (DropDownList)row.FindControl("ddlProject");
                DropDownList ddlTask = (DropDownList)row.FindControl("ddlTask");
                string qry = "SELECT TaskDescription,TaskCode From task WHERE classCode='" + ddlProject.SelectedValue + "'";
                ddlTask.DataSource = objTimeSheet.returnDatatable(qry);
                ddlTask.DataTextField = "TaskDescription";
                ddlTask.DataValueField = "TaskCode";
                ddlTask.DataBind();
                ddlTask.Items.Insert(0, new ListItem("--Select--", "0"));
            }  



    Tuesday, September 24, 2013 4:58 AM

Answers

  • User289622470 posted

    Hi,


      Since your dropdown list in inside a grid view SelectedIndexChanged will not be fired. All the events inside the grid view will be bublled up to GridView ItemCommand event. So you need to use this event to do any operation.


    Thanks,

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 24, 2013 5:23 AM