GridView不会在Page_Load上填充,而是在PostBack或Refresh上填充

时间:2015-10-25 19:29:32

标签: asp.net vb.net gridview url-parameters

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

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

ASPX标记:

<%@ 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>

代码背后:

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 个答案:

答案 0 :(得分:0)

我看到你#!/bin/bash sitecode=`cat /etc/hostname`; login="${sitecode}" pass="pass.${sitecode}" host="10.10.10.12" base_name="$(basename "$0")" lock_file="/home/mit/$base_name.lock" trap "rm -f $lock_file" SIGINT SIGTERM if [ -e "$lock_file" ] then echo "$base_name is running already." exit else touch "$lock_file" lftp -u $login,$pass $host << EOF mirror --exclude-glob lost+found/ -n -e --use-cache /music/ /music/ quit EOF rm -f "$lock_file" trap - SIGINT SIGTERM exit fi 将其置于真实状态。

只是一般性说明:

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

如果您使用UpdatePanel,则使用UpdatePanel事件来填充数据 并放Onload

祝你好运,编码愉快。