GridView没有填充在Page_Load上,而是填充在PostBack或Refresh上

时间:2021-07-06 10:35:14

In an ASP.NET WebForms application there are just two controls in an aspx page, a DropDownList and a GridView. There is no default selected value of DropDownList on Page_Load. Changing the selection in DropDownList populates GridView accurately.

在ASP.NET WebForms应用程序中,aspx页面中只有两个控件,一个DropDownList和一个GridView。 Page_Load上没有DropDownList的默认选择值。更改DropDownList中的选择会准确填充GridView。

When the page is requested with a URL parameter such as .../View_Details.aspx?C_ID=123, the selected value in DropDownList changes but GridView does not populate for the first time but refreshing the page shows the records for given URL parameter.

当使用URL参数(例如... / View_Details.aspx?C_ID = 123)请求页面时,DropDownList中的选定值会更改,但GridView不会首次填充,但刷新页面会显示给定URL参数的记录。

ASPX markup:

<%@ Page Title="Data" Language="vb" AutoEventWireup="false" MasterPageFile="~/HomePage.Master" CodeBehind="View_Details.aspx.vb" Inherits="App1.View_Details" %>

<asp:Content ID="Content4" ContentPlaceHolderID="BodyCP" runat="server">
<asp:DropDownList ID="CIDCombo" runat="server" DataSourceID="SqlDSCID" DataTextField="CName" DataValueField="CID" AutoPostBack="true"></asp:DropDownList>
<asp:SqlDataSource ID="SqlDSCID" runat="server" ... ></asp:SqlDataSource>

<asp:GridView ID="gvData" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="Fld1" />
        <asp:BoundField DataField="Fld2" />
        ...
    </Columns>
</asp:GridView>
</asp:Content>

Code Behind:

Private C_ID As Long
Dim con As SqlConnection = New SqlConnection(ConfigurationManager.Connect...)
Dim cmd As New SqlCommand()
Dim stSqlQry As String = ""

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    C_ID = CLng(Request.QueryString("C_ID"))
    If IsPostBack Then

    Else
        If C_ID > 0 Then
            CIDCombo.SelectedValue = C_ID.ToString
            LoadGVData(C_ID)
        End If
    End If
End Sub

Private Sub CIDCombo_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles CIDCombo.SelectedIndexChanged
    If CIDCombo.SelectedIndex >= 0 AndAlso CLng(CIDCombo.SelectedValue) > 0 Then
        LoadGVData(CLng(CIDCombo.SelectedValue))
    End If
End Sub

Private Sub LoadGVData(ByVal lnCID As Long)
    Try
        If con.State <> ConnectionState.Open Then con.Open()
        Dim da As SqlDataAdapter = New SqlDataAdapter()
        stSqlQry = "SELECT Fld1, Fld2 ... WHERE CID = @CID"
        da = New SqlDataAdapter()
        cmd = New SqlCommand(stSqlQry, con)
        cmd.Parameters.AddWithValue("@CID", lnCID)
        Dim dtDataTableInc As DataTable = New DataTable("t_Data")
        da.SelectCommand = cmd
        da.Fill(dtDataTableInc)
        'SOME DATA MANIPULATION WITH DATATABLE'

        '****************************************************************************'
        'DEBUG MODE SHOWS DataTable HAS ROWS BUT DON'T SHOW UP FIRST TIME IN GRIDVIEW'
        '****************************************************************************'
        gvData.DataSource = dtDataTableInc
        gvData.DataBind()
    Catch ex As Exception
        'EXCEPTION HANDLING
    Finally
        If con.State <> ConnectionState.Closed Then con.Close()
    End Try
End Sub

1 个解决方案

#1


0  

I see you have AutoEventWireup="false" put it on true.

我看到你有AutoEventWireup =“false”把它设置为true。

Just a general note:

只是一般性说明:

When working with DropDownLists and using AutoPostBack=True make use of an UpdatePanel since the User gets frustrated when he always see a white page flickering :)

当使用DropDownLists并使用AutoPostBack = True时,请使用UpdatePanel,因为当用户总是看到白页闪烁时会感到沮丧:)

if you use an UpdatePanel you use the Onload event to populate your data and put UpdateMode=Conditional

如果您使用UpdatePanel,则使用Onload事件填充数据并将UpdateMode = Conditional

Good luck and happy coding.

祝你好运,编码愉快。

#1


0  

I see you have AutoEventWireup="false" put it on true.

我看到你有AutoEventWireup =“false”把它设置为true。

Just a general note:

只是一般性说明:

When working with DropDownLists and using AutoPostBack=True make use of an UpdatePanel since the User gets frustrated when he always see a white page flickering :)

当使用DropDownLists并使用AutoPostBack = True时,请使用UpdatePanel,因为当用户总是看到白页闪烁时会感到沮丧:)

if you use an UpdatePanel you use the Onload event to populate your data and put UpdateMode=Conditional

如果您使用UpdatePanel,则使用Onload事件填充数据并将UpdateMode = Conditional

Good luck and happy coding.

祝你好运,编码愉快。