Hello,
I have a SQL database with about 300 company names and corresponding phone numbers. I would like to show a list of linkbuttons titled A-Z and when pressed, rebind the sqldatasource so that my GridView will only show company names that start with that letter.
I know there are some examples on codeproject.com, but they are a bit over my head... besides, I don't mind writing a custom select statement for the OnClick of every linkbutton if that's what I have to do. Problem is I haven't a clue how to write a select statement that will return items who's first letter matches my desired letter?
Any idea?
Thanks,
-Derek
The basic sql statement looks like this:
SELECT *FROM CompanyTableWHERE CompanyNameLIKE'A%';
The percentage is the sql wildcard character that matches any number of characters when combined with the LIKE operator
Since you need to not hard code the letter you are searching for, you'll want to use a parameterized sql statement and you would put the wildcard into the parameters value leaving you with a sql statement like this:
SELECT *FROM CompanyTableWHERE CompanyNameLIKE @.P1;|||
Hey Thanks!
That worked like a charm! I can now filter it based on what linkbutton is pressed. I'm still a bit fuzzy on the parametrized statement though... I understand it and all, but where and how would I set the parameter to each letter? On the OnClick of each linkbutton? If so, I would be still be writing out all 26 OnClick events?
On a side note, I cant believe how simple this filtering thing really is. I think the fellas over at codeproject are really over complicating it ;) Thanks again for your response,
-Derek
|||add a usercontrol to encapsulate the Alphabet linkbuttons
PartialClass AlphabetBarInherits System.Web.UI.UserControlPublic Event Click(ByVal valueAs String)Protected Sub Page_Init(ByVal senderAs Object,ByVal eAs System.EventArgs)Handles Me.Init'dynamically create a series of linkbuttonsFor keycodeAs Integer = 65To 90'one for each letter in the alphabetDim lnkAs New LinkButton lnk.Text = Chr(keycode) lnk.CommandArgument = Chr(keycode)AddHandler lnk.Click,AddressOf onClick'have them all use the same event handlerMe.Controls.Add(lnk)Me.Controls.Add(New LiteralControl(" "))'space them outNext End Sub Private Sub onClick(ByVal senderAs Object,ByVal eAs EventArgs)Dim lnkAs LinkButton =DirectCast(sender, LinkButton)'raise a single event 'using the clicked links commandargument as our events argumentRaiseEvent Click(lnk.CommandArgument)End SubEnd Class
Now drag that control onto your page and add a handler for its new Click event
Protected Sub AlphabetBar1_Click(ByVal valueAs String)Handles AlphabetBar1.Click'value is the alphabet letter that was clicked on in the usercontrol 'now we can build our filterDim filterParameterAs String = value &"%"'... '...End Sub
No comments:
Post a Comment