2012-10-27 112 views
0

我有一個下拉列表,獲取基於3個其他控件(如下)的值的數據填充。DropDownList內容不更新

有2個問題,我有:

1)在初始頁面加載底部的下拉列表是空的,只有與其他控件的一個觸發回發後的數據填充。有沒有簡單的方法讓它在初始頁面加載時填充正確的數據?

2)如果我從代碼隱藏中刪除UpdatePanel.Update(),那麼只要有一個由其他控件觸發的回發,底部下拉列表中的數據就無法更新。下拉列表中的數據是否應該在這種情況下自動更新,而不需要UpdatePanel.Update()

我希望這解釋清楚,足以讓某人明白我在這裏所得到的。

<asp:SqlDataSource ID="srcTheatreList" runat="server" ConnectionString="<%$ ConnectionStrings:database %>" SelectCommand="SELECT theatreID, name FROM Theatre"></asp:SqlDataSource> 
<asp:SqlDataSource ID="srcMovieList" runat="server" ConnectionString="<%$ ConnectionStrings:database %>" SelectCommand="SELECT movieID, title FROM Movie"></asp:SqlDataSource> 
<asp:SqlDataSource ID="srcSessionTimes" runat="server" ConnectionString="<%$ ConnectionStrings:database %>" 
    SelectCommand="SELECT s.time FROM Session s INNER JOIN Movie m ON (s.movieID = m.movieID) INNER JOIN Theatre t ON (s.theatreID = t.theatreID) WHERE [email protected] AND [email protected] AND [email protected]"> 
    <SelectParameters> 
     <asp:ControlParameter Name="theatreID" ControlID="ddlTheatreList" PropertyName="SelectedValue" /> 
     <asp:ControlParameter Name="movieID" ControlID="ddlMovieList" PropertyName="SelectedValue" /> 
     <asp:ControlParameter Name="date" ControlID="txtDate" PropertyName="Text" DefaultValue="23/09/2012" /> 
    </SelectParameters> 
</asp:SqlDataSource> 

<asp:UpdatePanel ID="UpdatePanel" runat="server"> 
<ContentTemplate> 
    <table> 
    <tr><td>Select theatre:</td><td><asp:DropDownList ID="ddlTheatreList" runat="server" DataSourceID="srcTheatreList" DataTextField="name" DataValueField="theatreID" AutoPostBack="True"></asp:DropDownList></td></tr> 
    <tr><td>Select movie:</td><td><asp:DropDownList ID="ddlMovieList" runat="server" DataSourceID="srcMovieList" DataTextField="title" DataValueField="movieID" AutoPostBack="True"></asp:DropDownList></td></tr> 
    <tr><td>Select date:</td><td><asp:TextBox ID="txtDate" runat="server" AutoPostBack="True" Text="23/09/2012"></asp:TextBox><img id="calendarButton2" src="~/images/calendar.png" alt="" runat="server" /> 
    <asp:CalendarExtender ID="CalendarExtender" runat="server" TargetControlID="txtDate" TodaysDateFormat="d MMMM yyyy" DaysModeTitleFormat="MMMM yyyy"></asp:CalendarExtender></td></tr> 
    <tr><td>Select time:</td><td><asp:DropDownList ID="ddlSessionTimes" runat="server" AutoPostBack="True"></asp:DropDownList></td></tr> 
    </table><br /> 
</ContentTemplate> 
</asp:UpdatePanel> 

代碼隱藏:

Protected Sub UpdateSessionTimes(sender As Object, e As EventArgs) Handles ddlMovieList.SelectedIndexChanged, ddlTheatreList.SelectedIndexChanged, txtDate.TextChanged 
    Dim dv As DataView = srcSessionTimes.Select(DataSourceSelectArguments.Empty) 
    ddlSessionTimes.DataSource = dv 
    ddlSessionTimes.DataTextField = "time" 
    ddlSessionTimes.DataValueField = "time" 
    ddlSessionTimes.DataBind() 
    UpdatePanel.Update() 
End Sub 

回答

0

自己的方式與選擇參數來訪問SqlDataSource的是錯的.. 這裏是一個示例代碼..

<asp:SqlDataSource ID="SqlDataSourceGridView" runat="server" 
    ProviderName="System.Data.SqlClient" 
    SelectCommand="SELECT [Name],[Address] FROM [Table_Emp] where Id = @Id" 
    OnSelecting="SqlDataSourceGridView_Selecting"> 

    <SelectParameters> 
     <asp:Parameter Name="Id" Type="Int32" /> 
    </SelectParameters> 

</asp:SqlDataSource> 

3) in Selecting event method put parameter value to the command 

protected void SqlDataSourceGridView_Selecting(object sender, SqlDataSourceSelectingEventArgs e) 
{ 
    e.Command.Parameters["@Id"].Value = (int)(Id ?? 0); // nullable ind is not applicable here 
} 

和不寫任何問題的意見:)