关于gridview控件和DetailsView控件的问题
我是新手。。最好有一个具体例子参考一下。。1:求教如何利用DetailsView控件向数据库插入数据...我开启插入选项后,如何获得Detailsview文本输入框的内容?(使插入选项生效,最好有一个插入操作的完整例子,谢谢大神们)
2:关于gridview控件,我设置了一个查询功能,但是查询之后,编辑功能不能正确实现,就是查询之后,我点其中一项的编辑按钮,出来的编辑框所在行不是我点的那个,这样的如何解决?这是后台代码:
我的后台代码如下:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
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;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bind();
}
}
public void bind()
{
string sqlstr = "select * from rs_user";
string strCon = "Server=WIN-JNVKFSPA4HB\\MYSERVER;Database=User;Trusted_Connection=True";
SqlConnection sqlcon = new SqlConnection(strCon);
SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
DataSet myds = new DataSet();
sqlcon.Open();
myda.Fill(myds, "rs_user");
GridView1.DataSource = myds;
GridView1.DataKeyNames = new string[] { "id" };
GridView1.DataBind();
sqlcon.Close();
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
bind();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string strCon = "Server=WIN-JNVKFSPA4HB\\MYSERVER;Database=User;Trusted_Connection=True";
SqlConnection sqlcon = new SqlConnection(strCon);
string sqlstr = "update rs_user set realname='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[0].Controls[0])).Text.ToString().Trim() + "',易做图='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',age='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() + "',data_name='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "',position='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString().Trim() + "',rztime='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[5].Controls[0])).Text.ToString().Trim() + "',mobile='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[6].Controls[0])).Text.ToString().Trim() + "',address='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[7].Controls[0])).Text.ToString().Trim() + "'where id='"
+ GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
SqlCommand sqlcom = new SqlCommand(sqlstr, sqlcon);
sqlcon.Open();
sqlcom.ExecuteNonQuery();
sqlcon.Close();
GridView1.EditIndex = -1;
bind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
bind();
}
protected void Button1_Click(object sender, EventArgs e)
{
string sqlstr = "select * from rs_user where 易做图='" + TextBox1.Text + "'";
string strCon = "Server=WIN-JNVKFSPA4HB\\MYSERVER;Database=User;Trusted_Connection=True";
SqlConnection sqlcon = new SqlConnection(strCon);
SqlCommand sqlcom = new SqlCommand(sqlstr, sqlcon);
SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
DataSet myds = new DataSet();
sqlcon.Open();
myda.Fill(myds, "myda");
GridView1.DataSource = myds;
GridView1.DataKeyNames = new string[] { "id" };
GridView1.DataBind();
sqlcon.Close();
TextBox2.Text = "";
}
补充:.NET技术 , ASP.NET