C# GridView中的ASP.NET dropdownlist不断清除
我在网格视图中有两个下拉列表。选择第一个(EqpCatDDL)将确定第二个(descrippddl)将填充哪些值 但是,每次我单击Add New Row按钮时,DescriptPDDL dropdownlist就会清除。我不知道为什么会这样。代码如下: aspx:C# GridView中的ASP.NET dropdownlist不断清除,c#,asp.net,dropdown,C#,Asp.net,Dropdown,我在网格视图中有两个下拉列表。选择第一个(EqpCatDDL)将确定第二个(descrippddl)将填充哪些值 但是,每次我单击Add New Row按钮时,DescriptPDDL dropdownlist就会清除。我不知道为什么会这样。代码如下: aspx: c#: 受保护的无效页面加载(对象发送方,事件参数e) { 如果(!Page.IsPostBack) { SetInitialRow();//创建要绑定到GridView的数据表 } } 私有void SetInitialRow(
c#:
受保护的无效页面加载(对象发送方,事件参数e)
{
如果(!Page.IsPostBack)
{
SetInitialRow();//创建要绑定到GridView的数据表
}
}
私有void SetInitialRow()
{
DataTable dt=新的DataTable();
数据行dr=null;
添加(新数据列(“S/N”,类型为(字符串));
Add(新的数据列(“Column1”,typeof(string));
Add(新的数据列(“Column2”,typeof(string));
Add(新的数据列(“Column3”,typeof(string));
Add(新的数据列(“Column4”,typeof(string));
dr=dt.NewRow();
dr[“序列号”]=1;
dr[“Column1”]=string.Empty;
dr[“Column2”]=string.Empty;
dr[“Column3”]=string.Empty;
dr[“Column4”]=string.Empty;
dt.Rows.Add(dr);
//将数据表存储在ViewState中
视图状态[“当前表”]=dt;
Gridview1.DataSource=dt;
Gridview1.DataBind();
}
受保护的无效按钮单击(对象发送者,事件参数e)
{
AddNewRowToGrid();
GridViewRow=(GridViewRow)((发送者作为按钮).NamingContainer);
Button btn=行。FindControl(“ButtonAd”)作为按钮;
ScriptManager.GetCurrent(this.RegisterAsyncPostBackControl(btn);
}
私有void AddNewRowToGrid()
{
int rowIndex=0;
如果(ViewState[“CurrentTable”]!=null)
{
DataTable dtCurrentTable=(DataTable)视图状态[“CurrentTable”];
DataRow drCurrentRow=null;
如果(dtCurrentTable.Rows.Count>0)
{
//提取文本框值
对于(int i=1;i 0)
{
对于(int i=0;i<(dt.Rows.Count-1);i++)
{
DropDownList ddl1=(DropDownList)Gridview1.Rows[rowIndex].Cells[1].FindControl(“EqpCatDDL”);
DropDownList ddl2=(DropDownList)Gridview1.Rows[rowIndex].Cells[2].FindControl(“descripPDDL”);
TextBox box3=(TextBox)Gridview1.Rows[rowIndex]。单元格[3]。FindControl(“TextBox 3”);
TextBox box4=(TextBox)Gridview1.Rows[rowIndex]。单元格[4]。FindControl(“TextBox 4”);
ddl1.Text=dt.Rows[i][“Column1”].ToString();
ddl2.Text=dt.Rows[i][“Column2”].ToString();
box3.Text=dt.Rows[i][“Column3”].ToString();
box4.Text=dt.Rows[i][“Column4”].ToString();
rowIndex++;
}
}
}
}
//从EqpCategory表中的EqpCat列填充项目名称dropdownlist
受保护的void Gridview1_RowDataBound(对象发送方,GridViewRowEventArgs e)
{
如果(e.Row.RowType==DataControlRowType.DataRow)
{
string connString=ConfigurationManager.ConnectionString[“MyDbConn”].ConnectionString;
SqlConnection连接=新的SqlConnection(connString);
//在该行中找到DropDownList
DropDownList EqpCatDDL=(e.Row.FindControl(“EqpCatDDL”)作为DropDownList);
SqlCommand cmd1=新的SqlCommand(“从EqpCategory中选择不同的EqpCat”,连接);
cmd1.Connection.Open();
SqlDataReader ddlValues1;
ddlValues1=cmd1.ExecuteReader();
EqpCatDDL.DataSource=ddlValues1;
EqpCatDDL.DataTextField=“EqpCat”;
EqpCatDDL.DataValueField=“EqpCat”;
EqpCatDDL.DataBind();
cmd1.Connection.Close();
cmd1.Connection.Dispose();
//在下拉列表中添加默认项
插入(0,新的ListItem(“--Select-->”);
}
}
//根据项目名称dropdownlist的选定值填充描述dropdownlist
受保护的无效设备\u SelectedIndexChanged(对象发送方,事件参数e)
{
string connString=ConfigurationManager.ConnectionString[“MyDbConn”].ConnectionString;
SqlConnection连接=新的SqlConnection(connString);
GridViewRow gvr=(GridViewRow)((DropDownList)发送方).Parent.Parent;
DropDownList EqpCatDDL=gvr.FindControl(“EqpCatDDL”)作为DropDownList;
DropDownList descrippddl=gvr.FindControl(“descrippddl”)作为DropDownList;
字符串cate=EqpCatDDL.SelectedValue.ToString();
SqlCommand cmd2=新SqlCommand(“从EqpCat='“+cate+'”,连接的EqpCategory中选择描述);
cmd2.Connection.Open();
SqlDataReader ddlValues2;
ddlValues2=cmd2.ExecuteReader();
descrippddl.DataSource=ddlValues2;
descrippddl.DataTextField=“Description”;
descrippddl.DataValueField=“Description”;
descrippddl.DataBind();
cmd2.Connection.Close();
cmd2.Connection.Dispose();
//在下拉列表中添加默认项
描述
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel runat="server">
<ContentTemplate>
<asp:GridView ID="Gridview1" runat="server" ShowFooter="True" AutoGenerateColumns="False"
OnRowDataBound="Gridview1_RowDataBound">
<Columns>
<asp:BoundField DataField="S/N" HeaderText="S/N" />
<asp:TemplateField HeaderText="Item Name">
<ItemTemplate>
<asp:DropDownList ID="EqpCatDDL" runat="server" AutoPostBack="true" OnSelectedIndexChanged="EqpCatDDL_SelectedIndexChanged">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Description">
<ItemTemplate>
<asp:DropDownList ID="DescripDDL" runat="server" AutoPostBack="true">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Quantity">
<ItemTemplate>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Remarks">
<ItemTemplate>
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
</ItemTemplate>
<FooterStyle HorizontalAlign="Right" />
<FooterTemplate>
<contenttemplate>
<asp:Button ID="ButtonAdd" onclick="ButtonAdd_Click" runat="server" Text="Add New Row" />
</contenttemplate>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
SetInitialRow(); //create a datatable to bind to GridView
}
}
private void SetInitialRow()
{
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn("S/N", typeof(string)));
dt.Columns.Add(new DataColumn("Column1", typeof(string)));
dt.Columns.Add(new DataColumn("Column2", typeof(string)));
dt.Columns.Add(new DataColumn("Column3", typeof(string)));
dt.Columns.Add(new DataColumn("Column4", typeof(string)));
dr = dt.NewRow();
dr["S/N"] = 1;
dr["Column1"] = string.Empty;
dr["Column2"] = string.Empty;
dr["Column3"] = string.Empty;
dr["Column4"] = string.Empty;
dt.Rows.Add(dr);
//Store the DataTable in ViewState
ViewState["CurrentTable"] = dt;
Gridview1.DataSource = dt;
Gridview1.DataBind();
}
protected void ButtonAdd_Click(object sender, EventArgs e)
{
AddNewRowToGrid();
GridViewRow row = (GridViewRow)((sender as Button).NamingContainer);
Button btn = row.FindControl("ButtonAdd") as Button;
ScriptManager.GetCurrent(this).RegisterAsyncPostBackControl(btn);
}
private void AddNewRowToGrid()
{
int rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
DataRow drCurrentRow = null;
if (dtCurrentTable.Rows.Count > 0)
{
//extract the TextBox values
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
//Locate controls in GridView
DropDownList ddl1 = (DropDownList)Gridview1.Rows[rowIndex].Cells[1].FindControl("EqpCatDDL");
DropDownList ddl2 = (DropDownList)Gridview1.Rows[rowIndex].Cells[2].FindControl("DescripDDL");
TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox3");
TextBox box4 = (TextBox)Gridview1.Rows[rowIndex].Cells[4].FindControl("TextBox4");
//Save control values into DataTable
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["S/N"] = i + 1;
drCurrentRow["Column1"] = ddl1.SelectedValue.ToString();
drCurrentRow["Column2"] = ddl2.SelectedValue.ToString();
drCurrentRow["Column3"] = box3.Text;
drCurrentRow["Column4"] = box4.Text;
dtCurrentTable.Rows[rowIndex]["Column1"] = ddl1.SelectedValue.ToString();
dtCurrentTable.Rows[rowIndex]["Column2"] = ddl2.SelectedValue.ToString();
dtCurrentTable.Rows[rowIndex]["Column3"] = box3.Text;
dtCurrentTable.Rows[rowIndex]["Column4"] = box4.Text;
rowIndex++;
}
//add new row to DataTable
dtCurrentTable.Rows.Add(drCurrentRow);
//Store the current data to ViewState
ViewState["CurrentTable"] = dtCurrentTable;
//Rebind the Grid with the current data
Gridview1.DataSource = dtCurrentTable;
Gridview1.DataBind();
}
}
else
{
Response.Write("ViewState is null");
}
SetPreviousData(); //Set Previous Data from DataTable to GridView on Postbacks from Add New Row button click
}
private void SetPreviousData()
{
int rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dt = (DataTable)ViewState["CurrentTable"];
if (dt.Rows.Count > 0)
{
for (int i = 0; i < (dt.Rows.Count-1); i++)
{
DropDownList ddl1 = (DropDownList)Gridview1.Rows[rowIndex].Cells[1].FindControl("EqpCatDDL");
DropDownList ddl2 = (DropDownList)Gridview1.Rows[rowIndex].Cells[2].FindControl("DescripDDL");
TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox3");
TextBox box4 = (TextBox)Gridview1.Rows[rowIndex].Cells[4].FindControl("TextBox4");
ddl1.Text = dt.Rows[i]["Column1"].ToString();
ddl2.Text = dt.Rows[i]["Column2"].ToString();
box3.Text = dt.Rows[i]["Column3"].ToString();
box4.Text = dt.Rows[i]["Column4"].ToString();
rowIndex++;
}
}
}
}
//Populate Item Name dropdownlist from EqpCat column in EqpCategory table
protected void Gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string connString = ConfigurationManager.ConnectionStrings["MyDbConn"].ConnectionString;
SqlConnection connection = new SqlConnection(connString);
//Find the DropDownList in the Row
DropDownList EqpCatDDL = (e.Row.FindControl("EqpCatDDL") as DropDownList);
SqlCommand cmd1 = new SqlCommand("SELECT DISTINCT EqpCat FROM EqpCategory", connection);
cmd1.Connection.Open();
SqlDataReader ddlValues1;
ddlValues1 = cmd1.ExecuteReader();
EqpCatDDL.DataSource = ddlValues1;
EqpCatDDL.DataTextField = "EqpCat";
EqpCatDDL.DataValueField = "EqpCat";
EqpCatDDL.DataBind();
cmd1.Connection.Close();
cmd1.Connection.Dispose();
//Add Default Item in the DropDownList
EqpCatDDL.Items.Insert(0, new ListItem("--Select--"));
}
}
//Populate Description dropdownlist based on selected value of Item Name dropdownlist
protected void EqpCatDDL_SelectedIndexChanged(object sender, EventArgs e)
{
string connString = ConfigurationManager.ConnectionStrings["MyDbConn"].ConnectionString;
SqlConnection connection = new SqlConnection(connString);
GridViewRow gvr = (GridViewRow)((DropDownList)sender).Parent.Parent;
DropDownList EqpCatDDL = gvr.FindControl("EqpCatDDL") as DropDownList;
DropDownList DescripDDL = gvr.FindControl("DescripDDL") as DropDownList;
string cate = EqpCatDDL.SelectedValue.ToString();
SqlCommand cmd2 = new SqlCommand("SELECT Description FROM EqpCategory WHERE EqpCat = '" + cate + "'", connection);
cmd2.Connection.Open();
SqlDataReader ddlValues2;
ddlValues2 = cmd2.ExecuteReader();
DescripDDL.DataSource = ddlValues2;
DescripDDL.DataTextField = "Description";
DescripDDL.DataValueField = "Description";
DescripDDL.DataBind();
cmd2.Connection.Close();
cmd2.Connection.Dispose();
//Add Default Item in the DropDownList
DescripDDL.Items.Insert(0, new ListItem("--Select--"));
}
private void SetPreviousData()
{
int rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dt = (DataTable)ViewState["CurrentTable"];
if (dt.Rows.Count > 0)
{
for (int i = 0; i < (dt.Rows.Count - 1); i++)
{
DropDownList ddl1 = (DropDownList)Gridview1.Rows[rowIndex].Cells[1].FindControl("EqpCatDDL");
DropDownList ddl2 = (DropDownList)Gridview1.Rows[rowIndex].Cells[2].FindControl("DescripDDL");
TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox3");
TextBox box4 = (TextBox)Gridview1.Rows[rowIndex].Cells[4].FindControl("TextBox4");
ddl1.SelectedValue = dt.Rows[i]["Column1"].ToString();
string connString = ConfigurationManager.ConnectionStrings["MyDbConn"].ConnectionString;
SqlConnection connection = new SqlConnection(connString);
SqlCommand cmd2 = new SqlCommand(""SELECT Description FROM EqpCategory WHERE EqpCat = '" + Convert.ToInt32(ddl1.SelectedValue) + "'", connection);
cmd2.Connection.Open();
SqlDataReader ddlValues2;
ddlValues2 = cmd2.ExecuteReader();
ddl2.DataTextField = "Description";
ddl2.DataValueField = "Description";
ddl2.DataSource = ddlValues2;
ddl2.DataBind();
cmd2.Connection.Close();
cmd2.Connection.Dispose();
ddl2.SelectedValue = dt.Rows[i]["Column2"].ToString();
box3.Text = dt.Rows[i]["Column3"].ToString();
box4.Text = dt.Rows[i]["Column4"].ToString();
rowIndex++;
}
}
}
}
ddl1.SelectedValue = dt.Rows[i]["Column1"].ToString();
ddl2.SelectedValue = dt.Rows[i]["Column2"].ToString();