嗨我有一个带有两列文本框和下拉列表的网格视图,当我添加值并单击“添加”按钮我想添加带有Previous值的新行时, 我这样做,但我以前的价值观刷新了。请帮我。 这是我的aspx
<form id="form1" runat="server">
<div>
<asp:GridView ID="gvStudent" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:TextBox ID="TextBoxName" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Address">
<ItemTemplate>
<asp:DropDownList ID="DropDownListAddress" runat="server" AutoPostBack="true">
<asp:ListItem Text="Select" Value="0"> </asp:ListItem>
<asp:ListItem Text="Address1" Value="1"> </asp:ListItem>
<asp:ListItem Text="Address2" Value="2"> </asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
<div>
<asp:Button ID="ButtonADD" runat="server" Text="Add" OnClick="ButtonADD_Click" />
</div>
</form>
这是我的CodeBehind
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
SetInitialRow();
}
}
private void SetInitialRow()
{
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn("Name", typeof(string)));
dt.Columns.Add(new DataColumn("Address", typeof(string)));
dr = dt.NewRow();
dr["Name"] = string.Empty;
dr["Address"] = string.Empty;
dt.Rows.Add(dr);
ViewState["StudentTable"] = dt;
gvStudent.DataSource = dt;
gvStudent.DataBind();
}
protected void ButtonADD_Click(object sender, EventArgs e)
{
//Add Rows
}
}
}
答案 0 :(得分:0)
有3个要做的事情:
将数据表绑定到gridview
private void SaveGridViewDataIntoDataTable()
{
DataTable StudentTable = ViewState["StudentTable"] as DataTable;
foreach (GridViewRow row in gvStudent.Rows)
{
//get ddl value
DropDownList DropDownListAddress = row.FindControl("DropDownListAddress") as DropDownList;
StudentTable.Rows[row.RowIndex]["Address"] = DropDownListAddress.SelectedValue;
//get name from textbox
TextBox TextBoxName = row.FindControl("TextBoxName") as TextBox;
StudentTable.Rows[row.RowIndex]["Name"] = TextBoxName.Text;
}
ViewState["StudentTable"] = StudentTable;
}
private void AddNewRowToGridView()
{
SaveGridViewDataIntoDataTable();
DataTable StudentTable = ViewState["StudentTable"] as DataTable;
StudentTable.Rows.Add("Name", "Select");
gvStudent.DataSource = StudentTable;
gvStudent.DataBind();
}
现在订阅Gridview RowBound事件
<asp:GridView ID="gvStudent" runat="server" AutoGenerateColumns="False" OnRowDataBound="gvStudent_RowDataBound">
还将“RowIndex”属性添加到任何gridview控件
<asp:TextBox ID="TextBoxName" runat="server" RowIndex='<%# Container.DisplayIndex %>'></asp:TextBox>
代码背后:
protected void gvStudent_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//get name from textbox
TextBox TextBoxName = row.FindControl("TextBoxName") as TextBox;
//get ddl value
DropDownList DropDownListAddress = row.FindControl("DropDownListAddress") as DropDownList;
//get rowindex
int RowIndex = Convert.ToInt32(TextBoxName.Attributes["RowIndex"]);
//get datatable stored in viewstate
DataTable StudentTable = ViewState["StudentTable"] as DataTable;
//assign values to controls
TextBoxName.Text = StudentTable.Rows[RowIndex]["Name"].ToString();
DropDownListAddress.SelectedValue = StudentTable.Rows[RowIndex]["Address"].ToString();
}
}