Answered by:
gridview not loading in pageload event

Question
-
User1064293087 posted
This should be simple. I have searched all day and can't find out why only the button event loads and populates the gridview on my page.
Here is the page:
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
<p>
<br />
<H1>Job Descriptions</H1>
</p>
<p>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" ShowHeaderWhenEmpty="True">
<Columns>
<asp:HyperLinkField HeaderText="Name" DataTextField="Name" DataNavigateUrlFields="Name"
DataNavigateUrlFormatString="~/Resources/JobDesc/{0}" Target="_blank" >
</asp:HyperLinkField>
<asp:BoundField DataField="CreationTime" DataFormatString=""{0:MMMM d, yyyy}"" HeaderText="Created Date" />
</Columns>
</asp:GridView>
</p>
<p>
<asp:Button ID="Button1" runat="server" Text="Button" />
</p>
</asp:Content>
Here is the codebehind:
Public Class JobDesc
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Dim dinfo As DirectoryInfo = New DirectoryInfo(Server.MapPath("~/Resources/JobDesc"))
GridView1.DataSource = dinfo.GetFiles
GridView1.DataBind()
End Sub
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim dinfo As DirectoryInfo = New DirectoryInfo(Server.MapPath("~/Resources/JobDesc"))
GridView1.DataSource = dinfo.GetFiles
GridView1.DataBind()
End Sub
End Class
Can anyone see what I am missing here? It works from the button click but not in the page load event.
Thanks
Wednesday, December 13, 2017 9:44 PM
Answers
-
User2103319870 posted
flprogrammer
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.ClickMake sure you have a Handles Me.Load added on your PageLoad code like below
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load End Sub
Also you should load the gridview with data inside if Not IsPostback block like below
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Not IsPostBack Then Dim dinfo As DirectoryInfo = New DirectoryInfo(Server.MapPath("~/Resources/JobDesc")) GridView1.DataSource = dinfo.GetFiles GridView1.DataBind() End If End Sub
If you are still didnt get the Gridview populated then put a breakpoint and see if debugger hits the pageload event at runtime
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, December 13, 2017 9:57 PM
All replies
-
User2103319870 posted
flprogrammer
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.ClickMake sure you have a Handles Me.Load added on your PageLoad code like below
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load End Sub
Also you should load the gridview with data inside if Not IsPostback block like below
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Not IsPostBack Then Dim dinfo As DirectoryInfo = New DirectoryInfo(Server.MapPath("~/Resources/JobDesc")) GridView1.DataSource = dinfo.GetFiles GridView1.DataBind() End If End Sub
If you are still didnt get the Gridview populated then put a breakpoint and see if debugger hits the pageload event at runtime
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, December 13, 2017 9:57 PM -
User1064293087 posted
That did it a2h. Many thanks.
Thursday, December 14, 2017 1:12 PM