1

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.

j.f.
  • 3,908
  • 2
  • 29
  • 42
  • You cast `e.CommandSource` to an `ImageButton` but I don't see an `ImageButton` anywhere on your GridView. Where is it? – j.f. Jun 11 '15 at 12:54

2 Answers2

1

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();
} 
j.f.
  • 3,908
  • 2
  • 29
  • 42
  • Note that this works because the SqlDataSource binds for you on each PostBack. So you set the EditIndex, the GridView binds automatically via the SqlDataSource, then you have the controls you need when you finally click the update button. – j.f. Jun 18 '15 at 15:05
0

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...

}
j.f.
  • 3,908
  • 2
  • 29
  • 42
  • hi i tried using the first four rows of this code and i got the error " Index was out of range. Must be non-negative and less than the size of the collection." on the fourth line, why? :O – Angeline J. Tan Jun 22 '15 at 12:26
  • hi i tried using the first four rows of this code and i got the error " Index was out of range. Must be non-negative and less than the size of the collection." on the fourth line, why? :O @j.f. – Angeline J. Tan Jun 22 '15 at 12:28
  • Sorry, I think the issue may be that you need to set a DataSource before binding. If you set a breakpoint, my guess is that the rows collection doesn't contain any rows. – j.f. Jun 22 '15 at 13:01
  • oh ok thanks! :) i thought it was gonna work without the datasource :) – Angeline J. Tan Jun 23 '15 at 03:25