本文介绍了使用foreach statemet在datalist中没有获取Data键字段值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 大家好, 我有一个 DataList 。我将数据键字段值设置为 costid 。但它没有给出正确的 costid 。而不是给ascii vlaue 67.以下是我的代码。请帮助我。 受保护 无效 BtnUpdate_Click( object sender,EventArgs e) { foreach (DataListItem item in DataList1.Items) { TextBox txt =(TextBox)item.FindControl( TxtSetCost); // int pid = Convert.ToInt32(DataList1.DataKeys [item.ItemIndex] .Value); string costid = DataList1.DataKeyField [item.ItemIndex] .ToString(); // int costid = Convert.ToInt32(DataList1.DataKeyField [item.ItemIndex]); string sql_rqstcost = string .Format( 从RequestMaster中选择SetCost,其中CostId = + costid + ); DataTable dt_cst = obj.Do_NonTransaction(sql_rqstcost); if (dt_cst.Rows.Count == 0 ) { string sql_insert = string .Format( 插入RequestMaster(CustomerId,CostId,SetCost)值( + customerid + , + costid + , + txt。 Text + )); obj.Do_Transaction(sql_insert); } else { string sql_update = string .Format( update RequestMaster set SetCost = + txt.Text + 其中CostId = + costid + ); obj.Do_Transaction(sql_update); } } } 解决方案 尝试如下... string costid = DataList1.DataKeys [item.ItemIndex] .ToString(); Hi all,I have one DataList. I put data key field value as costid. but its not giving proper costid. instead of that giving ascii vlaue 67. Below is my code. Please help me.protected void BtnUpdate_Click(object sender, EventArgs e){ foreach (DataListItem item in DataList1.Items) { TextBox txt = (TextBox)item.FindControl("TxtSetCost"); // int pid = Convert.ToInt32(DataList1.DataKeys[item.ItemIndex].Value); string costid = DataList1.DataKeyField[item.ItemIndex].ToString(); //int costid = Convert.ToInt32(DataList1.DataKeyField[item.ItemIndex]); string sql_rqstcost = string.Format("select SetCost from RequestMaster where CostId=" + costid + ""); DataTable dt_cst = obj.Do_NonTransaction(sql_rqstcost); if (dt_cst.Rows.Count == 0) { string sql_insert = string.Format("insert into RequestMaster(CustomerId,CostId,SetCost) values(" + customerid + "," + costid + "," + txt.Text + ")"); obj.Do_Transaction(sql_insert); } else { string sql_update = string.Format("update RequestMaster set SetCost=" + txt.Text + " where CostId=" + costid + " "); obj.Do_Transaction(sql_update); } }} 解决方案 Try like below...string costid = DataList1.DataKeys[item.ItemIndex].ToString(); 这篇关于使用foreach statemet在datalist中没有获取Data键字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
11-03 11:20