手动绑定DetailsView编辑时,数据无法更新
我手动绑定DetailsView时,但是运行时,单击编辑按钮,修改表中数据后,数据更新不了,还是原来未修改的原因,不知道什么原因,代码如下,希望大家能指导下public partial class CheckResult : System.Web.UI.Page
{
SqlConnection sqlcon;
string strCon = "Data Source=.;Initial Catalog=dkkDB;Integrated Security=True;Uid=sa;Pwd=123456 ";
private void Bind()
{
string ID = Session["name"].ToString();
string sqlstr = "select * from dkk_companyinfo where orgcode =" + ID;
sqlcon = new SqlConnection(strCon);
SqlDataAdapter adp = new SqlDataAdapter(sqlstr, sqlcon);
DataSet mys = new DataSet();
sqlcon.Open();
adp.Fill(mys, "dkk_companyinfo");
DetailsView1.DataSource = mys;
DetailsView1.DataKeyNames = new string[] { "orgcode" };
DetailsView1.DataBind();
sqlcon.Close();
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Bind();
}
}
protected void DetailsView_PageIndexChanging(object sender, DetailsViewPageEventArgs e)
{
DetailsView1.PageIndex = e.NewPageIndex;
}
protected void DetailsView1_Deleting(object sender, DetailsViewDeleteEventArgs e)
{
string charAreaCode = this.DetailsView1.DataKey.Value.ToString();
Bind();
}
protected void DetailsView1_ModeChaning(object sender, DetailsViewModeEventArgs e)
{
this.DetailsView1.ChangeMode(e.NewMode);
Bind();
}
protected void DetailsView1_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
{
string charAreaCode = this.DetailsView1.DataKey.Value.ToString();
dkk_Companyinfo Comp = new dkk_CompanyinfoDAL().Selctbyid(charAreaCode);
int i;
= charAreaCode;
for (i = 0; i < e.NewValues.Count;i++ )
{
e.NewValues[i] = Server.HtmlEncode(e.NewValues[i].ToString());
}
for (i = 0; i < e.NewValues.Count-1; i++)
{
switch (i)
{
case 0: = e.NewValues[i].ToString(); break;
case 1: = e.NewValues[i].ToString(); break;
case 2: Comp.Txadress = e.NewValues[i].ToString(); break;
case 3: Comp.Postcode = e.NewValues[i].ToString(); break;
case 4: Comp.Telephone = e.NewValues[i].ToString(); break;
case 5: Comp.Faxnumber = e.NewValues[i].ToString(); break;
case 6: Comp.Mail = e.NewValues[i].ToString(); break;
case 7: Comp.Date = Convert.ToDateTime(e.NewValues[i]); break; ;
case 8: Comp.Character = e.NewValues[i].ToString(); break;
case 9: Comp.Type = e.NewValues[i].ToString(); break;
case 10: Comp.Registercode = e.NewValues[i].ToString(); break;
case 11: Comp.Duedate = Convert.ToDateTime(e.NewValues[i]); break;
case 12: Comp.Financecode = e.NewValues[i].ToString(); break;
case 13: Comp.Division = e.NewValues[i].ToString(); break;
case 14: Comp.Classfied = e.NewValues[i].ToString(); break;
case 15: Comp.Sqare = float.Parse(e.NewValues[i].ToString()); break;
case 16: Comp.Owner = e.NewValues[i].ToString(); break;
default: break;
}
}
if(new dkk_CompanyinfoDAL().Update(Comp))
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>alert('更新成功')</script>");
}
new dkk_CompanyinfoDAL().Update(Comp);
Bind();
}
protected void DetailView1_Inserting(object sender, DetailsViewInsertEventArgs e)
{
int i;
for (i = 0; i < e.Values.Count; i++)
{
if (e.Values[i] != null)
{
e.Values[i] = Server.HtmlEncode(e.Values[i].ToString());
}
}
Bind();
}
}