C# 无法使用onrow命令在EditItemTemplate中填充DropDownList

C# 无法使用onrow命令在EditItemTemplate中填充DropDownList,c#,asp.net,drop-down-menu,edititemtemplate,C#,Asp.net,Drop Down Menu,Edititemtemplate,这是我的代码隐藏代码。我想在用户单击“编辑”后填充DropDownList,但我得到的DropDownList为空。为什么? protected void SupportSchedule_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "EditRow") { int rowIndex = ((GridViewRow)((ImageButton) e.Co

这是我的代码隐藏代码。我想在用户单击“编辑”后填充DropDownList,但我得到的DropDownList为空。为什么?

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代码

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

更新
取消
//图像按钮前的两个下拉列表

在最近的更新中,我刚刚在这里添加了ImageButton,但这是我的原始代码,不起作用。

我使用了SqlDataSource,而不是从后面添加列表项 这就是我用来获取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();
} 

你的答案是处理你所看到的问题的正确方法。但是,如果你没有找出真正的原因

单击的
ImageButton
位于
ItemTemplate
中。要绑定的
DropDownList
位于
EditItemTemplate
中<代码>lbEdit在您未处于编辑模式时存在,但
ddlshiftmanager
仅在您处于编辑模式时存在

因此,修复方法是将GridView置于编辑模式。请注意,这是您实际上已经开始做的事情。您需要设置EditIndex,重新绑定GridView,然后再次获取该行。然后,您将使该行处于编辑模式。此行现在应该包含
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...

}

您将
e.CommandSource
强制转换为
ImageButton
,但我在GridView的任何位置都看不到
ImageButton
。它在哪里?请注意,这是有效的,因为每次回发时SqlDataSource都会为您绑定。因此,您设置了EditIndex,GridView通过SqlDataSource自动绑定,然后在最后单击update按钮时,您就拥有了所需的控件。您好,我尝试使用这段代码的前四行,结果出现错误“Index超出范围。必须是非负的,并且小于集合的大小。”在第四行,为什么Ohi我尝试使用这段代码的前四行,我得到了错误“索引超出范围。必须是非负的并且小于集合的大小。”在第四行,为什么O@j.f.抱歉,我认为问题可能是您需要在绑定之前设置一个数据源。如果设置断点,我猜rows集合不包含任何行。哦,好的,谢谢!:)我以为没有数据源也能用:)