0

这是我第一次创建搜索表单。我正在尝试创建一个当前包含两个字段以输入姓氏和名字的表单。单击按钮时,我想从表格中获取结果。听起来很简单。我浏览了网页,但无法理解如何使用 sqldatasource 进行操作。我无法通过单击按钮连接如何触发查询。我有这个:

<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
4

1 回答 1

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

这似乎工作正常。有没有什么时髦的方式来做同样的工作?谢谢~尼塔

于 2013-10-04T14:28:18.970 回答