I have a gridview that displays the contents of a database table, using an sqlDataSource. That part (including inserting, updating and deleting rows) works perfectly. What I am wanting to do is add a “search” function to restrict the rows returned to a user entered name. I have a textbox for the name to be searched and a “Search” button in the headertemplate as such:
<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>
Now, in the RowCommand event handler, I have the following code:
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
The DataBind fails, with the error:
Could not find control ‘txtSearchName’ in ControlParameter ‘username’.
Any suggestions on alternative methods for achieving this?
Thanks.
I found a different way – rather than using a ControlParameter, I just used a “standard” parameter as follows:
This works.