Search code examples
c#asp.netdrop-down-menuedititemtemplate

Cannot populate a DropDownList in EditItemTemplate using OnRowCommand


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.


Solution

  • 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();
    }