簡體   English   中英

在ASP.NET中編輯GridView時出現問題:GridView為空

[英]Issue when Editing GridView in ASP.NET: GridView becomes empty

因為我是ASP.NET的初學者,所以請多多包涵。

我有一個GridViewPopupExtender和一個Button (按鈕位於GridView旁邊)。 GridView具有文本框和下拉列表。 最初,GridView有一個空行(即,文本框為空,並且下拉列表顯示“空白”選項。)

當您單擊按鈕“ AddFlowButton”時,將顯示一個Popupextender,其中包含幾個文本框和下拉列表以及兩個按鈕(“ AddCashFlowButton”和“ CancelButton”)。 填寫PopupExtender並單擊擴展器中的“ AddCashFlowButton”按鈕后,將使用PopupExtender中的值填充GridView。

現在的問題是,當我單擊GridView行旁邊的編輯按鈕時,GridView變為空,即文本框變為空,並且下拉列表顯示了空值。 我不確定如何解決此問題!

任何幫助,將不勝感激! 先感謝您!

這是代碼:

ASPX:

<asp:UpdatePanel ID="UpdatePanel3" runat="server">
                <ContentTemplate>
                    <asp:Panel ID="FlowPanel" runat="server" CssClass="modalPopup">
                        <table>
                            <tr valign="top">
                                <td>
                                    Name
                                </td>
                                <td>
                                    <asp:TextBox ID="NameTextBox" runat="server"></asp:TextBox>
                                </td>
                            </tr>
                            <tr valign="top">
                                <td>
                                    Direction&nbsp;
                                </td>
                                <td>
                                    <asp:DropDownList ID="DirectionDropDownList" runat="server">
                                        <asp:ListItem Value="W">Withdrawal</asp:ListItem>
                                        <asp:ListItem Value="C">Contribution</asp:ListItem>
                                    </asp:DropDownList>
                                </td>
                            </tr>
                            <tr valign="top">
                                <td>
                                    Start Year
                                </td>
                                <td>
                                    <asp:TextBox ID="StartYearTextBox" runat="server"></asp:TextBox>
                                    <asp:NumericUpDownExtender ID="StartYearNumericUpDownExtender" runat="server" TargetControlID="StartYearTextBox"
                                        Minimum="1" Width="70">
                                    </asp:NumericUpDownExtender>
                                </td>
                            </tr>
                            <tr valign="top">
                                <td>
                                    End Year
                                </td>
                                <td>
                                    <asp:TextBox ID="EndYearTextBox" runat="server"></asp:TextBox>
                                    <asp:NumericUpDownExtender ID="EndYearNumericUpDownExtender" runat="server" TargetControlID="EndYearTextBox"
                                        Minimum="1" Width="70">
                                    </asp:NumericUpDownExtender>
                                </td>
                            </tr>
                            <tr valign="top">
                                <td>
                                    Timing
                                </td>
                                <td>
                                    <asp:DropDownList ID="TimingDropDownList" runat="server">
                                        <asp:ListItem Value="E">End of Year</asp:ListItem>
                                        <asp:ListItem Value="B">Beginning of Year</asp:ListItem>
                                    </asp:DropDownList>
                                </td>
                            </tr>
                            <tr valign="top">
                                <td>
                                    Inflation Adjusted
                                </td>
                                <td>
                                    <asp:DropDownList ID="TypeDropDownList" runat="server">
                                        <asp:ListItem Value="Y">Yes</asp:ListItem>
                                        <asp:ListItem Value="N">No</asp:ListItem>
                                    </asp:DropDownList>
                                </td>
                            </tr>
                            <tr valign="top">
                                <td>
                                    Amount
                                </td>
                                <td>
                                    <asp:TextBox ID="AmountTextBox" runat="server"></asp:TextBox>
                                    <br />
                                    Currency amounts are in thousands
                                </td>
                            </tr>
                            <tr valign="top">
                                <td>
                                    <asp:Button ID="AddFlowButton" runat="server" Text="Add Flow" OnClick="AddFlowButton_Click" />
                                    &nbsp<asp:Button ID="CancelFlowsButton" runat="server" Text="Cancel" />
                                </td>
                            </tr>
                        </table>
                    </asp:Panel>
                    <br />
                    <table style="height: 180px; width: 904px;">
                        <tr>
                            <td class="style7" valign="top">
                                <div style="overflow: auto; height: 200px; width: 788px">
                                    <asp:GridView ID="FlowGridView" runat="server" ShowHeaderWhenEmpty="true" AutoGenerateColumns="False"
                                        Width="691px" CellPadding="4" ForeColor="#333333" GridLines="None" AllowSorting="True"
                                        AutoGenerateEditButton="True" OnRowEditing="FlowGridView_RowEditing" OnRowUpdating="FlowGridView_RowUpdating"
                                        OnRowCancelingEdit="FlowGridView_RowCancelingEdit">
                                        <AlternatingRowStyle BackColor="White" ForeColor="#284775" Height="2px" />
                                        <Columns>
                                            <asp:TemplateField HeaderText="Name">
                                                <ItemStyle Font-Size="13px" Width="20%" />
                                                <ItemTemplate>
                                                    <asp:TextBox ID="GridNameTextBox" runat="server" Text="" ReadOnly="false" BorderWidth="0px" Style="text-align: left;"></asp:TextBox>
                                                </ItemTemplate>
                                            </asp:TemplateField>
                                            <asp:TemplateField HeaderText="Direction">
                                                <ItemStyle Font-Size="13px" Width="10%" HorizontalAlign="Center" />
                                                <ItemTemplate>
                                                    <asp:DropDownList ID="GridDirectionDropDownList" runat="server">
                                                        <asp:ListItem Value=""></asp:ListItem>
                                                        <asp:ListItem Value="W">Withdrawal</asp:ListItem>
                                                        <asp:ListItem Value="C">Contribution</asp:ListItem>
                                                    </asp:DropDownList>
                                                </ItemTemplate>
                                            </asp:TemplateField>
                                            <asp:TemplateField HeaderText="Start Year">
                                                <ItemStyle Font-Size="13px" Width="5%" HorizontalAlign="Center" />
                                                <ItemTemplate>
                                                    <asp:TextBox ID="GridStartYearTextBox" runat="server" Text="" Width="50px" BorderWidth="0px" Style="text-align: center;"></asp:TextBox>
                                                </ItemTemplate>
                                            </asp:TemplateField>
                                            <asp:TemplateField HeaderText="End Year">
                                                <ItemStyle Font-Size="13px" Width="5%" HorizontalAlign="Center" />
                                                <ItemTemplate>
                                                    <asp:TextBox ID="GridEndYearTextBox" runat="server" Text="" Width="50px" BorderWidth="0px" Style="text-align: center;"></asp:TextBox>
                                                </ItemTemplate>
                                            </asp:TemplateField>
                                            <asp:TemplateField HeaderText="Timing">
                                                <ItemStyle Font-Size="13px" Width="7%" HorizontalAlign="Center" />
                                                <ItemTemplate>
                                                    <asp:DropDownList ID="GridTimingDropDownList" runat="server">
                                                        <asp:ListItem Value=""></asp:ListItem>
                                                        <asp:ListItem Value="E">End of Year</asp:ListItem>
                                                        <asp:ListItem Value="B">Beginning of Year</asp:ListItem>
                                                    </asp:DropDownList>
                                                </ItemTemplate>
                                            </asp:TemplateField>
                                            <asp:TemplateField HeaderText="Inflation Adj.">
                                                <ItemStyle Font-Size="13px" Width="30%" HorizontalAlign="Center" />
                                                <ItemTemplate>
                                                    <asp:DropDownList ID="GridTypeDropDownList" runat="server" Width="60px">
                                                        <asp:ListItem Value=""></asp:ListItem>
                                                        <asp:ListItem Value="Y">Yes</asp:ListItem>
                                                        <asp:ListItem Value="N">No</asp:ListItem>
                                                    </asp:DropDownList>
                                                </ItemTemplate>
                                            </asp:TemplateField>
                                            <asp:TemplateField HeaderText="Amount">
                                                <ItemStyle Font-Size="13px" Width="10%" HorizontalAlign="Center" />
                                                <ItemTemplate>
                                                    <asp:TextBox ID="GridAmountTextBox" runat="server" Text="" BorderWidth="0px" Style="text-align: center;"></asp:TextBox>
                                                </ItemTemplate>
                                            </asp:TemplateField>
                                        </Columns>
                                        <EditRowStyle BackColor="#999999" />
                                        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                                        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                                        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                                        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                                        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" Height="10px" />
                                        <SortedAscendingCellStyle BackColor="#E9E7E2" />
                                        <SortedAscendingHeaderStyle BackColor="#506C8C" />
                                        <SortedDescendingCellStyle BackColor="#FFFDF8" />
                                        <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
                                    </asp:GridView>
                                </div>
                            </td>
                            <td valign="top">
                                <asp:Button ID="AddCashFlowButton" runat="server" Text="Add Flow..." Width="91px" />
                                <asp:ModalPopupExtender ID="FlowModalPopupExtender" runat="server" DynamicServicePath=""
                                    Enabled="True" TargetControlID="AddCashFlowButton" PopupControlID="FlowPanel"
                                    CancelControlID="CancelFlowsButton" BackgroundCssClass="modalBackground">
                                </asp:ModalPopupExtender>
                            </td>
                        </tr>
                    </table>
                </ContentTemplate>
            </asp:UpdatePanel>

ASPX.cs

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!Page.IsPostBack)
        {               
            FirstFlowGridViewRow();
            retrieveData();

        }


    }

    protected void retrieveData()
    {
        if (Session["currentTable"] != null)
        {
            DataTable dt = (DataTable)Session["currentTable"]; ;
            //dt = (DataTable) ViewState["currentTable"];
            if ((dt != null) && (dt.Rows.Count > 0))
            {
                FlowGridView.Visible = true;
                FlowGridView.DataSource = dt;
                FlowGridView.DataBind();
            }
            else
            {
                FlowGridView.Visible = false;
            }
        }
    }



    protected void AddFlowButton_Click(object sender, EventArgs e)
    {
        AddNewFlowGridViewRow();
        FlowModalPopupExtender.Hide();
    }


    protected void FirstFlowGridViewRow()
    {
        DataTable table = new DataTable();
        DataRow dr = null;

        table.Columns.Add(new DataColumn("Col1", typeof(string)));
        table.Columns.Add(new DataColumn("Col2", typeof(string)));
        table.Columns.Add(new DataColumn("Col3", typeof(int)));
        table.Columns.Add(new DataColumn("Col4", typeof(int)));
        table.Columns.Add(new DataColumn("Col5", typeof(string)));
        table.Columns.Add(new DataColumn("Col6", typeof(string)));
        table.Columns.Add(new DataColumn("Col7", typeof(int)));

        dr = table.NewRow();

        dr["Col1"] = string.Empty;
        dr["Col2"] = string.Empty;
        dr["Col3"] = 0;
        dr["Col4"] = 0;
        dr["Col5"] = string.Empty;
        dr["Col6"] = string.Empty;
        dr["Col7"] = 0;
        table.Rows.Add(dr);

        Session["currentTable"] = table;
        FlowGridView.DataSource = table;
        FlowGridView.DataBind();
    }

    protected void AddNewFlowGridViewRow()
    {
        DataTable currentDt = new DataTable();
        //****** change
        if (Session["currentTable"] != null)
        {
            currentDt = (DataTable)Session["currentTable"];

        }

        else
        {
           //**** change
            currentDt.Columns.Add("Name");
            currentDt.Columns.Add("Direction");
            currentDt.Columns.Add("Start Year");
            currentDt.Columns.Add("End Year");
            currentDt.Columns.Add("Timing");
            currentDt.Columns.Add("Inflation Adj.");
            currentDt.Columns.Add("Amount");

        }


        DataRow currentRow = null;
        currentRow = currentDt.NewRow();

        currentRow["Col1"] = NameTextBox.Text;
        currentRow["Col2"] = DirectionDropDownList.SelectedValue;
        currentRow["Col3"] = StartYearTextBox.Text;
        currentRow["Col4"] = EndYearTextBox.Text; ;
        currentRow["Col5"] = TimingDropDownList.SelectedValue;
        currentRow["Col6"] = TypeDropDownList.SelectedValue;
        currentRow["Col7"] = AmountTextBox.Text;

        currentDt.Rows.Add(currentRow);

        if (currentDt.Rows.Count > 1)
        {
            DataRow firstRow = currentDt.Rows[0];

            if ((String.IsNullOrEmpty(firstRow["Col1"].ToString()) &&
                    String.IsNullOrEmpty(firstRow["Col2"].ToString()) &&
                    String.IsNullOrEmpty(firstRow["Col5"].ToString()) &&
                    String.IsNullOrEmpty(firstRow["Col6"].ToString())) &&
                (firstRow["Col3"].ToString() == "0" &&
                    firstRow["Col4"].ToString() == "0" &&
                    firstRow["Col7"].ToString() == "0"))
            {
                currentDt.Rows.RemoveAt(0);
            }
        }

        FlowGridView.DataSource = currentDt;
        FlowGridView.DataBind();
        Session["currentTable"] = currentDt;

        SetPreviousFlowData();

    }


    protected void SetPreviousFlowData()
    {
        int rowIndex = 0;
        //**** Change
        if (Session["currentTable"] != null)
        {
            DataTable currentDt = (DataTable)Session["currentTable"];

            if (currentDt.Rows.Count > 0)
            {
                for (int i = 0; i < currentDt.Rows.Count; i++)
                {
                    TextBox TextName = (TextBox)FlowGridView.Rows[rowIndex].Cells[1].FindControl("GridNameTextBox");
                    DropDownList DropDownDirection = (DropDownList)FlowGridView.Rows[rowIndex].Cells[2].FindControl("GridDirectionDropDownList");
                    TextBox TextStart = (TextBox)FlowGridView.Rows[rowIndex].Cells[3].FindControl("GridStartYearTextBox");
                    TextBox TextEnd = (TextBox)FlowGridView.Rows[rowIndex].Cells[4].FindControl("GridEndYearTextBox");
                    DropDownList DropDownTiming = (DropDownList)FlowGridView.Rows[rowIndex].Cells[5].FindControl("GridTimingDropDownList");
                    DropDownList DropDownType = (DropDownList)FlowGridView.Rows[rowIndex].Cells[6].FindControl("GridTypeDropDownList");
                    TextBox TextAmount = (TextBox)FlowGridView.Rows[rowIndex].Cells[7].FindControl("GridAmountTextBox");

                    TextName.Text = currentDt.Rows[i]["Col1"].ToString();
                    DropDownDirection.SelectedValue = currentDt.Rows[i]["Col2"].ToString();
                    TextStart.Text = currentDt.Rows[i]["Col3"].ToString();
                    TextEnd.Text = currentDt.Rows[i]["Col4"].ToString();
                    DropDownTiming.SelectedValue = currentDt.Rows[i]["Col5"].ToString();
                    DropDownType.SelectedValue = currentDt.Rows[i]["Col6"].ToString();
                    TextAmount.Text = currentDt.Rows[i]["Col7"].ToString();
                    rowIndex++;
                }
            }
        }
    }


    protected void FlowGridView_RowEditing(object sender, GridViewEditEventArgs e)
    {

        FlowGridView.EditIndex = e.NewEditIndex;
        retrieveData();

    }

    protected void FlowGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {

        TextBox TextName = (TextBox)FlowGridView.Rows[e.RowIndex].Cells[1].FindControl("GridNameTextBox");
        DropDownList DropDownDirection = (DropDownList)FlowGridView.Rows[e.RowIndex].Cells[2].FindControl("GridDirectionDropDownList");
        TextBox TextStart = (TextBox)FlowGridView.Rows[e.RowIndex].Cells[3].FindControl("GridStartYearTextBox");
        TextBox TextEnd = (TextBox)FlowGridView.Rows[e.RowIndex].Cells[4].FindControl("GridEndYearTextBox");
        DropDownList DropDownTiming = (DropDownList)FlowGridView.Rows[e.RowIndex].Cells[5].FindControl("GridTimingDropDownList");
        DropDownList DropDownType = (DropDownList)FlowGridView.Rows[e.RowIndex].Cells[6].FindControl("GridTypeDropDownList");
        TextBox TextAmount = (TextBox)FlowGridView.Rows[e.RowIndex].Cells[7].FindControl("GridAmountTextBox");

        string newName = TextName.Text.ToString();
        string newDirection = DropDownDirection.Text.ToString();
        string newStart = TextStart.Text.ToString();
        string newEnd = TextEnd.Text.ToString();
        string newTiming = DropDownTiming.Text.ToString();
        string newInf = DropDownType.Text.ToString();
        string newAmount = TextAmount.Text.ToString();


        //DataTable dt = (DataTable)ViewState["currentTable"];
        DataTable dt = (DataTable)Session["currentTable"];
        DataRow dr = dt.Rows[e.RowIndex];

        dr["Col1"] = newName;
        dr["Col2"] = newDirection;
        dr["Col3"] = newStart;
        dr["Col4"] = newEnd;
        dr["Col5"] = newTiming;
        dr["Col6"] = newInf;
        dr["Col7"] = newAmount;

        dr.AcceptChanges();
        Session["currentTable"] = dt;
        FlowGridView.EditIndex = -1;
        retrieveData();
    }

    protected void FlowGridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        FlowGridView.EditIndex = -1;
        retrieveData();
        //AddNewFlowGridViewRow();
    }

通過執行以下操作自己解決了該問題:

  • 對於文本框 (例如“ GridNameTextBox ”):add Text='<%# Eval("Col1") %>'

     <asp:TextBox ID="GridNameTextBox" runat="server" Text='<%# Eval("Col1") %>' ReadOnly="false" BorderWidth="0px" Style="text-align: left;"></asp:TextBox> 
  • 對於下拉列表 (例如“ GridDirectionDropDownList ”):

     asp:TemplateField HeaderText="Direction"> <ItemStyle Font-Size="13px" Width="10%" HorizontalAlign="Center" /> <ItemTemplate> <asp:Label ID="lblDirection" Text='<%# Eval("Col2") %>' runat="server" /> </ItemTemplate> <EditItemTemplate> <asp:Label ID="HiddenDirectionLabel" Text='<%# Eval("Col2") %>' runat="server" Visible="false" /> <asp:DropDownList ID="GridDirectionDropDownList" runat="server" AutoPostBack="false"> <asp:ListItem Value=""></asp:ListItem> <asp:ListItem Value="W">Withdrawal</asp:ListItem> <asp:ListItem Value="C">Contribution</asp:ListItem> </asp:DropDownList> </EditItemTemplate> </asp:TemplateField> 

並使用事件處理程序RowDataBound :(將OnRowDataBound="FlowGridView_RowDataBound"添加到<asp:GridView ...>

    protected void FlowGridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        DropDownList ddl = null;
        if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState & DataControlRowState.Edit) == DataControlRowState.Edit)
        {
            ddl = e.Row.FindControl("GridDirectionDropDownList") as DropDownList;
            Label direction = (Label)e.Row.FindControl("HiddenDirectionLabel");
            ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByText(direction.Text));

        }

    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM