我有一个Dropdownlist" Servers"在表单加载事件期间由下面的代码填充。连接字符串中的数据库有一个服务器名称列表,使用下面的代码可以填充而没有任何问题。
Public Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
Populate_Server()
End If
End Sub
Public Sub Populate_Server()
Dim query As String = "select ServerName, ServerId from Server_Names"
BindDropDownList(DropDownList_Server, query, "ServerName", "ServerId", "")
End Sub
接下来,我有另一个下拉列表" Instances"。要填充到此ddlist中的数据位于另一个服务器/数据库上。因此,根据之前的选择(ddlist_server),我填充数据,如下面的代码所示:
Public Sub Populate_Instance()
Dim State As String
Dim ConnectString As String
Dim SelectSQL As String
Srv = DropDownList_Instance.SelectedItem.Text
DropDownList_Instance.Items.Clear()
ConnectString = "Data Source=" & Srv & ";Initial Catalog=master;Integrated Security=true"
Using Con As New SqlConnection(ConnectString)
Con.Open()
SelectSQL = "Select * from Instance;"
Dim com As SqlCommand = New SqlCommand(SelectSQL, Con)
Dim dr As SqlDataReader = com.ExecuteReader()
Do While dr.Read = True
DropDownList_Database.Items.Add(dr.GetString(0))
Loop
'DropDownList_Database.DataBind()
End Using
End Sub
实例ddlist成功填充,但第一个值是表中的第一条记录,但我的要求是空白。原因是,我想进行服务器端验证,如下面的代码所示:
<asp:RequiredFieldValidator ID="req1" runat="server" Style="top: 85px;
left: 378px; position: absolute; height: 20px; width: 160px" Font- Bold="True" Font-Names="Trebuchet M S" Font-Size="Small"
ControlToValidate="DropDownList_Instance" validationgroup="Info"
ErrorMessage="Select an Instance" InitialValue=0>
</asp:RequiredFieldValidator>
我需要您的帮助才能在ddl_Instance
中将第一个条目留空或验证ddl_Instance
答案 0 :(得分:0)
正如Pikoh所说。
DropDownList_Database.Items.Add("", "0")
添加空白项目。
然后更改您的reuqired字段验证器,使其通过添加知道初始值。
InitialValue="0"