I may be doing this wrong but this is what I’m trying to do. I have a website that has an announcements area. When the page initially loads a funtion will be called using the current year to run a SQL statement that returns all the announcements from the current year. These are then printed to the screen. Above the announcements are several linkbuttons for other years. Once clicked on I want the page to postback PUTing the year from the button so that once the page loads again and isPostBack is true the page will GET that value and use it in the SQL statement. Right now I’m having a hard time trying to figure out what goes where and need a little help. If you have no idea what I’m asking let me know and I’ll post some pseudocode or something to make it clearer. Thank in advance!
How are you assigning years to the linkbuttons? The text? LinkButton1.Text = 1995?
Is your initial SQL statement inside a block of code to prevent it from being called during postback (!this.IsPostBack if using c#, Not Page.IsPostBack if vb)? On second thought you don’t need to do this, you will query the database twice if you don’t though.
it will postback automatically the value you changed. In the eventhandler function of the linkbutton you should be able to put code like this:
Year = LinkButton1.Text
PopulateDataGrid(Year)
Dim iYear As Integer
Dim _dsAnnouncements As DataSet
Dim sHeadline As String
Dim sMessage As String
Dim dtPosted As DateTime
If Page.IsPostBack = True Then
'
' _dsAnnouncements = dba.announcements_dba.GetAnnouncements_Year(, iYear)
'
Else
'
' _dsAnnouncements = dba.announcements_dba.GetAnnouncements_Year(, Today.Year())
'
End If
'If we have data, then let's list some announcments
If _dsAnnouncements.Tables(0).Rows.Count > 0 Then
'For Each dRow As DataRow In _dsAnnouncements.Tables(0).Rows
'
' Properly format and print data returned
That’s roughly it. LinkButton.Text is set as the corresponding year. Right now my event handlers for the LinkButtons do not have anything. I’m not quite sure what to put there. Hopefully this makes some sense and only needs a little help instead of a complete redo. Thanks for your help so far!
I guess I should note that the above code is in the Page_Load event handler.
Ok, I built an example so I can show you how it should (can) work:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not Page.IsPostBack() Then
' Set the linkbutton to display the past years
LinkButton1.Text = (Today.Year - 1).ToString
' Retrieve the data
PopulateDT(Today.Year)
End If
End Sub
Private Sub PopulateDT(ByVal Year As Integer)
' move DB retrieval code here
' just for testing...
Response.Write("Year selected = " + Year.ToString())
End Sub
Private Sub LinkButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LinkButton1.Click
' Get the year
Dim Year As Integer = Integer.Parse(LinkButton1.Text)
' Retrieve the data
PopulateDT(Year)
End Sub
Basically, move your code to query the database to a single function. I am not sure what software you are using so hopefully it builds the EventHandler code for the link button automatically if you double click it. If it doesn’t, let me know I can give you the code to set the EventHandler.