Cannot populate a DropDownList in EditItemTemplate using OnRowCommand

310 Views Asked by At

This is my code behind code. I want to populate the DropDownList once the user clicked edit but the DropDownList I'm getting is null. Why?

protected void SupportSchedule_RowCommand(object sender, GridViewCommandEventArgs e) 
{
    if (e.CommandName == "EditRow") 
    {
        int rowIndex = ((GridViewRow)((ImageButton) e.CommandSource).NamingContainer).RowIndex;
        GridViewRow row = (GridViewRow)(((ImageButton) e.CommandSource).NamingContainer);
        SupportScheduleTable.EditIndex = rowIndex;
        shift.Enabled = true;
        resourcedate.Enabled = true;
        ListItemCollection c = db.fillList();

        DropDownList ddl1 = row.FindControl("ddlshiftmanager") as DropDownList;
        DropDownList ddl2 = row.FindControl("ddldispatcherone") as DropDownList;
        DropDownList ddl3 = row.FindControl("ddldispatchertwo") as DropDownList;

        if (c != null && ddl1 != null) 
        {
            ddl1.DataSource = c;
            ddl2.DataSource = c;
            ddl3.DataSource = c;
            ddl1.DataBind();
            ddl2.DataBind();
            ddl3.DataBind();
        }
        getSupportSchedule();
    } 
    else if (e.CommandName == "CancelUpdate")
    {
        //some codes here
    } else if (e.CommandName == "UpdateRow")
    {
        //some codes here
    }
}

//asp code

<asp:GridView ID="SupportScheduleTable" AutoGenerateColumns="False" Width="100%" runat="server" OnRowCommand="SupportSchedule_RowCommand">
  <Columns>
    <asp:TemplateField HeaderText="Shift Manager">
      <EditItemTemplate>
        <asp:DropDownList ID="ddlshiftmanager" runat="server" Width="99%"></asp:DropDownList>
      </EditItemTemplate>
      <ItemTemplate>
        <asp:Label ID="Label1" runat="server" Text='<%# Bind("shift_manager") %>'></asp:Label>
      </ItemTemplate>
      <HeaderStyle Width="32%" />
    </asp:TemplateField>
    <asp:TemplateField ItemStyle-HorizontalAlign="Center">
      <ItemTemplate>
        <asp:ImageButton ID="lbEdit" CssClass="btn" ImageUrl="~/Files/edit.png" CommandArgument='<%# Eval("support_schedule_id") %>' CommandName="EditRow" runat="server"></asp:ImageButton>
      </ItemTemplate>
      <EditItemTemplate>
        <asp:LinkButton ID="lbUpdate" CommandArgument='<%# Eval("support_schedule_id") %>' CommandName="UpdateRow" runat="server">Update</asp:LinkButton>
        <asp:LinkButton ID="lbCancel" CommandArgument='<%# Eval("support_schedule_id") %>' CommandName="CancelUpdate" runat="server" CausesValidation="false">Cancel</asp:LinkButton>
      </EditItemTemplate>
      <HeaderStyle Width="2%" />
    </asp:TemplateField>
    //two dropdownlists before image button
  </Columns>
</GridView>

I just added the ImageButton here in the recent update but this is my original code that doesn't work.

2

There are 2 best solutions below

1
On BEST ANSWER

I used a SqlDataSource instead of adding the list items from the back and this is what I used to get the selected value of the DropDownList.

else if (e.CommandName == "UpdateRow")
{
    int rowIndex = ((GridViewRow)((LinkButton)e.CommandSource).NamingContainer).RowIndex;
    DropDownList ddlshift = (DropDownList)SupportScheduleTable.Rows[rowIndex].FindControl("ddlshiftmanager");
    DropDownList ddlone = (DropDownList)SupportScheduleTable.Rows[rowIndex].FindControl("ddldispatcherone");
    DropDownList ddltwo = (DropDownList)SupportScheduleTable.Rows[rowIndex].FindControl("ddldispatchertwo");
    string manager = ddlshift.SelectedValue;
    string one = ddlone.SelectedValue;
    string two = ddltwo.SelectedValue;
    int supportID = Convert.ToInt32(e.CommandArgument);
    String sh = shift.Text;
    String date = resourcedate.Text;
    db.updateSS(supportID, sh, manager, one, two,date);
    SupportScheduleTable.EditIndex = -1;
    shift.Enabled = false;
    resourcedate.Enabled = false;
    getSupportSchedule();
} 
4
On

Your answer is a correct way to handle the issue you are seeing. But, in case you didn't figure out the actual cause...

The ImageButton you click is in your ItemTemplate. The DropDownList you want to bind is in your EditItemTemplate. lbEdit exists when you are not in edit mode but ddlshiftmanager only exists when you are.

So, the fix is to put the GridView in edit mode. Notice that this is something you actually already started to do. You need to set the EditIndex, re-bind the GridView, then get the row again. You'll then have the row in edit mode. This row should now contain ddlshiftmanager.

protected void SupportSchedule_RowCommand(object sender, GridViewCommandEventArgs e) 
{
    if (e.CommandName == "EditRow") 
    {
        int rowIndex = ((GridViewRow)((ImageButton) e.CommandSource).NamingContainer).RowIndex;

        // Set the index to edit
        SupportScheduleTable.EditIndex = rowIndex;

        // Re-bind the GridView to put it in edit mode
        SupportScheduleTable.DataSource = /* your data source */
        SupportScheduleTable.DataBind();

        // Get the row at the index. The row will be the
        // row reflected in edit mode.
        GridViewRow editRow = SupportScheduleTable.Rows[rowIndex];

        // Find your DropDownLists in this edit row
        DropDownList ddl1 = editRow.FindControl("ddlshiftmanager") as DropDownList;
        DropDownList ddl2 = editRow.FindControl("ddldispatcherone") as DropDownList;
        DropDownList ddl3 = editRow.FindControl("ddldispatchertwo") as DropDownList;

        shift.Enabled = true;
        resourcedate.Enabled = true;
        ListItemCollection c = db.fillList();

        if (c != null && ddl1 != null) 
        {
            ddl1.DataSource = c;
            ddl2.DataSource = c;
            ddl3.DataSource = c;
            ddl1.DataBind();
            ddl2.DataBind();
            ddl3.DataBind();
        }
        getSupportSchedule();
    }

    // Everything else...

}