gridview 和detailsview配合使用(手动添加,修改数据源)

 //gridview的cs
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;
namespace xianbiancang
{
    public partial class test_Default2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        DataSet ds = new DataSet();
        ds = selectforsimplecontrol.selectfordataset("select * from test");
        this.GridView1.DataSource = ds.Tables[0];
        this.GridView1.DataBind();

        }
        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string txt = GridView1.Rows[GridView1.SelectedIndex].Cells[1].Text.ToString();
            Response.Redirect("~/test/Default.aspx?="+txt);
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            DataSet ds = new DataSet();
            string sqlstr = "select * from test where datediff(d,'" + TextBox1.Text + "',ta)<=datediff(d,'" + TextBox1.Text + "','" + TextBox2.Text + "')";
            ds = selectforsimplecontrol.selectfordataset("select * from test where datediff(d,'"+TextBox1 .Text +"',ta)<=datediff(d,'"+TextBox1 .Text +"','"+TextBox2 .Text +"')");
            this.GridView1.DataSource = ds.Tables[0];
            this.GridView1.DataBind();
        }
}
}
//----------------------------------------------------------------------------------------------
//detailsview---cs
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;
namespace xianbiancang
{
    public partial class test_Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //if (!IsPostBack)
            //{
            string gettxt = Request.QueryString[0].ToString();
                Response.Write(gettxt);
                DataSet ds = new DataSet();
                ds = selectforsimplecontrol.selectfordataset("select * from test where textstring='" + gettxt + "'");
                this.DetailsView1.DataSource = ds.Tables[0];
                this.DetailsView1.DataBind();
            //}
        }
        //protected void DetailsView1_ModeChanged(object sender, EventArgs e)
        //{

        //}
        //public  void detailsviewbind()
        //{
        //    string gettxt = Request.QueryString[0].ToString();
        //    Response.Write(gettxt);
        //    DataSet ds = new DataSet();
        //    ds = selectforsimplecontrol.selectfordataset("select * from test where textstring='" + gettxt + "'");
        //    this.DetailsView1.DataSource = ds.Tables[0];
        //    this.DetailsView1.DataBind();
        //}
        protected void DetailsView1_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
        {
            //detailsviewbind();
            string ghtext = ((TextBox)DetailsView1.FindControl("TextBox2")).Text;
            Response.Write(ghtext);
            DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
        }
        //protected void LinkButton1_Click(object sender, EventArgs e)
        //{
        //    DetailsView1.ChangeMode(DetailsViewMode.Edit);
        //}


        protected void DetailsView1_ModeChanging1(object sender, DetailsViewModeEventArgs e)
        {
            //detailsviewbind();
            DetailsView1.ChangeMode(DetailsViewMode.Edit);
        }
}
}
//------------------------------------------------------------
优化后的代码================================================================
//detailsview---cs
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;
namespace xianbiancang
{
    public partial class qiantaimokuai_cxywmkxg : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                dvyw_bind();
            }
        }

        //protected void dvyw_ModeChanging(object sender, DetailsViewModeEventArgs e)
        //{
        //    dvyw.ChangeMode(DetailsViewMode.Edit);
        //    //dvyw.CurrentMode = DetailsViewMode.Edit;
        //}

        protected void dvyw_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
        {

        }
       
        protected void dvyw_bind()
        {
            string ddh = Request.QueryString[0].ToString();
            DataSet ds = new DataSet();
            ds = selectforsimplecontrol.selectfordataset("select * from chanxiao where ddh='" + ddh + "'");

            dvyw.DataSource = ds.Tables[0];
            dvyw.DataBind();
            dvyw.ChangeMode(DetailsViewMode.Edit);
        }





        protected void dvyw_ItemCommand(object sender, DetailsViewCommandEventArgs e)
        {
            if (e.CommandName == "Edit")
            {
                //((TextBox)dvyw.FindControl("khmc_e")).Enabled = false;
                dvyw.ChangeMode(DetailsViewMode.Edit);
               
               
            }
            if (e.CommandName == "Update")
            {  
                #region 更新字符串
               
                string ddh = ((Label)dvyw.FindControl("ddh_e")).Text;
                string khdh = ((TextBox)dvyw.FindControl("khdh_e")).Text;
                string khmc = ((TextBox)dvyw.FindControl("khmc_e")).Text;
                string yw = ((DropDownList )dvyw.FindControl("yw_e")).SelectedItem .Text   ;
                string yz = ((TextBox)dvyw.FindControl("yz_e")).Text;
                string jdsj = ((TextBox)dvyw.FindControl("jdsj_e")).Text;
                string chsj = ((TextBox)dvyw.FindControl("chsj_e")).Text;
                string cpmx = ((TextBox)dvyw.FindControl("cpmx_e")).Text;
                string cpzl = ((TextBox)dvyw.FindControl("cpzl_e")).Text;
                string ywqrchsj = ((TextBox)dvyw.FindControl("ywqrchsj_e")).Text;
                string sqlstr = "update chanxiao set khdh='"
                    + khdh +
                    "',khmc='" + khmc +
                    "',yw='" + yw +
                    "',yz='" + yz +
                    "',jdsj='" + jdsj +
                    "',chsj='" + chsj +
                    "',cpmx='" + cpmx +
                    "',cpzl='" + cpzl +
                    "',ywqrchsj='" + ywqrchsj +
                    "' where ddh='" + ddh + "'";
                insertdeleteupdata.ins("update chanxiao set khdh='"
                    + khdh +
                    "',khmc='" + khmc +
                    "',yw='" + yw +
                    "',yz='" + yz +
                    "',jdsj='" + jdsj +
                    "',chsj='" + chsj +
                    "',cpmx='" + cpmx +
                    "',cpzl='" + cpzl +
                    "',ywqrchsj='" + ywqrchsj +
                    "' where ddh='" + ddh + "'");

                //Response.Write(khdh);
                #endregion
                dvyw.ChangeMode(DetailsViewMode.ReadOnly);
                dvyw_bind();
            }
            if (e.CommandName == "Cancel")
            {
                dvyw.ChangeMode(DetailsViewMode.ReadOnly);
               
                dvyw_bind();
            }
        }
        protected void dvyw_ModeChanging(object sender, DetailsViewModeEventArgs e)
        {
            //dvyw.ChangeMode(DetailsViewMode.Edit);
            //dvyw.ChangeMode(DetailsViewMode.Edit);
        }
        protected void dvyw_PageIndexChanging(object sender, DetailsViewPageEventArgs e)
        {

        }

你可能感兴趣的:(1.c#,asp.net)