In my setup, I have a GridView that I want to populate using a ModalPopupExtender. The ModalPopupExtender contains a panel (ID="FlowPanel") that containsseveral controls: Labels, DropdownLists and two buttons, Cancel and Submit.
The Cancel button (ID="CancelFlowsButton") is the button that cancels the ModalPopupExtender, while the Submit button (ID="AddFlowButton) creates a new row in the GridView with data from the panel "FlowPanel".
Using my code, rows get added just fine to the GridView. However, everytime a new row is added, the data in the previously created rows get overriden with the values of the newly created row.
I am not sure what I am doing wrong. As I am a beginner at ASP.NET, please bear with me, and any help would be appreciated. Thanks!
Default.aspx.cs code (snapshot)
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
FirstFlowGridViewRow();
}
}
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);
ViewState["currentTable"] = table;
FlowGridView.DataSource = table;
FlowGridView.DataBind();
}
protected void AddNewFlowGridViewRow()
{
int rowIndex = 0;
if (ViewState["currentTable"] != null)
{
DataTable currentDt = (DataTable) ViewState["currentTable"];
DataRow currentRow = null;
if(currentDt.Rows.Count > 0)
{
for(int i = 1; i <= currentDt.Rows.Count; i++)
{
currentRow = currentDt.NewRow();
currentDt.Rows[i - 1]["Col1"] = NameTextBox.Text;
currentDt.Rows[i - 1]["Col2"] = DirectionDropDownList.SelectedValue;
currentDt.Rows[i - 1]["Col3"] = StartYearTextBox.Text;
currentDt.Rows[i - 1]["Col4"] = EndYearTextBox.Text; ;
currentDt.Rows[i - 1]["Col5"] = TimingDropDownList.SelectedValue;
currentDt.Rows[i - 1]["Col6"] = TypeDropDownList.SelectedValue;
currentDt.Rows[i - 1]["Col7"] = AmountTextBox.Text;
rowIndex++;
}
currentDt.Rows.Add(currentRow);
ViewState["currentTable"] = currentDt;
FlowGridView.DataSource = currentDt;
FlowGridView.DataBind();
}
}
else
{
Response.Write("ViewState is null");
}
SetPreviousFlowData();
}
protected void SetPreviousFlowData()
{
int rowIndex = 0;
if (ViewState["currentTable"] != null)
{
DataTable currentDt = (DataTable) ViewState["currentTable"];
if(currentDt.Rows.Count > 0)
{
for(int i = 0; i < currentDt.Rows.Count; i++)
{
Label LabelName = (Label)FlowGridView.Rows[rowIndex].Cells[1].FindControl("NameLabel");
DropDownList DropDownDirection = (DropDownList)FlowGridView.Rows[rowIndex].Cells[2].FindControl("GridDirectionDropDownList");
Label LabelStart = (Label)FlowGridView.Rows[rowIndex].Cells[3].FindControl("StartYearLabel");
Label LabelEnd = (Label)FlowGridView.Rows[rowIndex].Cells[4].FindControl("EndYearLabel");
DropDownList DropDownTiming = (DropDownList)FlowGridView.Rows[rowIndex].Cells[5].FindControl("GridTimingDropDownList");
DropDownList DropDownType = (DropDownList)FlowGridView.Rows[rowIndex].Cells[6].FindControl("GridTypeDropDownList");
Label LabelAmount = (Label)FlowGridView.Rows[rowIndex].Cells[7].FindControl("AmountLabel");
LabelName.Text = currentDt.Rows[i]["Col1"].ToString();
DropDownDirection.SelectedValue = currentDt.Rows[i]["Col2"].ToString();
LabelStart.Text = currentDt.Rows[i]["Col3"].ToString();
LabelEnd.Text = currentDt.Rows[i]["Col4"].ToString();
DropDownTiming.SelectedValue = currentDt.Rows[i]["Col5"].ToString();
DropDownType.SelectedValue = currentDt.Rows[i]["Col6"].ToString();
LabelAmount.Text = currentDt.Rows[i]["Col7"].ToString();
rowIndex++;
}
}
}
}
Default.aspx code (snapshot)
<table style="height: 180px; width: 755px;">
<tr>
<td class="style7" valign="top">
<div style="overflow: auto; height: 200px; width: 669px">
<asp:GridView ID="FlowGridView" runat="server" ShowHeaderWhenEmpty="true" AutoGenerateColumns="False"
Width="577px" 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="30%" />
<ItemTemplate>
<asp:Label ID="NameLabel" runat="server" Text="Label"></asp:Label>
</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="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="10%" HorizontalAlign="Center" />
<ItemTemplate>
<asp:Label ID="StartYearLabel" runat="server" Text="Label"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="End Year">
<ItemStyle Font-Size="13px" Width="10%" HorizontalAlign="Center" />
<ItemTemplate>
<asp:Label ID="EndYearLabel" runat="server" Text="Label"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Timing">
<ItemStyle Font-Size="13px" Width="10%" HorizontalAlign="Center" />
<ItemTemplate>
<asp:DropDownList ID="GridTimingDropDownList" runat="server">
<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="10%" HorizontalAlign="Center" />
<ItemTemplate>
<asp:DropDownList ID="GridTypeDropDownList" runat="server">
<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:Label ID="AmountLabel" runat="server" Text="Label"></asp:Label>
</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 Cash Flow..."
Width="158px" />
<asp:ModalPopupExtender ID="FlowModalPopupExtender" runat="server" DynamicServicePath=""
Enabled="True" TargetControlID="AddCashFlowButton" PopupControlID="FlowPanel"
CancelControlID="CancelFlowsButton" BackgroundCssClass="modalBackground">
</asp:ModalPopupExtender>
</td>
</tr>
</table>