下面这些代码是实现当点击gridview中的详细时候,显示的某条记录的详细信息,我用的是自定义的方式,把字段绑定到textbox上显示出来,然后详细的上面有一个修改的按钮,可是为什么当修改了textbox的值后却没有写进数据库中啊?就是无法获取后来修改的值。要是用模板吧,没有写代码,自动实现很好办,可是里面有个时间字段,要获取更改时候的系统时间就很难。请教大侠们怎么办啊?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();
    }
}

解决方案 »

  1.   

    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();
            }}
      

  2.   

    要将编辑事件与gridview里的编辑命令关联起来,如下代码你可以参考下:        <asp:DataGrid ID="DataGrid1" runat="server" AllowPaging="True" AllowSorting="True"
                AutoGenerateColumns="False" OnPageIndexChanged="DataGrid1_PageIndexChanged" OnSortCommand="DataGrid1_SortCommand"
                PageSize="3" OnSelectedIndexChanged="DataGrid1_SelectedIndexChanged" OnItemDataBound="DataGrid1_ItemDataBound" OnDeleteCommand="DataGrid1_DeleteCommand" OnEditCommand="DataGrid1_EditCommand" OnCancelCommand="DataGrid1_CancelCommand" OnUpdateCommand="DataGrid1_UpdateCommand">
                <Columns>
                    <asp:BoundColumn DataField="stuID" HeaderText="学号" SortExpression="stuID" ReadOnly="True"></asp:BoundColumn>
                    <asp:BoundColumn DataField="stuName" HeaderText="姓名" ReadOnly="True"></asp:BoundColumn>
                    <asp:TemplateColumn HeaderText="性别">
                        <ItemTemplate>
                            <asp:Label runat="server"  ID ="labSex" Text='<%# DataBinder.Eval(Container, "DataItem.stuSex") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            &nbsp;<asp:TextBox ID="TextBox1" runat="server"  Text='<%# DataBinder.Eval(Container, "DataItem.stuSex") %>' CausesValidation="True"></asp:TextBox>
                            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="*性别不能为空" ControlToValidate="TextBox1"></asp:RequiredFieldValidator>
                        </EditItemTemplate>
                    </asp:TemplateColumn>
                    <asp:ButtonColumn CommandName="Select" HeaderText="备注" Text="查看"></asp:ButtonColumn>
                    <asp:EditCommandColumn CancelText="取消" EditText="编辑" UpdateText="更新"></asp:EditCommandColumn>
                    <asp:ButtonColumn CommandName="Delete" Text="删除"></asp:ButtonColumn>
                    <asp:TemplateColumn>
                        <ItemTemplate>
                            <asp:CheckBox ID="CheckBox1" runat="server" />
                        </ItemTemplate>
                    </asp:TemplateColumn>
                </Columns>
                <PagerStyle Mode="NumericPages" />
                <AlternatingItemStyle BackColor="#FFFFC0" />
                <ItemStyle BackColor="#FFC0C0" />
            </asp:DataGrid>处理事件:
      protected void DataGrid1_EditCommand(object source, DataGridCommandEventArgs e)
        {
            this.DataGrid1.EditItemIndex = e.Item.ItemIndex;
            this.BindWithView();
        }
     protected void DataGrid1_UpdateCommand(object source, DataGridCommandEventArgs e)
        {
            string sex = ((TextBox )(e.Item.Cells[2].FindControl("TextBox1"))).Text ;
            //string sex = ((TextBox)(e.Item.Cells[2].Controls[0])).Text;  //产生异常
            string id=this.DataGrid1 .DataKeys [e.Item .ItemIndex ].ToString ();
            SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["constr"]);
            SqlCommand cmd = new SqlCommand("update proTest set stuSex=@sex where stuId=@id", con);
            SqlParameter par = new SqlParameter("@sex", SqlDbType.Char, 5);
            par.Value =sex;
            cmd.Parameters .Add (par);
            par =new SqlParameter ("@id",SqlDbType .Char ,10);
            par.Value =id;
            cmd.Parameters .Add (par);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            this.DataGrid1.EditItemIndex = -1;
            this.BindWithView();
        }