我希望在第一次加载页面时让我的下拉列表选中第一个值“--select--”,因为我需要强制用户从列表中选择一个事件。我很模糊,因为我需要在page_load执行其他功能来检查复选框启用属性,这样我就不知道在哪里可以将编码添加到dropdownlist的空值。
我确实在页面加载中放入了以下代码,但没有任何反应,“ - 选择 - 也没有出现在下拉列表中。
If Not Page.IsPostBack Then
ddlEventTitle.Items.Insert(0, New ListItem("-select-", String.Empty))
End If
我该怎么做才能修改我的编码?
客户代码:
<%@ Page Language="VB" MasterPageFile="~/MasterPageAdmin.master" AutoEventWireup="false" CodeFile="addbooth.aspx.vb" Inherits="addbooth" title="Untitled Page" %>
<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="asp" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="ContentPlaceHolder2" runat="server"
contentplaceholderid="ContentPlaceHolder2">
<table class="style8" cellpadding="3">
<tr>
<td class="style9">Add New Booth</td>
<td> </td>
</tr>
<tr>
<td class="style9"> </td>
<td> </td>
</tr>
<tr>
<td class="style9">Event Title:</td>
<td>
<asp:SqlDataSource ID="SqlDataSourceEventTitle" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString1 %>" SelectCommand="SELECT DISTINCT [eventID], [eventTitle] FROM [eventinfo]">
</asp:SqlDataSource>
<asp:DropDownList ID="ddlEventTitle" runat="server" DataSourceID="SqlDataSourceEventTitle" DataTextField="eventTitle" DataValueField="eventID" Width="240px" AutoPostBack="True">
</asp:DropDownList>
</td>
</tr>
<tr>
<td class="style9">Booth Alias:</td>
<td>
<asp:TextBox ID="txtBoothAlias" runat="server" Width="235px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style9"> Booth Type:</td>
<td>
<asp:DropDownList ID="ddlBoothType" runat="server" Width="240px" AutoPostBack="True">
<asp:ListItem Value="7">8'x8'</asp:ListItem>
<asp:ListItem Value="10">9'x8'</asp:ListItem>
<asp:ListItem Value="11">12'x50'</asp:ListItem>
<asp:ListItem Value="12">16'x10'</asp:ListItem>
<asp:ListItem Value="13">20'x16'</asp:ListItem>
<asp:ListItem Value="15">32'x10'</asp:ListItem>
<asp:ListItem Value="17">32'x14.5'</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td class="style10" valign="top" colspan="2">Booth Duration:<br />
<font size="2">Tick to activate availability</font></td>
</tr>
<tr>
<td class="style10" valign="top" colspan="2" align="center">
<asp:Panel ID="Panel1" runat="server" Width="328px" Direction="LeftToRight" HorizontalAlign="Center">
<table cellpadding="5" class="style8">
<tr>
<td>
<asp:CheckBox ID="cbWeek1" runat="server" Text="Week 1" TextAlign="Left" OnCheckedChanged="cbWeek1_CheckedChanged" AutoPostBack="true"/>
</td>
<td>
<asp:CheckBox ID="cbWeek2" runat="server" Text="Week 2" TextAlign="Left" OnCheckedChanged="cbWeek2_CheckedChanged" AutoPostBack="true"/>
</td>
<td>
<asp:CheckBox ID="cbWeek3" runat="server" Text="Week 3" TextAlign="Left" OnCheckedChanged="cbWeek3_CheckedChanged" AutoPostBack="true"/>
</td>
<td>
<asp:CheckBox ID="cbWeek4" runat="server" Text="Week 4" TextAlign="Left" OnCheckedChanged="cbWeek4_CheckedChanged" AutoPostBack="true"/>
</td>
</tr>
<tr>
<td>
<asp:CheckBox ID="cbWeek5" runat="server" Text="Week 5" TextAlign="Left" OnCheckedChanged="cbWeek5_CheckedChanged" AutoPostBack="true"/>
</td>
<td>
<asp:CheckBox ID="cbWeek6" runat="server" Text="Week 6" TextAlign="Left" OnCheckedChanged="cbWeek6_CheckedChanged" AutoPostBack="true"/>
</td>
<td>
<asp:CheckBox ID="cbWeek7" runat="server" Text="Week 7" TextAlign="Left" OnCheckedChanged="cbWeek7_CheckedChanged" AutoPostBack="true"/>
</td>
<td>
<asp:CheckBox ID="cbWeek8" runat="server" Text="Week 8" TextAlign="Left" OnCheckedChanged="cbWeek8_CheckedChanged" AutoPostBack="true"/>
</td>
</tr>
</table>
</asp:Panel>
<asp:RoundedCornersExtender ID="Panel1_RoundedCornersExtender"runat="server" Enabled="True" TargetControlID="Panel1" BorderColor="CadetBlue" Color="ButtonFace">
</asp:RoundedCornersExtender>
</td>
</tr>
<tr>
<td class="style11">
</td>
<td class="style11">
<asp:Button ID="ButtonAddNewBooth" runat="server" Text="Add New Booth" Height="50px" />
</td>
</tr>
<tr>
<td class="style9"> </td>
<td> </td>
</tr>
<tr>
<td class="style9" colspan="2">
<asp:GridView ID="GridViewBoothAdded" runat="server" AutoGenerateColumns="False" CellPadding="4" DataSourceID="SqlDataSourceBoothAdded" Font-Size="Small" ForeColor="#333333" GridLines="None" AllowPaging="True" PageSize="5" Visible="False">
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<Columns>
<asp:BoundField DataField="boothAlias" HeaderText="Booth Alias" SortExpression="boothAlias" />
<asp:BoundField DataField="week1" HeaderText="week1" SortExpression="week1" />
<asp:BoundField DataField="week2" HeaderText="week2" SortExpression="week2" />
<asp:BoundField DataField="week3" HeaderText="week3" SortExpression="week3" />
<asp:BoundField DataField="week4" HeaderText="week4" SortExpression="week4" />
<asp:BoundField DataField="week5" HeaderText="week5" SortExpression="week5" />
<asp:BoundField DataField="week6" HeaderText="week6" SortExpression="week6" />
<asp:BoundField DataField="week7" HeaderText="week7" SortExpression="week7" />
<asp:BoundField DataField="week8" HeaderText="week8" SortExpression="week8" />
</Columns>
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<EmptyDataTemplate>
No booth record
</EmptyDataTemplate>
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" Font-Size="Small" ForeColor="White" />
<EditRowStyle BackColor="#999999" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
<asp:RoundedCornersExtender ID="GridViewBoothAdded_RoundedCornersExtender" runat="server" Enabled="True" TargetControlID="GridViewBoothAdded">
</asp:RoundedCornersExtender>
<asp:SqlDataSource ID="SqlDataSourceBoothAdded" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString1 %>"
SelectCommand="SELECT [week1], [week2], [week3], [week4], [week5], [week6], [week7], [week8], [eventID], [boothAlias], [eventTitle] FROM [booth_eventinfo] WHERE ([eventID] = @eventID)">
<SelectParameters>
<asp:ControlParameter ControlID="ddlEventTitle" Name="eventID" PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
</td>
</tr>
</table>
服务器代码:
Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Threading
Imports System.Object
Partial Class addbooth
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
ddlEventTitle.Items.Insert(0, New ListItem("-select-", String.Empty))
End If
Dim eventID As String = ddlEventTitle.SelectedValue
Dim connString As String = ConfigurationManager.ConnectionStrings("ConnectionString1").ConnectionString
' Dim connString As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\CikguTube3.mdf;Integrated Security=True;User Instance=True"
Dim con As SqlConnection = New SqlConnection(connString)
Dim cmdQuery As String = "SELECT * FROM eventinfo WHERE eventID = '" + eventID + "'"
Dim cmd As SqlCommand = New SqlCommand(cmdQuery)
Dim myReader As SqlDataReader
cmd.Connection = con
cmd.CommandType = CommandType.Text
con.Open()
myReader = cmd.ExecuteReader()
While myReader.Read()
Label1.Text = myReader("totalDay").ToString()
End While
myReader.Close()
cmd.ExecuteNonQuery()
cmd.Parameters.Clear()
con.Close()
If Label1.Text = "7" Then
cbWeek1.Enabled = True
cbWeek2.Enabled = False
cbWeek3.Enabled = False
cbWeek4.Enabled = False
cbWeek5.Enabled = False
cbWeek6.Enabled = False
cbWeek7.Enabled = False
cbWeek8.Enabled = False
ElseIf Label1.Text = "14" Then
cbWeek1.Enabled = True
cbWeek2.Enabled = True
cbWeek3.Enabled = False
cbWeek4.Enabled = False
cbWeek5.Enabled = False
cbWeek6.Enabled = False
cbWeek7.Enabled = False
cbWeek8.Enabled = False
End If
End Sub
Protected Sub ButtonAddNewBooth_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ButtonAddNewBooth.Click
Dim connString As String = ConfigurationManager.ConnectionStrings("ConnectionString1").ConnectionString
'"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\eventdb.mdf;Integrated Security=True;User Instance=True"
Dim con As SqlConnection = New SqlConnection(connString)
Dim cmdQuery As String = "INSERT INTO booth(eventID,boothAlias,boothType,week1,week2,week3,week4,week5,week6,week7,week8) VALUES (@eventID,@boothAlias,@boothType,@week1,@week2,@week3,@week4,@week5,@week6,@week7,@week8)"
Dim cmd = New SqlCommand(cmdQuery)
cmd.Connection = con
cmd.CommandType = CommandType.Text
con.Open()
cmd.Parameters.AddWithValue("@eventID", ddlEventTitle.SelectedValue)
cmd.Parameters.AddWithValue("@boothAlias", txtBoothAlias.Text)
cmd.Parameters.AddWithValue("@boothType", ddlBoothType.SelectedValue)
If (cbWeek1.Checked = True) Then
cmd.Parameters.AddWithValue("@week1", "1")
Else
cmd.Parameters.AddWithValue("@week1", "0")
End If
If (cbWeek2.Checked = True) Then
cmd.Parameters.AddWithValue("@week2", "1")
Else
cmd.Parameters.AddWithValue("@week2", "0")
End If
If (cbWeek3.Checked = True) Then
cmd.Parameters.AddWithValue("@week3", "1")
Else
cmd.Parameters.AddWithValue("@week3", "0")
End If
If (cbWeek4.Checked = True) Then
cmd.Parameters.AddWithValue("@week4", "1")
Else
cmd.Parameters.AddWithValue("@week4", "0")
End If
If (cbWeek5.Checked = True) Then
cmd.Parameters.AddWithValue("@week5", "1")
Else
cmd.Parameters.AddWithValue("@week5", "0")
End If
If (cbWeek6.Checked = True) Then
cmd.Parameters.AddWithValue("@week6", "1")
Else
cmd.Parameters.AddWithValue("@week6", "0")
End If
If (cbWeek7.Checked = True) Then
cmd.Parameters.AddWithValue("@week7", "1")
Else
cmd.Parameters.AddWithValue("@week7", "0")
End If
If (cbWeek8.Checked = True) Then
cmd.Parameters.AddWithValue("@week8", "1")
Else
cmd.Parameters.AddWithValue("@week8", "0")
End If
GridViewBoothAdded.DataBind()
cmd.ExecuteNonQuery()
cmd.Parameters.Clear()
con.Close()
End Sub
End Class
答案 0 :(得分:3)
我不喜欢编写这样的代码。我个人不喜欢在aspx文件中保存我的数据库相关信息和查询。我会将该代码移动到codebehind文件中的函数。
所以我会像这样更新我的UI(.aspx文件)。只需简单的下拉列表声明
<asp:DropDownList ID="ddlEventTitle" runat="server"></asp:DropDownList>
在你的代码中,写一个像这样的方法
Private Sub LoadEvents()
Dim cmdString As String = "SELECT DISTINCT eventID, eventTitle FROM eventinfo"
Dim connStr As String = "Data Source=Localhost\SQLEXPRESS;Initial Catalog=Flash2;Integrated Security=True;"
Using conn As New SqlConnection(connStr)
conn.Open()
Using cmd As New SqlCommand(cmdString, conn)
Dim rdr As SqlDataReader = cmd.ExecuteReader()
ddlEventTitle.DataValueField = "eventID"
ddlEventTitle.DataTextField = "eventTitle"
ddlEventTitle.DataSource = rdr
ddlEventTitle.DataBind()
ddlEventTitle.Items.Insert(0, New ListItem("Select Event", "0"))
End Using
End Using
End Sub
现在在页面加载事件中调用此方法
Protected Sub Page_Load(sender As Object, e As EventArgs)
If Not IsPostBack Then
LoadFromDataReader()
End If
End Sub
这应该会为您带来带有事件的下拉列表,其中第一个元素为“选择事件”,值为“0”。