2017-03-07 71 views
0

我想做一個簡單的ASP.NET應用程序,從插入,刪除,編輯,搜索選項從mdb文件中繪製數據。在ASP.NET上Gridview搜索

當我在我的頁面上添加Gridview和sqldatasource後,我從Gridview任務中選擇了Datasource。我選中啓用編輯,啓用從那裏刪除。對於插入,我添加了一個文本框和一個按鈕。對於我寫的這個按鈕。

OleDbConnection con= new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Server.MapPath("App_Data\\veritabani.mdb")); 
con.Open(); 
string name= txtname.Text; 
string lname= txtlname.Text; 
string idno= txtidno.Text; 
string job= jobbox.SelectedItem.ToString(); 
string age= txtage.Text; 
OleDbCommand add= new OleDbCommand("INSERT INTO Tablo1(name, lname, idno, job, age) values ('" + name + "','" + lname+ "','" + idno + "','" + job+ "','" + age+ "')", con); 
int check= add.ExecuteNonQuery(); 
if (check> 0) 
    lblInfo.Text = "<font color=green>Successfull</font>"; 
else 
    lblInfo.Text = "<font color=red>Unsuccessfull.</font>"; 
con.Close(); 
con.Dispose(); 

插入工作正常。但我無法創建搜索按鈕。在我的搜索按鈕上寫什麼?我寫了這個,但我沒有工作。

OleDbConnection con= new OleDbConnection(); 
     OleDbCommand komut = new OleDbCommand(); 
     komut.Connection = con; 
     komut.CommandText = "SELECT * FROM Tablo1 WHERE name LIKE '%" + namesearch.Text + "%'"; 
     OleDbDataAdapter da = new OleDbDataAdapter(komut); 
     DataTable dt = new DataTable(); 
     da.Fill(dt); 
     GridView1.DataSource = dt; 
     GridView1.DataBind(); 

我不希望使用AJAX,DevExpress的等

回答

1

ASPX

<asp:TextBox ID="Text1" runat="server" ToolTip="Search"></asp:TextBox> 
<asp:Button ID="Button1" runat="server" Text="Search" onclick="Button1_Click" ToolTip="Search" /> 

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:LocalSqlServer %>" 
       SelectCommand="SELECT * FROM [ Tablo1]" 
       FilterExpression="name LIKE '%{0}%'"> 
       <FilterParameters> 
        <asp:ControlParameter Name="name" ControlID="Text1" PropertyName="Text" /> 
       </FilterParameters> 
    </asp:SqlDataSource> 

CS

protected void Button1_Click(object sender, EventArgs e) 
    { 
      GridView1.DataBind(); 
    } 
1
FilterExpression="name +' '+ lname LIKE '%{0}%'" 
+0

它工作但如果姓氏文本框爲空,則不起作用。我希望它用輸入的值進行搜索。 (兩者或一個) – esege

+1

在SqlDataSource1 和ControlParameter'ConvertEmptyStringToNull =「false」上設置'CancelSelectOnNullParameter =「false」'' – Shady