我有一个gridview,它使用sqlDataSource显示数据库表的内容。该部分(包括插入,更新和删除行)完美运行。我想要做的是添加一个“搜索”功能来限制返回到用户输入名称的行。我有一个要搜索的名称的文本框和headertemplate中的“搜索”按钮:
<HeaderTemplate>
<asp:Button ID="btnShowAll" runat="server" CausesValidation="False" CommandName="ShowAll" Text="Show All" Visible="false" />
<asp:Button ID="btnSearch" runat="server" CausesValidation="True" CommandName="Search" Text="Search" ValidationGroup="vldSearch" />
<asp:TextBox ID="txtSearchName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="vldSearchName" runat="server" ErrorMessage="You have to provide an attorney name to search for." Text="*" ControlToValidate="txtSearchName" ValidationGroup="vldSearch" ForeColor="White"></asp:RequiredFieldValidator>
</HeaderTemplate>
现在,在RowCommand事件处理程序中,我有以下代码:
Case "Search"
If Page.IsValid Then
Dim btnSearch As Button = Me.dgAttorneys.HeaderRow.FindControl("btnSearch")
Dim btnShowAll As Button = Me.dgAttorneys.HeaderRow.FindControl("btnShowAll")
Dim txtSearchName As TextBox = Me.dgAttorneys.HeaderRow.FindControl("txtSearchName")
Me.sqlAttorneys.SelectCommand = "SELECT [userID], [login], [username], [password] FROM [attorney] WHERE [username] LIKE '%' + @username + '%' ORDER BY [login]"
Dim parmUserName As New ControlParameter("username", "txtSearchName", "Text")
Me.sqlAttorneys.SelectParameters.Add(parmUserName)
Me.dgAttorneys.DataBind()
Me.dgAttorneys.PageIndex = 0
btnSearch.Visible = False
btnShowAll.Visible = True
txtSearchName.Visible = False
End If
DataBind失败,错误:
无法在ControlParameter'username'中找到控件'txtSearchName'。
有关实现此目标的替代方法的任何建议吗?
感谢。
答案 0 :(得分:0)
我发现了一种不同的方式 - 而不是使用ControlParameter,我只使用了“标准”参数,如下所示:
Me.sqlAttorneys.SelectCommand = "SELECT [userID], [login], [username], [password] FROM [attorney] WHERE [username] LIKE '%' + @username + '%' ORDER BY [login]"
Dim parmUserName As New Parameter("username", DbType.String, txtSearchName.Text)
Me.sqlAttorneys.SelectParameters.Add(parmUserName)
这很有效。