0

你能解释一下我能用母版页的组合框做什么吗?我在组合框中设置数据库名称,我通过子页面调用代码如下尝试

        sqlConn = New SqlConnection(strConn)
        sqlConn.Open()
        Dim sqlda1 As SqlClient.SqlDataAdapter
        Dim sqlds1 As New DataSet

        sqlQry = "EXEC sp_databases"
        sqlda1 = New SqlDataAdapter(sqlQry, sqlConn)
        sqlda1.Fill(sqlds1, "DataBaseList")
        sqlConn.Close()
        Session.Add("dscb", " ")
        Session.Add("dscb", sqlds1.Tables("DataBaseList"))

        Dim IntPCount As Integer

        IntPCount = sqlds1.Tables("DataBaseList").Rows.Count
        'IntPCount = Session("dscb")
        Dim PCol As String
        ' cbDataBaseList.Items.Clear()
        cbDataBaseList.Items.Clear()
        cbDataBaseList.Text = "Select"

        For p = 0 To IntPCount - 1
            PCol = sqlds1.Tables("DataBaseList").Rows(p).Item("DATABASE_NAME").ToString
            If PCol.Length > 5 Then
                If PCol <> "master" Or PCol <> "msdb" Or PCol <> "tempdb" Then
                    Dim strDataBaseName As String = PCol
                    strDataBaseName = strDataBaseName.Remove(4)
                    '*** Add only "Customer's" which got the prefix "CLT_ " ***
                    If strDataBaseName = "CLT_" Then
                        NewPCol = PCol.Replace("CLT_", "")
                        NewPCol.Trim()
                        'cbDataBaseList.Items.Add(NewPCol)
                        cbDataBaseList.Items.Add(NewPCol)
                    End If
                End If
            End If
        Next
                Session.Add("CMBID", "")

        'cbDataBaseList.Text = NewPCol.ToString

        sqlds1.Dispose()
    Catch ex As Exception
        strex = ex.Message
        'MessageBox.Show(strex, "General form exception message 5.", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        End If

选择索引更改事件

会话(“CMBID”)= cbDataBaseList.Text

4

1 回答 1

0

上面提到的代码是母版页的Page_Load()函数吗?如果是,那么您必须先检查

// Check if it has already been set.
if(Session("CMBID") != null) 
{
// Now if it is set, get the value
 String cmbid = (String) Session("CMBID");
//Get the post back value of the combo box  also
String cmbval = Request["<your control name>"];
// Make the above code of setting the combo box value as a separate function
FillComboBoxFromDB();
etif(!cmbval.Equals(cmbid))
{
   // set the session value with the new selected value
   Session("CMBID") = cmbval
 }


}

现在在会话中执行相同的检查并在子页面中回发值。我猜这是在母版页中执行代码的微小时间差中,子页面在设置会话变量之前尝试访问它。因此,只需检查会话并发回以确保双重确定。

于 2012-04-12T10:17:47.550 回答