質問

Please bear with me as I am a beginner at ASP.NET.

I have a GridView, a PopupExtender and a Button (button is located next to the GridView). The GridView has textboxes and dropdownlists. Initially, the GridView has one empty row (i.e. the textboxes are empty, and the dropdownlist show "blank" option.)

When you click on the button "AddFlowButton", a Popupextender shows up, with several textboxes and dropdownlists, and two Buttons ("AddCashFlowButton" and "CancelButton"). Once you fill out the PopupExtender, and click the "AddCashFlowButton" button in the extender, the GridView is populated using the values in the PopupExtender.

The problem now is that when I click on the edit button next to a GridView row, the GridView becomes empty, i.e. the textboxes become empty and the dropdownlists show empty values. I am not sure how to fix this!

Any help would be appreciated! Thank you in advance!

Here is the code:

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();
    }
役に立ちましたか?

解決

Solved the problem myself by doing the following:

  • For textboxes (example "GridNameTextBox"): add Text='<%# Eval("Col1") %>'

    <asp:TextBox ID="GridNameTextBox" runat="server" Text='<%# Eval("Col1") %>' ReadOnly="false" BorderWidth="0px" Style="text-align: left;"></asp:TextBox>
    
  • For dropdownlists (example "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>
    

and use event handler RowDataBound: (add OnRowDataBound="FlowGridView_RowDataBound" to <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帰属
所属していません StackOverflow
scroll top