asp.net转发器中的下拉列表在_ItemCommand上丢失了值

时间:2016-12-13 11:14:32

标签: c# asp.net

我在更新面板中使用asp转发器控件。以下是供参考的代码:

<asp:UpdatePanel ID="upDisbursementEntry" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="true">
    <ContentTemplate>
    .
    .
    .

    <asp:Repeater ID="rptrCotrolsRow" runat="server"  OnItemDataBound="rptrCotrolsRow_ItemDataBound" OnItemCommand="rptrCotrolsRow_ItemCommand" >

        <ItemTemplate>
            <tr>
                <td>
                    <asp:TextBox ID="txtToolId" Text='<%# Eval("ToolId").ToString() %>' AutoPostBack="true" OnTextChanged="txtToolId_TextChanged" class="form-control" runat="server"> </asp:TextBox>
                </td>

                <td>
                    <asp:DropDownList ID="ddlDepartment" class="form-control" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlDepartment_SelectedIndexChanged"  DataTextField='<%# Eval("DepartmentText").ToString() %>' DataValueField='<%# Eval("DepartmentValue").ToString() %>'>
                    </asp:DropDownList> 
                </td>
                <td style="width: 90px">
                    <asp:DropDownList ID="ddlCell" class="form-control" runat="server" AutoPostBack="true" Enabled="false" OnSelectedIndexChanged="ddlCell_SelectedIndexChanged" EnableViewState="true"></asp:DropDownList></td>
                <td>
                    <asp:DropDownList ID="ddlMachine" class="form-control" runat="server" Enabled="false" EnableViewState="true"></asp:DropDownList>
                </td>

                <td>
                    <asp:DropDownList ID="ddlRemovalReason" class="form-control" runat="server" EnableViewState="true"></asp:DropDownList>
                </td>
                <td>
                    <asp:ImageButton ID="btnRemove" runat="server" ImageUrl="../remove-icon.png" ToolTip="Remove this row"/>
                </td>
            </tr>
        </ItemTemplate>
        <FooterTemplate>
            <tr>

                <td>
                    <asp:ImageButton ID="btnAdd" runat="server" ImageUrl="../add-icon.png" OnClick="btnAdd_Click" />
                </td>
            </tr>
        </FooterTemplate>
    </asp:Repeater>
    .
    .
    .
</ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="btnSubmit" EventName="click" />
    </Triggers>
</asp:UpdatePanel>

下面是删除btnRemove行的C#代码:

protected void rptrCotrolsRow_ItemCommand(object source, RepeaterCommandEventArgs e){
foreach (RepeaterItem item in rptrCotrolsRow.Items)
{
     TextBox txtToolId = (TextBox)item.FindControl("txtToolId");
     DataRow row = myTable.NewRow();
     row["ToolId"] = txtToolId.Text;
     TextBox txtPartProduced = (TextBox)item.FindControl("txtPartProduced");
     row["PartProduced"] = txtPartProduced.Text;
     TextBox txtToolLifeAchieved = (TextBox)item.FindControl("txtToolLifeAchieved");
     row["ToolLifeAchieved"] = txtToolLifeAchieved.Text;
     DropDownList ddlDepartment = (DropDownList)item.FindControl("ddlDepartment");
     row["Department"] = ddlDepartment.SelectedValue;
     DropDownList ddlCell = (DropDownList)item.FindControl("ddlCell");
     row["Cell"] = ddlCell.SelectedValue;
     DropDownList ddlMachine = (DropDownList)item.FindControl("ddlMachine");
     row["Machine"] = ddlMachine.SelectedValue;
     TextBox txtQty = (TextBox)item.FindControl("txtQty");
     row["Qty"] = txtQty.Text;
     DropDownList ddlRemovalReason = (DropDownList)item.FindControl("ddlRemovalReason");
     row["RemovalReason"] = ddlRemovalReason.SelectedValue;
     myTable.Rows.Add(row);

}
myTable.Rows[e.Item.ItemIndex].Delete();
rptrCotrolsRow.DataSource = myTable;
rptrCotrolsRow.DataBind();    }

以下是在btnAdd上添加行的C#代码:

protected void btnAdd_Click(object sender, EventArgs e){
foreach (RepeaterItem item in rptrCotrolsRow.Items)
{
     TextBox txtToolId = (TextBox)item.FindControl("txtToolId");
     DataRow row = myTable.NewRow();
     row["ToolId"] = txtToolId.Text;
     TextBox txtPartProduced = (TextBox)item.FindControl("txtPartProduced");
     row["PartProduced"] = txtPartProduced.Text;
     TextBox txtToolLifeAchieved = (TextBox)item.FindControl("txtToolLifeAchieved");
     row["ToolLifeAchieved"] = txtToolLifeAchieved.Text;
     DropDownList ddlDepartment = (DropDownList)item.FindControl("ddlDepartment");
     row["Department"] = ddlDepartment.SelectedValue;
     DropDownList ddlCell = (DropDownList)item.FindControl("ddlCell");
     row["Cell"] = ddlCell.SelectedValue;
     DropDownList ddlMachine = (DropDownList)item.FindControl("ddlMachine");
     row["Machine"] = ddlMachine.SelectedValue;
     TextBox txtQty = (TextBox)item.FindControl("txtQty");
     row["Qty"] = txtQty.Text;
     DropDownList ddlRemovalReason = (DropDownList)item.FindControl("ddlRemovalReason");
     row["RemovalReason"] = ddlRemovalReason.SelectedValue;
     myTable.Rows.Add(row);

}
myTable.Rows.Add(myTable.NewRow());
rptrCotrolsRow.DataSource = myTable;
rptrCotrolsRow.DataBind();}

每件事情都很完美但是当我添加/删除行时,下拉值会重置。请帮忙。

0 个答案:

没有答案