如何在gridview中编写搜索按钮的代码..我写下面的代码,但它有一些错误,所以如何写 [英] how to write the code for search button in gridview .. am write the below code but it have some errors so how to write
问题描述
这是我的代码
SqlConnection con = new SqlConnection(Data Source ...)
protected void Page_Load(object sender ,EventArgs e)
{
BindEmployeeDetails();
}
protected void BindEmployeeDetails()
{
con.Open();
SqlCommand cmd = new SqlCommand( select * from insertdetails,con);
SqlDataAdapter da = new SqlDataAdapter(select * from insertdetails,con);
DataSet ds = new DataSet();
da.Fill(ds,insertdetails);
GridView1.DataSource = ds.Tables [insertdetails];
GridView1.DataBind();
con.Close();
}
protected void Button1_Click(object sender,EventArgs e)
{
con.Open();
SqlCommand cmd1 = new SqlCommand(select * from insertdetails wher e username =''+ TextBox1.Text +'',con);
BindndEmployeeDetails();
con.Close();
}
更改您的查询...以获得更好的结果..
SqlCommand cmd1 = new SqlCommand( select * from insertdetails其中用户名如'%%',con);
和你的代码应该是
< pre lang =c#> SqlConnection con = new SqlConnection( 数据源......);
受保护 void Page_Load( object sender,EventArgs e)
{
string command = select * from insertdetails;
BindEmployeeDetails(command);
}
受保护 void BindEmployeeDetails(< span class =code-keyword> string command)
{
con.Open();
SqlCommand cmd = new SqlCommand(command);
SqlDataAdapter da = new SqlDataAdapter(cmd,con);
DataSet ds = new DataSet();
da.Fill(ds, insertdetails);
GridView1.DataSource = ds.Tables [ insertdetails];
GridView1.DataBind();
con.Close();
}
受保护 void Button1_Click( object sender,EventArgs e)
{
string command = select * from insertdetails其中用户名如'% + TextBox1.Text + < span class =code-string>%';
BindndEmployeeDetails(command);
}
首先从gridview事件中找到控件
this is my code
SqlConnection con= new SqlConnection("Data Source...)
protected void Page_Load(object sender, EventArgs e)
{
BindEmployeeDetails();
}
protected void BindEmployeeDetails()
{
con.Open();
SqlCommand cmd= new SqlCommand("select *from insertdetails",con);
SqlDataAdapter da = new SqlDataAdapter("select *from insertdetails", con);
DataSet ds= new DataSet();
da.Fill(ds,"insertdetails");
GridView1.DataSource = ds.Tables["insertdetails"];
GridView1.DataBind();
con.Close();
}
protected void Button1_Click(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd1= new SqlCommand(" select * from insertdetails where username=''"+TextBox1.Text+"''",con);
BindndEmployeeDetails();
con.Close();
}
change your query...for more better results..
SqlCommand cmd1= new SqlCommand(" select * from insertdetails where username like '%"+TextBox1.Text+"%'",con);
and your code should be
SqlConnection con= new SqlConnection("Data Source..."); protected void Page_Load(object sender, EventArgs e) { string command = "select * from insertdetails"; BindEmployeeDetails(command); } protected void BindEmployeeDetails(string command) { con.Open(); SqlCommand cmd= new SqlCommand(command); SqlDataAdapter da = new SqlDataAdapter(cmd, con); DataSet ds= new DataSet(); da.Fill(ds,"insertdetails"); GridView1.DataSource = ds.Tables["insertdetails"]; GridView1.DataBind(); con.Close(); } protected void Button1_Click(object sender, EventArgs e) { string command = "select * from insertdetails where username like '%"+TextBox1.Text+"%'"; BindndEmployeeDetails(command); }
First find the control from gridview event
这篇关于如何在gridview中编写搜索按钮的代码..我写下面的代码,但它有一些错误,所以如何写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!