2013-10-03 39 views
0

這是我第一次創建搜索表單。我正在嘗試創建一個當前有兩個字段的表單,以輸入最後一個名字。點擊按鈕後,我想從表格中獲得結果。聽起來很簡單。我瀏覽了網頁,但是無法使用sqldatasource來解決問題。我無法連接如何通過點擊按鈕來觸發查詢。我有這樣的:使用sqldatasource搜索vb.net表格

<asp:SqlDataSource ID="SearchPDS" runat="server" ConnectionString="<%$ ConnectionStrings:IDBConnectionString %>" 
SelectCommand="SELECT * from [tblPatron] WHERE LName = @LName" > 
<SelectParameters> 
<asp:ControlParameter ControlID="txtLName" Name="LName" ConvertEmptyStringToNull="false" PropertyName="Text"/> 
</SelectParameters> 
</asp:SqlDataSource> 

我想顯示在網格中的結果:

<asp:GridView ID="GridView1" runat="server" DataSourceID="SearchPDS" AutoGenerateColumns="true"> 
</asp:GridView> 

而且我寫的代碼背後的按鈕的onclick事件:

Protected Sub BtnSearch_Click(sender As Object, e As EventArgs) Handles BtnSearch.Click 
SearchPDS.SelectCommand = "Select * From tblPatron where LName = @LastName" 
SearchPDS.SelectParameters.Add("LastName", txtLName.Text) 
SearchPDS.DataBind() 
GridView1.DataBind() 
End Sub 
+0

我現在只是在點擊事件上運行查詢,然後將它關閉到gridview。 – Nita

回答

0

我將sqlsource代碼更改爲

<asp:SqlDataSource ID="SearchPDS" runat="server" ConnectionString="<%$ ConnectionStrings:IDBConnectionString %>" 
    SelectCommand="SELECT * from [tblPatron]"> 
</asp:SqlDataSource> 

和運行onclick事件的查詢。

Protected Sub BtnSearch_Click(sender As Object, e As EventArgs) Handles BtnSearch.Click 
    Dim strSearch As String 
    strSearch = "SELECT * FROM tblPatron WHERE (LName Like '%" + txtLName.Text.ToString() + "%')" 
    strSearch = strSearch + "AND (FNAME Like '%" + txtFName.Text.ToString() + "%')" 
    SearchPDS.SelectCommand = strSearch 
    GridSearchResults.Visible = True 
End Sub 

這似乎工作正常。有沒有什麼圓滑的方式來做同樣的工作? 謝謝 〜妮塔