2012-05-20 91 views
2

當我需要強制用戶從列表中選擇一個事件時,我想讓我的下拉列表選擇第一個值「--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>&nbsp;</td> 
</tr> 
<tr> 
<td class="style9">&nbsp;</td> 
<td>&nbsp;</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&#39;x8&#39;</asp:ListItem> 
<asp:ListItem Value="10">9&#39;x8&#39;</asp:ListItem> 
<asp:ListItem Value="11">12&#39;x50&#39;</asp:ListItem> 
<asp:ListItem Value="12">16&#39;x10&#39;</asp:ListItem> 
<asp:ListItem Value="13">20&#39;x16&#39;</asp:ListItem> 
<asp:ListItem Value="15">32&#39;x10&#39;</asp:ListItem> 
<asp:ListItem Value="17">32&#39;x14.5&#39;</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"> &nbsp;</td> 
    <td>&nbsp;</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 

回答

3

我不喜歡這樣寫代碼的人。我個人不喜歡在aspx文件中保存我的數據庫相關的信息和查詢。我將該代碼移動到代碼隱藏文件中的函數。

所以我會像這樣更新我的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」的第一個元素「選擇事件」。

相關問題