Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Datagrid Events - Why won't they fire? 1

Status
Not open for further replies.

ChasBoots

MIS
Jul 23, 2002
24
US
Greetings all... I have learned a wealth of knowledge reading and searching through all the threads in this forum. However, I am at a loss to find a solution to my problem, despite reviewing all relevant posts.

I have a form that is correctly populating a datagrid when a query executes. Unfortunately, whenever I tried to re-sort or advance to another page, I get nothing but a blank page. I have a label embedded on the form that should be changing when the OnSortCommand or OnPageIndexChanged events fire. Unfortunately, the label never changes which is an indication to me that the event is not firing. The pertinent code follows:

Datagrid:
Code:
<asp:datagrid id="QryResults" style="Z-INDEX: 103; LEFT: 8px; POSITION: absolute; TOP: 304px" runat="server" Font-Names="Arial Narrow" Width="888px" OnPageIndexChanged="dg_page" OnSortCommand="dg_sort" AllowSorting="True"></asp:datagrid>

Page_Load (QryResults is the name of the datagrid):
Code:
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim sql As String
        Dim conn As New SqlConnection()
        Dim farm As String

        If Not IsPostBack Then
            farm = farmselect()
            InitializeRptList()
            strConnection = ConfigurationSettings.AppSettings(farm)
            conn.ConnectionString() = strConnection
            conn.Open()

            PopulateApps(conn)
            InitializeServers()
            ConfigQryResults()
            sql = "Select * from lu_appname"
            QryResults.Visible = False
            Bind_QryResults(sql)
            lblSampleData.Text = "Nothing to report yet"
        End If
        Page.DataBind()
        dd_ServerName.Items.Insert(0, "")
        rbFarmSelect.AutoPostBack = True
        'dd_AppName.Items.Remove("")
    End Sub

Binding QryResults data:
Code:
     Private Sub Bind_QryResults(ByVal locsql As String)
        Dim conn As New SqlConnection()
        Dim farm As String

        farm = farmselect()

        strConnection = ConfigurationSettings.AppSettings(farm)
        conn.ConnectionString() = strConnection
        conn.Open()
        sqlCmd = New SqlCommand(locsql, conn)
        da = New SqlDataAdapter(sqlCmd)
        da.Fill(ds)
        QryResults.DataSource = ds
        QryResults.DataBind()
        conn.Close()
    End Sub

Subs for sorting/paginating QryResults:
Code:
    Sub dg_page(ByVal sender As Object, ByVal e As DataGridPageChangedEventArgs) Handles QryResults.PageIndexChanged
        Dim newsql As String
        Dim s, f, g, h, o, w As String

        lblSampleData.Text = "Selected Page Index: " & e.NewPageIndex
        s = lbSql.Items.Item(0).Value
        f = lbSql.Items.Item(1).Value
        g = lbSql.Items.Item(2).Value
        h = lbSql.Items.Item(3).Value
        w = lbSql.Items.Item(4).Value
        o = lbSql.Items.Item(5).Value

        newsql = s & f & g & h & w & o
        QryResults.CurrentPageIndex = e.NewPageIndex
        Bind_QryResults(newsql)
    End Sub

    Sub dg_sort(ByVal sender As Object, ByVal e As DataGridSortCommandEventArgs) Handles QryResults.SortCommand
        Dim newsql As String
        Dim s, f, g, h, o, w As String

        lblSampleData.Text = "Selected Sort column: " & e.SortExpression

        s = lbSql.Items.Item(0).Value
        f = lbSql.Items.Item(1).Value
        g = lbSql.Items.Item(2).Value
        h = lbSql.Items.Item(3).Value
        w = lbSql.Items.Item(4).Value
        o = e.SortExpression

        newsql = s & f & g & h & w & o
        Bind_QryResults(newsql)
    End Sub

One final note. I set AutoGenerateColumns="TRUE" in another sub that configures the datagrid format. I need to autogenerate columns because the SQL commands that run from the form will generate different numbers of columns. All help is appreciated.
 
Thanks for the suggestion but, as you have surmised, I have gone through that article from beginning to end without finding a solution to my problem as yet. I will review it again in case I did miss something but hopefully someone will see a problem in my code or have another suggestion.
 
Unfortunately, the label never changes which is an indication to me that the event is not firing.
I suggest you trace through your code to see if the code block is hit. Simply put a stop on each event.

Jim
 
After several days and much research against a multitude of .NET sites, I finally solved my problem. Simply removing the Page.DataBind() line from my Page_Load procedure took care of it. Do I get negative stars for that? Guess it's one of the pitfalls of learning on the fly instead of in a classroom. Thanks to those of you who viewed this thread and/or provided some insight.
 
that's because your Page.Bind should be in the IsPostBack section of you page load.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top