简体   繁体   中英

Dropdown_SelectedIndexchange event fires for all rows in gridview

I have gridview which contains dropdown list in each row. The drop down list has selected index change event. Now when I change the value in drop down in any row of a grid, it fires selected index change event for all rows in gridview. How to solve this issue?

Below is HTML mark up

<asp:GridView ID="gvEditFields" runat="server" 
                        AllowSorting="false" AllowPaging="false" OnRowDataBound="gvEditFields_RowDataBound" >
                        <Columns>
                            <asp:TemplateField HeaderText="Project">
                                <ItemTemplate>
                                    <asp:DropDownList ID="ddlProjectList" runat="server" Width="130px" AutoPostBack="true"  OnSelectedIndexChanged="ddlProjectList_SelectedIndexChanged"></asp:DropDownList>
                                    <asp:RequiredFieldValidator ID="rfvProjectList" runat="server" ErrorMessage="* Required"
                                        Display="Dynamic" ControlToValidate="ddlProjectList" SetFocusOnError="True" ValidationGroup="EditGridSave"
                                        Text="Required" ></asp:RequiredFieldValidator>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Task">
                                <ItemTemplate>
                                    <asp:DropDownList ID="ddlTaskList" runat="server" AppendDataBoundItems="false" Width="150px" OnSelectedIndexChanged="ddlTaskList_SelectedIndexChanged"></asp:DropDownList>
                                    <asp:RequiredFieldValidator ID="rfvTaskList" runat="server" ErrorMessage="* Required"
                                        Display="Dynamic" ControlToValidate="ddlTaskList" SetFocusOnError="True" ValidationGroup="EditGridSave"
                                        Text="Required" ></asp:RequiredFieldValidator>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Task Status">
                                <ItemTemplate>
                                    <asp:DropDownList ID="ddlTaskStatus" runat="server"  Width="100px"></asp:DropDownList>
                                    <asp:RequiredFieldValidator ID="rfvTaskStatus" runat="server" ErrorMessage="* Required"
                                        Display="Dynamic" ControlToValidate="ddlTaskStatus" SetFocusOnError="True" ValidationGroup="EditGridSave"
                                        Text="Required" ></asp:RequiredFieldValidator>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Description">
                                <ItemTemplate>
                                    <asp:TextBox ID="txtDescription" runat="server" TextMode="MultiLine" Rows="3" Columns="30" />
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>

This is the code for row databound and selected index change event.

protected void gvEditFields_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        SetEditFieldsGridRecords(e.Row);
        //DropDownList ddlProjectList = (DropDownList)e.Row.FindControl("ddlProjectList");

    }
}

protected void ddlProjectList_SelectedIndexChanged(object sender, EventArgs e)
{        
    DropDownList ddlProjectList = (DropDownList)sender;
   // GridViewRow Row = (GridViewRow)ddlProjectList.NamingContainer;
    GridViewRow selectedRow = (GridViewRow)ddlProjectList.Parent.Parent;
    int i = selectedRow.RowIndex;
    DropDownList ddlTaskList = (DropDownList)selectedRow.Cells[1].FindControl("ddlTaskList");
    DropDownList ddlTaskStatus = (DropDownList)selectedRow.Cells[2].FindControl("ddlTaskStatus");

    BindTasks(clsCheckDBNull.ToStr(ddlProjectList.SelectedValue), -1, ddlTaskList);
    BindStatus(clsCheckDBNull.ToInt(ddlTaskList.SelectedValue), "", ddlTaskStatus);

}

Below is the code for methods used

private void BindStatus(int intTaskId, string TaskStatus, DropDownList drpStatus)
{
    DataTable dt = clsProjectTaskStatuses.SelectAll(BTMSession.AccountID);
    DataView dv = new DataView(dt);
    drpStatus.DataTextField = "str_PROJECT_TASK_STATUS_NAME";
    drpStatus.DataValueField = "int_PROJECT_TASK_STATUS_ID";
    drpStatus.DataSource = dv;
    drpStatus.DataBind();
    SiteUtility.BindTooltip(drpStatus);

    if (drpStatus.Items.Count >= 2 && TaskStatus.Trim() != "")
    {
        if (drpStatus.Items.FindByText(TaskStatus.ToString()) != null)
            drpStatus.SelectedIndex = drpStatus.Items.IndexOf(drpStatus.Items.FindByText(TaskStatus.ToString()));
    }
}

private void BindTasks(string ProjectNo, int intTaskid, DropDownList drpTasks)
{
    if (drpTasks.Items.Count >= 1)
        drpTasks.Items.Clear();
    DataSet ds = new DataSet();

    clsImportFileTypeDAL objImportFileTypeDAL = new clsImportFileTypeDAL();
    ds = objImportFileTypeDAL.SelectTaskListByProjectNoUserIDForTimeImport(ProjectNo, clsCheckDBNull.ToInt(BTMSession.UserId));
    CommonHelper.DisposeOf(objImportFileTypeDAL);

    if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
    {
        drpTasks.DataTextField = "str_TASK_SUMMERY";
        drpTasks.DataValueField = "int_task_id";
        drpTasks.DataSource = ds;
        drpTasks.DataBind();
    }

    drpTasks.Items.Insert(0, new ListItem("Select Task", "-1"));
    SiteUtility.BindTooltip(drpTasks);
    drpTasks.SelectedValue = "-1";

    if (drpTasks.Items.Count >= 2)
    {
        if (drpTasks.Items.FindByValue(intTaskid.ToString()) != null)
            drpTasks.SelectedIndex = drpTasks.Items.IndexOf(drpTasks.Items.FindByValue(intTaskid.ToString()));
    }
}

private void SetEditFieldsGridRecords(GridViewRow row) { DataRowView drv = (DataRowView)row.DataItem;

    DropDownList ddlProjectList = (DropDownList)row.FindControl("ddlProjectList");
    BindProjects(clsCheckDBNull.ToStr(drv["str_PROJECT_NO"]), ddlProjectList);

    DropDownList ddlTaskList = (DropDownList)row.FindControl("ddlTaskList");
    BindTasks(ddlProjectList.SelectedValue, clsCheckDBNull.ToInt(drv["int_TASK_NO"]), ddlTaskList);

    DropDownList ddlTaskStatus = (DropDownList)row.FindControl("ddlTaskStatus");
    BindStatus(clsCheckDBNull.ToInt(ddlTaskList.SelectedValue), clsCheckDBNull.ToStr(drv["TaskStatus"]), ddlTaskStatus);

    BTMWeb.Calender.Calender txtWorkDate = (BTMWeb.Calender.Calender)row.FindControl("txtWorkDate");
    DropDownList drpStartHours = (DropDownList)row.FindControl("drpStartHours");
    DropDownList drpStartMinutes = (DropDownList)row.FindControl("drpStartMinutes");
    DropDownList drpEndHours = (DropDownList)row.FindControl("drpEndHours");
    DropDownList drpEndMinutes = (DropDownList)row.FindControl("drpEndMinutes");
    TextBox txtDescription = (TextBox)row.FindControl("txtDescription");

    int StartHours = clsCheckDBNull.ToDate(drv["StartTime"]).Hour;
    int StartMinutes = clsCheckDBNull.ToDate(drv["StartTime"]).Minute;
    int EndHours = clsCheckDBNull.ToDate(drv["EndTime"]).Hour;
    int EndMinutes = clsCheckDBNull.ToDate(drv["EndTime"]).Minute;

    txtWorkDate.DateValue = clsCheckDBNull.ToDate(drv["dt_WORKDATE"]);
    drpStartHours.SelectedValue = StartHours.ToString();
    drpStartMinutes.SelectedValue = StartMinutes.ToString();
    drpEndHours.SelectedValue = EndHours.ToString();
    drpEndMinutes.SelectedValue = EndMinutes.ToString();

    txtDescription.Text = clsCheckDBNull.ToStr(drv["str_Description"]);

}

For eg. if I have rows and if I change dropdown value in first row, the selected index change fires for both the rows.

Just one or two improvement suggestions:

  • Use the NamingContainer property to get the reference to the the GridViewRow instead of Paret.Parent which is inherently error-prone(eg in case you use a control in the TemplateField that is a container-control.

So instead of:

GridViewRow selectedRow = (GridViewRow)ddlProjectList.Parent.Parent;

this

GridViewRow selectedRow = (GridViewRow)ddlProjectList.NamingContainer;
  • Related to this: always use FindControl on the NamingContainer of the control you want to find, that is the GridViewRow in this case, TableCell does not implement INamingContainer .

So instead of:

DropDownList ddlTaskList = (DropDownList)selectedRow.Cells[1].FindControl("ddlTaskList");

this

DropDownList ddlTaskList = (DropDownList)selectedRow.FindControl("ddlTaskList");

This is simply less error-prone.

Side-note: it's not clear why you need a method like CommonHelper.DisposeOf . I assume you'll find the core problem in BindTasks and BindStatus . Can you show them?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM