日期:2014-05-18  浏览次数:20388 次

显示详细,更新不了????
下面这些代码是实现当点击gridview中的详细时候,显示的某条记录的详细信息,我用的是自定义的方式,把字段绑定到textbox上显示出来,然后详细的上面有一个修改的按钮,可是为什么当修改了textbox的值后却没有写进数据库中啊?就是无法获取后来修改的值。要是用模板吧,没有写代码,自动实现很好办,可是里面有个时间字段,要获取更改时候的系统时间就很难。请教大侠们怎么办啊?
C# code
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class show1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request["id"] != null)
        {
            SqlConnection con = new SqlConnection("server=(local);user id=sa;pwd=sa;Database=web");
            con.Open();
            SqlDataAdapter ada = new SqlDataAdapter("select * from ERROR where id=" + Request["id"], con);
            DataSet ds = new DataSet();
            ada.Fill(ds, "ERROR");
            DataRowView rv = ds.Tables["ERROR"].DefaultView[0];
            this.ID.Text = rv["ID"].ToString();
            this.AGENT.Text = rv["AGENT"].ToString();
            this.PNR.Text = rv["INFORM"].ToString();
            this.TextBoxSTATUS.Text = rv["STATUS"].ToString();
        }
       
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Insert();
        Response.Write("<script>alert('success!');window.location.href='D.aspx'</script>"); 
    }
    private void Insert()
    {
        SqlConnection con = db.ceratcon();
        con.Open();
        SqlCommand com = new SqlCommand();
        com.Connection = (SqlConnection)con;
        com.CommandText = "update  [ERROR] set AGENT=@AGENT,INFORM=@INFORM,STATUS=@STATUS,DATETIME1=@DATETIME1 where id="+Request["id"];
        com.Parameters.Add("@AGENT", SqlDbType.VarChar).Value = this.AGENT.Text; ;
        com.Parameters.Add("@INFORM", SqlDbType.VarChar).Value = this.PNR.Text;
        com.Parameters.Add("@STATUS", SqlDbType.VarChar).Value = "DONE";
        com.Parameters.Add("@DATETIME1", SqlDbType.DateTime).Value = DateTime.Now;
        com.ExecuteNonQuery();
        con.Close();
    }
}



------解决方案--------------------
C# code
if(!Page.IsPostBack)
{
      if (Request["id"] != null)
        {
            SqlConnection con = new SqlConnection("server=(local);user id=sa;pwd=sa;Database=web");
            con.Open();
            SqlDataAdapter ada = new SqlDataAdapter("select * from ERROR where id=" + Request["id"], con);
            DataSet ds = new DataSet();
            ada.Fill(ds, "ERROR");
            DataRowView rv = ds.Tables["ERROR"].DefaultView[0];
            this.ID.Text = rv["ID"].ToString();
            this.AGENT.Text = rv["AGENT"].ToString();
            this.PNR.Text = rv["INFORM"].ToString();
            this.TextBoxSTATUS.Text = rv["STATUS"].ToString();
        }

}

------解决方案--------------------
textbox的AotuPoatBack为ture
------解决方案--------------------
要将编辑事件与gridview里的编辑命令关联起来,如下代码你可以参考下:
HTML code
 
    <asp:DataGrid ID="DataGrid1" runat="server" AllowPaging="True" AllowSorting="True"
      AutoGenerateColumns="False" OnPageIndexChanged="DataGrid1_PageIndexChanged" OnSortCommand="DataGrid1_SortCommand"
      PageSize="3" OnSelectedIndexChanged="Dat