请耐心等待我,因为我是ASP.NET的初学者。
我有 GridView , PopupExtender 和按钮(按钮位于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
</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" />
 <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();
}
答案 0 :(得分:0)
通过执行以下操作解决了问题:
对于文本框(例如“ GridNameTextBox ”):添加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));
}
}