本文介绍了网格文本框Asp.Net变化值在选定的指数变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个网格上我动态添加控件。现在我想改变选定的索引更改事件的产品成本价和销售价的相应值。问题是我不能得到的特定行。低于code我迄今所做的。
< ASP:GridView控件ID =grdSales=服务器
ShowFooter =真的AutoGenerateColumns =FALSE
CELLPADDING =4的CssClass =table表悬停表条纹
网格线=无OnRowDeleting =grdSales_RowDeletingOnRowCommand =grdSales_RowCommand>
<柱体和GT;
< ASP:BoundField的数据字段=ROWNUMBER的HeaderText =斯诺/> < ASP:的TemplateField的HeaderText =产品>
<&ItemTemplate中GT;
< ASP:DropDownList的ID =ddlProducts=服务器的CssClass =网格控制的形式控制OnSelectedIndexChanged =ddlProducts_SelectedIndexChanged>
< / ASP:DropDownList的>
< / ItemTemplate中>
< / ASP:的TemplateField>
< ASP:的TemplateField的HeaderText =成本价>
<&ItemTemplate中GT;
< ASP:文本框ID =txtCp=服务器的CssClass =网格控制的形式控制>< / ASP:文本框>
< / ItemTemplate中>
< / ASP:的TemplateField>
< ASP:的TemplateField的HeaderText =销售价>
<&ItemTemplate中GT;
< ASP:文本框ID =txtSp=服务器的CssClass =网格控制的形式控制>< / ASP:文本框>
< / ItemTemplate中>
< / ASP:的TemplateField>
< ASP:的TemplateField的HeaderText =单元总数>
<&ItemTemplate中GT;
< ASP:文本框ID =txtUnits=服务器的CssClass =网格控制的形式控制>< / ASP:文本框>
< / ItemTemplate中>
< FooterStyle HorizontalAlign =左/>
< FooterTemplate>
总造价杂志:LT; ASP:标签ID =lblTotalCost=服务器文本=0>< / ASP:标签>
< / FooterTemplate>
< / ASP:的TemplateField>
< ASP:的TemplateField的HeaderText =总成本>
<&ItemTemplate中GT;
< ASP:文本框ID =txtTotal=服务器的CssClass =网格控制的形式控制>< / ASP:文本框>
< / ItemTemplate中>
< FooterStyle HorizontalAlign =左/>
< FooterTemplate>
< ASP:按钮的ID =ButtonAdd=服务器文本=添加新行的CssClass =BTN BTN-小学的OnClick =ButtonAdd_Click/>
< / FooterTemplate>
< / ASP:的TemplateField> < ASP:CommandField中
的ShowDeleteButton =真/>
< /专栏>
< / ASP:GridView的>
和我的code的背后,是
#地区的私有函数
私人无效FirstGridViewRow()
{
DataTable的DT =新的DataTable();
医生的DataRow = NULL;
dt.Columns.Add(新的DataColumn(ROWNUMBER,typeof运算(字符串))); dt.Columns.Add(新的DataColumn(产品,typeof运算(字符串))); dt.Columns.Add(新的DataColumn(CostPrice,typeof运算(字符串)));
dt.Columns.Add(新的DataColumn(SalePrice,typeof运算(字符串)));
dt.Columns.Add(新的DataColumn(TotalUnits,typeof运算(字符串)));
dt.Columns.Add(新的DataColumn(TOTALCOST,typeof运算(字符串)));
博士= dt.NewRow(); 博士[ROWNUMBER] = 1; 博士[产品] =的String.Empty; 博士[CostPrice] =的String.Empty;
博士[SalePrice] =的String.Empty;
博士[TotalUnits] =的String.Empty;
博士[TOTALCOST] =的String.Empty;
dt.Rows.Add(DR); 的ViewState [CurrentTable] = DT; grdSales.DataSource = DT;
grdSales.DataBind();
AddNewRow();
SetRowData();
如果(的ViewState [CurrentTable]!= NULL)
{
数据表dtOld =(数据表)的ViewState [CurrentTable];
DataRow的drCurrentRow = NULL;
INT的rowIndex = Convert.ToInt32(0);
如果(dtOld.Rows.Count→1)
{
dtOld.Rows.Remove(dt.Rows [rowIndex位置]);
drCurrentRow = dtOld.NewRow();
的ViewState [CurrentTable] = dtOld;
grdSales.DataSource = dtOld;
grdSales.DataBind(); 的for(int i = 0; I< grdSales.Rows.Count - 1;我++)
{
grdSales.Rows [I] .Cells [0]。文本= Convert.ToString第(i + 1);
}
设置previousData();
}
} }
私人无效AddNewRow()
{
INT的rowIndex = 0; 如果(的ViewState [CurrentTable]!= NULL)
{
数据表dtCurrentTable =(数据表)的ViewState [CurrentTable];
DataRow的drCurrentRow = NULL;
如果(dtCurrentTable.Rows.Count大于0)
{
的for(int i = 1; I< = dtCurrentTable.Rows.Count;我++)
{ DropDownList的ddlProducts =
(DropDownList的)grdSales.Rows [rowIndex位置] .Cells [1] .FindControl(ddlProducts); 文本框txtCostPrice =
(文本框)grdSales.Rows [rowIndex位置] .Cells [2] .FindControl(txtCp);
文本框txtSalePrice =
(文本框)grdSales.Rows [rowIndex位置] .Cells [3] .FindControl(txtSp);
文本框txtUnits =
(文本框)grdSales.Rows [rowIndex位置] .Cells [4] .FindControl(txtUnits);
文本框txtTotal =
(文本框)grdSales.Rows [rowIndex位置] .Cells [5] .FindControl(txtTotal); drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow [ROWNUMBER] = I + 1;
dtCurrentTable.Rows [我 - 1] [产品] = ddlProducts.SelectedValue; dtCurrentTable.Rows [我 - 1] [CostPrice] = txtCostPrice.Text;
dtCurrentTable.Rows [我 - 1] [SalePrice] = txtSalePrice.Text;
dtCurrentTable.Rows [我 - 1] [TotalUnits] = txtUnits.Text;
dtCurrentTable.Rows [我 - 1] [TOTALCOST] = txtTotal.Text;
rowIndex位置++;
}
dtCurrentTable.Rows.Add(drCurrentRow);
的ViewState [CurrentTable] = dtCurrentTable; grdSales.DataSource = dtCurrentTable;
grdSales.DataBind();
}
}
其他
{
的Response.Write(ViewState中为空);
}
设置previousData();
}
私人无效套装previousData()
{
INT的rowIndex = 0;
如果(的ViewState [CurrentTable]!= NULL)
{
DataTable的DT =(数据表)的ViewState [CurrentTable];
如果(dt.Rows.Count大于0)
{
的for(int i = 0; I< dt.Rows.Count;我++)
{ DropDownList的ddlProducts =
(DropDownList的)grdSales.Rows [rowIndex位置] .Cells [1] .FindControl(ddlProducts); 文本框txtCostPrice =(文本框)grdSales.Rows [rowIndex位置] .Cells [2] .FindControl(txtCp);
文本框txtSalePrice =(文本框)grdSales.Rows [rowIndex位置] .Cells [3] .FindControl(txtSp);
文本框txtUnits =(文本框)grdSales.Rows [rowIndex位置] .Cells [4] .FindControl(txtUnits);
文本框txtTotal =(文本框)grdSales.Rows [rowIndex位置] .Cells [5] .FindControl(txtTotal);
//添加这些行 ddlProducts.DataValueField =键;
ddlProducts.DataTextField =值;
ddlProducts.DataSource = BindProductsDdl();
ddlProducts.DataBind(); // **************** 。ddlProducts.SelectedValue = dt.Rows [I] [产品]的ToString(); txtCostPrice.Text = dt.Rows [I] [CostPrice]的ToString()。
。txtSalePrice.Text = dt.Rows [I] [SalePrice]的ToString();
。txtUnits.Text = dt.Rows [I] [TotalUnits]的ToString();
txtTotal.Text = dt.Rows [I] [TOTALCOST]的ToString()。
rowIndex位置++;
}
}
}
}
私人无效SetRowData()
{
INT的rowIndex = 0; 如果(的ViewState [CurrentTable]!= NULL)
{
数据表dtCurrentTable =(数据表)的ViewState [CurrentTable];
DataRow的drCurrentRow = NULL;
如果(dtCurrentTable.Rows.Count大于0)
{
的for(int i = 1; I< = dtCurrentTable.Rows.Count;我++)
{ DropDownList的ddlProducts =
(DropDownList的)grdSales.Rows [rowIndex位置] .Cells [1] .FindControl(ddlProducts); 文本框txtCostPrice =(文本框)grdSales.Rows [rowIndex位置] .Cells [2] .FindControl(txtCp);
文本框txtSalePrice =(文本框)grdSales.Rows [rowIndex位置] .Cells [3] .FindControl(txtSp);
文本框txtUnits =(文本框)grdSales.Rows [rowIndex位置] .Cells [4] .FindControl(txtUnits);
文本框txtTotal =(文本框)grdSales.Rows [rowIndex位置] .Cells [5] .FindControl(txtTotal); drCurrentRow = dtCurrentTable.NewRow(); drCurrentRow [ROWNUMBER] = I + 1; dtCurrentTable.Rows [我 - 1] [产品] = ddlProducts.Text; dtCurrentTable.Rows [我 - 1] [CostPrice] = txtCostPrice.Text;
dtCurrentTable.Rows [我 - 1] [SalePrice] = txtSalePrice.Text;
dtCurrentTable.Rows [我 - 1] [TotalUnits] = txtUnits.Text;
dtCurrentTable.Rows [我 - 1] [TOTALCOST] = txtTotal.Text; rowIndex位置++;
} 的ViewState [CurrentTable] = dtCurrentTable;
//grvStudentDetails.DataSource = dtCurrentTable;
//grvStudentDetails.DataBind();
}
}
其他
{
的Response.Write(ViewState中为空);
}
//设置previousData();
}
私人无效BindControls()
{
BindWarehouseDdl(); BindStaticLists(); } 私人字典< INT,串> BindProductsDdl()
{
字典< INT,串>获取=新词典< INT,串>();
的for(int i = 0; I< Products.Count;我++)
{
INT ID = Convert.ToInt32(产品[I] .ID);
字符串名称=产品[I] .Name点;
Get.Add(ID,姓名);
}
得到的回报;
}
私人无效BindStaticLists()
{
Classes.CProducts CP =新Classes.CProducts();
产品= cp.GetAll();
}
私人无效BindWarehouseDdl()
{
字典< INT,串>项目=新词典与LT; INT,串>();
清单< Models.MwareHouse>得到=新的List< Models.MwareHouse>();
Classes.CWareHouse CW =新Classes.CWareHouse();
获取= cw.GetAll();
的for(int i = 0; I< get.Count;我++)
{
字符串ID =得到[I] .ID;
字符串名称=得到[I] .Name点;
Items.Add(Convert.ToInt32(ID),姓名); }
ddlWareHouse.DataTextField =值;
ddlWareHouse.DataValueField =键;
ddlWareHouse.DataSource =项目;
ddlWareHouse.DataBind();
}
#endregion 保护无效ddlProducts_SelectedIndexChanged(对象发件人,EventArgs的发送)
{ } 保护无效grdSales_RowCommand(对象发件人,GridViewCommandEventArgs E)
{
GridViewRow克= grdSales.SelectedRow; }
解决方案
解决它
保护无效ddlProducts_SelectedIndexChanged(对象发件人,EventArgs的发送)
{
DropDownList的DDL =(DropDownList的)寄件人;
GridViewRow行=(GridViewRow)ddl.NamingContainer;
文本框txtName的=(文本框)row.FindControl(txtCp);
txtName.Text =*****; }
I have this grid on which I am dynamically adding controls. Now I want to change the respective value of Cost Price and Sale Price on Products selected index changed event. The problem is I cannot get the specific row. Below the code I have done so far.
<asp:GridView ID="grdSales" runat="server"
ShowFooter="True" AutoGenerateColumns="False"
CellPadding="4" CssClass="table table-hover table-striped"
GridLines="None" OnRowDeleting="grdSales_RowDeleting" OnRowCommand="grdSales_RowCommand">
<Columns>
<asp:BoundField DataField="RowNumber" HeaderText="SNo" />
<asp:TemplateField HeaderText="Products">
<ItemTemplate>
<asp:DropDownList ID="ddlProducts" runat="server" CssClass="grid-control form-control" OnSelectedIndexChanged="ddlProducts_SelectedIndexChanged">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Cost Price">
<ItemTemplate>
<asp:TextBox ID="txtCp" runat="server" CssClass="grid-control form-control"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Sale Price">
<ItemTemplate>
<asp:TextBox ID="txtSp" runat="server" CssClass="grid-control form-control"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Total Units">
<ItemTemplate>
<asp:TextBox ID="txtUnits" runat="server" CssClass="grid-control form-control"></asp:TextBox>
</ItemTemplate>
<FooterStyle HorizontalAlign="Left" />
<FooterTemplate>
Total Journal Cost: <asp:Label ID="lblTotalCost" runat="server" Text="0"></asp:Label>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Total Cost">
<ItemTemplate>
<asp:TextBox ID="txtTotal" runat="server" CssClass="grid-control form-control"></asp:TextBox>
</ItemTemplate>
<FooterStyle HorizontalAlign="Left" />
<FooterTemplate>
<asp:Button ID="ButtonAdd" runat="server" Text="Add New Row" CssClass="btn btn-primary" OnClick="ButtonAdd_Click" />
</FooterTemplate>
</asp:TemplateField>
<asp:CommandField
ShowDeleteButton="True" />
</Columns>
</asp:GridView>
and my code behind is
#region Private Functions
private void FirstGridViewRow()
{
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
dt.Columns.Add(new DataColumn("Products", typeof(string)));
dt.Columns.Add(new DataColumn("CostPrice", typeof(string)));
dt.Columns.Add(new DataColumn("SalePrice", typeof(string)));
dt.Columns.Add(new DataColumn("TotalUnits", typeof(string)));
dt.Columns.Add(new DataColumn("TotalCost", typeof(string)));
dr = dt.NewRow();
dr["RowNumber"] = 1;
dr["Products"] = string.Empty;
dr["CostPrice"] = string.Empty;
dr["SalePrice"] = string.Empty;
dr["TotalUnits"] = string.Empty;
dr["TotalCost"] = string.Empty;
dt.Rows.Add(dr);
ViewState["CurrentTable"] = dt;
grdSales.DataSource = dt;
grdSales.DataBind();
AddNewRow();
SetRowData();
if (ViewState["CurrentTable"] != null)
{
DataTable dtOld = (DataTable)ViewState["CurrentTable"];
DataRow drCurrentRow = null;
int rowIndex = Convert.ToInt32(0);
if (dtOld.Rows.Count > 1)
{
dtOld.Rows.Remove(dt.Rows[rowIndex]);
drCurrentRow = dtOld.NewRow();
ViewState["CurrentTable"] = dtOld;
grdSales.DataSource = dtOld;
grdSales.DataBind();
for (int i = 0; i < grdSales.Rows.Count - 1; i++)
{
grdSales.Rows[i].Cells[0].Text = Convert.ToString(i + 1);
}
SetPreviousData();
}
}
}
private void AddNewRow()
{
int rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
DataRow drCurrentRow = null;
if (dtCurrentTable.Rows.Count > 0)
{
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
DropDownList ddlProducts =
(DropDownList)grdSales.Rows[rowIndex].Cells[1].FindControl("ddlProducts");
TextBox txtCostPrice =
(TextBox)grdSales.Rows[rowIndex].Cells[2].FindControl("txtCp");
TextBox txtSalePrice =
(TextBox)grdSales.Rows[rowIndex].Cells[3].FindControl("txtSp");
TextBox txtUnits =
(TextBox)grdSales.Rows[rowIndex].Cells[4].FindControl("txtUnits");
TextBox txtTotal =
(TextBox)grdSales.Rows[rowIndex].Cells[5].FindControl("txtTotal");
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["RowNumber"] = i + 1;
dtCurrentTable.Rows[i - 1]["Products"] = ddlProducts.SelectedValue;
dtCurrentTable.Rows[i - 1]["CostPrice"] = txtCostPrice.Text;
dtCurrentTable.Rows[i - 1]["SalePrice"] = txtSalePrice.Text;
dtCurrentTable.Rows[i - 1]["TotalUnits"] = txtUnits.Text;
dtCurrentTable.Rows[i - 1]["TotalCost"] = txtTotal.Text;
rowIndex++;
}
dtCurrentTable.Rows.Add(drCurrentRow);
ViewState["CurrentTable"] = dtCurrentTable;
grdSales.DataSource = dtCurrentTable;
grdSales.DataBind();
}
}
else
{
Response.Write("ViewState is null");
}
SetPreviousData();
}
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; i++)
{
DropDownList ddlProducts =
(DropDownList)grdSales.Rows[rowIndex].Cells[1].FindControl("ddlProducts");
TextBox txtCostPrice = (TextBox)grdSales.Rows[rowIndex].Cells[2].FindControl("txtCp");
TextBox txtSalePrice = (TextBox)grdSales.Rows[rowIndex].Cells[3].FindControl("txtSp");
TextBox txtUnits = (TextBox)grdSales.Rows[rowIndex].Cells[4].FindControl("txtUnits");
TextBox txtTotal = (TextBox)grdSales.Rows[rowIndex].Cells[5].FindControl("txtTotal");
//Added these lines
ddlProducts.DataValueField = "Key";
ddlProducts.DataTextField = "Value";
ddlProducts.DataSource = BindProductsDdl();
ddlProducts.DataBind();
//****************
ddlProducts.SelectedValue = dt.Rows[i]["Products"].ToString();
txtCostPrice.Text = dt.Rows[i]["CostPrice"].ToString();
txtSalePrice.Text = dt.Rows[i]["SalePrice"].ToString();
txtUnits.Text = dt.Rows[i]["TotalUnits"].ToString();
txtTotal.Text = dt.Rows[i]["TotalCost"].ToString();
rowIndex++;
}
}
}
}
private void SetRowData()
{
int rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
DataRow drCurrentRow = null;
if (dtCurrentTable.Rows.Count > 0)
{
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
DropDownList ddlProducts =
(DropDownList)grdSales.Rows[rowIndex].Cells[1].FindControl("ddlProducts");
TextBox txtCostPrice = (TextBox)grdSales.Rows[rowIndex].Cells[2].FindControl("txtCp");
TextBox txtSalePrice = (TextBox)grdSales.Rows[rowIndex].Cells[3].FindControl("txtSp");
TextBox txtUnits = (TextBox)grdSales.Rows[rowIndex].Cells[4].FindControl("txtUnits");
TextBox txtTotal = (TextBox)grdSales.Rows[rowIndex].Cells[5].FindControl("txtTotal");
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["RowNumber"] = i + 1;
dtCurrentTable.Rows[i - 1]["Products"] = ddlProducts.Text;
dtCurrentTable.Rows[i - 1]["CostPrice"] = txtCostPrice.Text;
dtCurrentTable.Rows[i - 1]["SalePrice"] = txtSalePrice.Text;
dtCurrentTable.Rows[i - 1]["TotalUnits"] = txtUnits.Text;
dtCurrentTable.Rows[i - 1]["TotalCost"] = txtTotal.Text;
rowIndex++;
}
ViewState["CurrentTable"] = dtCurrentTable;
//grvStudentDetails.DataSource = dtCurrentTable;
//grvStudentDetails.DataBind();
}
}
else
{
Response.Write("ViewState is null");
}
//SetPreviousData();
}
private void BindControls()
{
BindWarehouseDdl(); BindStaticLists();
}
private Dictionary<int, string> BindProductsDdl()
{
Dictionary<int, string> Get = new Dictionary<int, string>();
for (int i = 0; i < Products.Count; i++)
{
int id = Convert.ToInt32(Products[i].id);
string name = Products[i].Name;
Get.Add(id, name);
}
return Get;
}
private void BindStaticLists()
{
Classes.CProducts cp = new Classes.CProducts();
Products = cp.GetAll();
}
private void BindWarehouseDdl()
{
Dictionary<int, string> Items = new Dictionary<int, string>();
List<Models.MwareHouse> get = new List<Models.MwareHouse>();
Classes.CWareHouse cw = new Classes.CWareHouse();
get = cw.GetAll();
for (int i = 0; i < get.Count; i++)
{
string id = get[i].id;
string name = get[i].Name;
Items.Add(Convert.ToInt32(id), name);
}
ddlWareHouse.DataTextField = "Value";
ddlWareHouse.DataValueField = "Key";
ddlWareHouse.DataSource = Items;
ddlWareHouse.DataBind();
}
#endregion
protected void ddlProducts_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void grdSales_RowCommand(object sender, GridViewCommandEventArgs e)
{
GridViewRow gr = grdSales.SelectedRow;
}
解决方案
solved it
protected void ddlProducts_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddl = (DropDownList)sender;
GridViewRow row = (GridViewRow)ddl.NamingContainer;
TextBox txtName = (TextBox)row.FindControl("txtCp");
txtName.Text = "*****";
}
这篇关于网格文本框Asp.Net变化值在选定的指数变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!