1

这是我的代码背后的代码。一旦用户单击编辑,我想填充 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,但这是我的原始代码不起作用。

4

2 回答 2

1

我使用了 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();
} 
于 2015-06-15T09:08:46.050 回答
0

您的答案是处理您所看到的问题的正确方法。但是,如果你没有弄清楚真正的原因......

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

}
于 2015-06-15T13:12:21.000 回答