你可以解释一下我可以用母版的组合框我在组合框中设置数据库名称,并通过子页调用 代码如下 尝试
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
答案 0 :(得分: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
}
}
现在在会话中执行相同的检查并在子页面中回发值。我想这是在母版页中执行代码的小时差,子页面在设置之前试图访问会话变量。所以,只需检查会话并回发即可双重确认。