2013-10-25 93 views
1

我有兩個DropDownList與更新面板,當我從第一個dropdownlist中選擇值,然後在第二個dropdownlist顯示數據相關第一。但在運行時出現問題,無法顯示下拉列表並不斷刷新此頁面。DropDownList與updatepanel中的問題

所以請幫助解決這個問題。

這裏是我的代碼aspx頁面

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AddRoomPrice.aspx.cs" Inherits="AddRoomPrice" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
<head> 
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> 
    <title>Presha Admin</title> 
    <style type="text/css"> 
     @import url("css/style.css"); 
     @import url('css/style_text.css'); 
     @import url('css/form-buttons.css'); 
     @import url('css/link-buttons.css'); 
     @import url('css/menu.css'); 
     @import url('css/statics.css'); 
     @import url('css/messages.css'); 
     @import url('css/table-sorter.css'); 
     @import url('css/tabs.css'); 
     @import url('css/forms.css'); 
     @import url('css/datepicker.css'); 
     @import url('css/jquery.fancybox-1.3.4.css'); 
     @import url('css/accordions.css'); 
     @import url('css/jquery.treeview.css'); 
     @import url('css/wysiwyg.css'); 
     @import url('css/wysiwyg.modal.css'); 
     @import url('css/wysiwyg-editor.css'); 
    </style> 

    <script type="text/javascript" src="js/jquery-1.6.2.min.js"></script> 

    <link rel="shortcut icon" href="gfx/Favicon.png"> 

    <link rel="stylesheet" type="text/css" href="css/component.css" /> 
    <script type="text/javascript" src="js/modernizr.custom.js"></script> 

    <!--[if lte IE 8]> 
     <script type="text/javascript" src="js/excanvas.min.js"></script> 
    <![endif]--> 


</head> 
<body> 
<form id="form1" runat="server"> 
<asp:ScriptManager ID="ScriptManager1" runat="server"> 
</asp:ScriptManager> 
    <div class="container"> 
     <div class="logo-labels"> 
      <a href="Default.aspx"><img id="logo" runat="server" src="~/gfx/logo.png" height="50" /></a> 
      <ul> 
       <li><asp:Label ID="lblAdmin" runat="server" Text="Wel come "></asp:Label></li> 
       <li class="logout"><asp:LinkButton ID="lnkLogout" runat="server" onclick="lnkLogout_Click"><span>Logout</span></asp:LinkButton></li> 
      </ul> 
     </div> 
     <div class="menu-search"> 
      <ul> 
       <li><a href="Default.aspx">Home</a></li> 
       <li><a href="#">Hotel</a> 
        <ul> 
         <li><a href="HotelList.aspx">Hotel List</a></li> 
         <li><a href="AddHotel.aspx">Add Hotel</a></li> 
        </ul> 
       </li> 
       <li><a href="#">Hotel Album</a> 
        <ul> 
         <li><a href="HotelAlbumList.aspx">Album List</a></li> 
         <li><a href="AddHotelAlbum.aspx">Add Album</a></li> 
        </ul> 
       </li> 
       <li><a href="#">Hotel Facility</a> 
        <ul> 
         <li><a href="HotelFacilityList.aspx">Facility List</a></li> 
         <li><a href="AddHotelFacility.aspx">Add Facility</a></li> 
        </ul> 
       </li> 
       <li><a href="#">Category</a> 
        <ul> 
         <li><a href="RoomCategoryList.aspx">Category List</a></li> 
         <li><a href="AddRoomCategory.aspx">Add Category</a></li> 
        </ul> 
       </li> 
       <li class="current"><a href="#">Room Price</a> 
        <ul> 
         <li><a href="RoomPriceList.aspx">Price List</a></li> 
         <li><a href="AddRoomPrice.aspx">Add Price</a></li> 
        </ul> 
       </li> 
       <li><a href="AddFacility.aspx">New Facility</a></li> 
       <li><a href="#">Admin</a> 
        <ul> 
         <li><a href="AdminList.aspx">Admin List</a></li> 
         <li><a href="AddAdmin.aspx">Add Admin</a></li> 
        </ul> 
       </li> 
       <li><a href="Inquiry.aspx">Inquiry</a></li> 
       <li><a href="GetLatitudeLongitude.aspx" target="_blank">Map</a></li> 

      </ul> 
     </div> 
     <div class="breadcrumbs"> 
      <ul> 
       <li class="home"><a href="Default.aspx"></a></li> 
       <li class="break">&#187;</li> 
       <li><a href="AddRoomPrice.aspx">Add Room Price</a></li> 
      </ul> 
     </div> 
     <div class="section"> 
      <div class="box"> 
       <div class="title"> 
        <h2>Add/Edit Room Category</h2> 
       </div> 

       <div class="content forms"> 
        <asp:UpdatePanel ID="UpdatePanel1" runat="server"> 
         <ContentTemplate> 
          <div class="line padding-left50"> 
           <label>Hotel</label> 
           <asp:DropDownList ID="ddlHotel" runat="server" AutoPostBack="true" 
            onselectedindexchanged="ddlHotel_SelectedIndexChanged"> 
           </asp:DropDownList> 
          </div> 
         </ContentTemplate> 
         <Triggers> 
          <asp:AsyncPostBackTrigger ControlID="ddlHotel" EventName="selectedindexchanged" /> 
         </Triggers> 
        </asp:UpdatePanel> 

        <asp:UpdatePanel ID="UpdatePanel2" runat="server"> 
         <ContentTemplate> 
          <div class="line padding-left50"> 
           <label>Room Category</label> 
           <asp:DropDownList ID="ddlCategory" runat="server"> 
           </asp:DropDownList> 
          </div> 
         </ContentTemplate> 
         <Triggers> 
          <asp:AsyncPostBackTrigger ControlID="ddlHotel" EventName="selectedindexchanged" /> 
         </Triggers> 
        </asp:UpdatePanel> 
        <div class="line padding-left50"> 
         <label>Single Price</label> 
         <asp:TextBox ID="txtSingle" runat="server" class="medium"></asp:TextBox> 
        </div> 
        <div class="line padding-left50"> 
         <label>Double Price</label> 
         <asp:TextBox ID="txtDouble" runat="server" class="medium"></asp:TextBox> 
        </div> 
        <div class="line padding-left50"> 
         <label>Extra Bed Price</label> 
         <asp:TextBox ID="txtExtraBed" runat="server" class="medium"></asp:TextBox> 
        </div> 
        <div class="line padding-left50"> 
         <label>Meal Plan</label> 
         <asp:TextBox ID="txtMealPlan" runat="server" class="medium"></asp:TextBox> 
        </div> 
        <div class="line padding-left50"> 
         <label>Extra Meal Price</label> 
         <asp:TextBox ID="txtExtraMeal" runat="server" class="medium"></asp:TextBox> 
        </div> 

        <div class="line button" style="padding-left:325px"> 
         <asp:Button ID="btnSubmit" runat="server" Text="Sumbit" CssClass="btn btn-2 btn-2d" onclick="btnSubmit_Click"></asp:Button> 
        </div> 
       </div> 
      </div> 
     </div> 
    </div> 

    <script type="text/javascript" src="js/superfish.js"></script> 
    <script type="text/javascript" src="js/supersubs.js"></script> 
    <script type="text/javascript" src="js/hoverIntent.js"></script> 
    <script type="text/javascript" src="js/jquery.flot.js"></script> 
    <script type="text/javascript" src="js/jquery.graphtable-0.2.js"></script> 
    <script type="text/javascript" src="js/jquery-ui.js"></script> 
    <script type="text/javascript" src="js/customInput.jquery.js"></script> 
    <script type="text/javascript" src="js/jquery.tablesorter.js"></script> 
    <script type="text/javascript" src="js/jquery.tablesorter.pager.js"></script> 
    <script type="text/javascript" src="js/jquery.sparkbox-select.js"></script> 
    <script type="text/javascript" src="js/jquery.fancybox-1.3.4.js"></script> 
    <script type="text/javascript" src="js/jquery.filestyle.mini.js"></script> 
    <script type="text/javascript" src="js/date.js"></script> 
    <script type="text/javascript" src="js/jquery.datepicker.js"></script> 
    <script type="text/javascript" src="js/jquery.treeview.js"></script> 
    <script type="text/javascript" src="js/jquery.tipsy.js"></script> 
    <script type="text/javascript" src="js/jquery.wysiwyg.js"></script> 
    <script type="text/javascript" src="js/plugins/wysiwyg.rmFormat.js"></script> 
    <script type="text/javascript" src="js/controls/wysiwyg.image.js"></script> 
    <script type="text/javascript" src="js/controls/wysiwyg.link.js"></script> 
    <script type="text/javascript" src="js/controls/wysiwyg.table.js"></script> 
    <script type="text/javascript" src="js/inline.js"></script> 

</form> 
</body> 
</html> 

這裏是CS代碼

using System; 
using System.Collections; 
using System.Configuration; 
using System.Data; 
using System.Linq; 
using System.Web; 
using System.Web.Security; 
using System.Web.UI; 
using System.Web.UI.HtmlControls; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using System.Xml.Linq; 
using BLL; 

public partial class AddRoomPrice : System.Web.UI.Page 
{ 
    clsbll bll = new clsbll(); 

    protected void Page_Load(object sender, EventArgs e) 
    { 
     if (Session["admin"] != null) 
     { 
      lblAdmin.Text = "Wel Come " + Session["admin"].ToString(); 
     } 
     else 
     { 
      Response.Redirect("Login.aspx"); 
     } 

     if (!Page.IsPostBack) 
     { 
      if (Session["hotelID"] != null) 
      { 
       ddlHotel.SelectedValue = Session["hotelID"].ToString(); 
      } 
      ddlHotel.DataSource = bll.getHotel(); 
      ddlHotel.DataTextField = "HName"; 
      ddlHotel.DataValueField = "HotelID"; 
      ddlHotel.DataBind(); 
      ddlHotel.Items.Insert(0, "Select Hotel"); 
     } 
    } 
    protected void btnSubmit_Click(object sender, EventArgs e) 
    { 
     bll.insertRoomPrice(txtSingle.Text, txtDouble.Text, txtExtraBed.Text, txtMealPlan.Text, txtExtraMeal.Text, Convert.ToInt32(ddlCategory.SelectedValue), Convert.ToInt32(ddlHotel.SelectedValue)); 

     txtSingle.Text = ""; 
     txtDouble.Text = ""; 
     txtExtraBed.Text = ""; 
     txtMealPlan.Text = ""; 
     txtExtraMeal.Text = ""; 
     ddlCategory.SelectedIndex = 0; 
     ddlHotel.SelectedIndex = 0; 
     Session.Remove("hotelID"); 
     //Session.Remove("hotel"); 
    } 
    protected void ddlHotel_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     ddlCategory.DataSource = bll.getCategoryByID(ddlHotel.SelectedValue); 
     ddlCategory.DataTextField = "RCategoryNameBed"; 
     ddlCategory.DataValueField = "RCategoryID"; 
     ddlCategory.DataBind(); 
     ddlCategory.Items.Insert(0, "Select Category"); 
    } 

    protected void lnkLogout_Click(object sender, EventArgs e) 
    { 
     Session.Remove("admin"); 
     Response.Redirect("Login.aspx"); 
    } 
} 

所以請儘快回覆

感謝

+0

我認爲這一定是頁面回發問題。你有沒有調試你的代碼? – Monika

+0

是的,我調試我的代碼,它不斷調用page_load()事件。 –

+0

這是回傳事件問題。 – Monika

回答

1

這不是你調用頁面加載函數,這是ASP.NET的工作方式。頁面發佈到自身,從而調用page_load函數,當頁面上的任何服務器控件被觸發(設置爲回發)。

你需要做的是把一些檢查的初始頁面加載和後背部

if(!IsPostBack) 
{ 
//Code when initial loading 
} 
else 
{ 
// code when post back 
} 
0

UpdatePanel控件的設計只刷新其內容。

用於過濾的目的,你可以選擇下列方法之一:

  • 所有相關DropDownLists必須放在一個UpdatePanel內。

  • 首先UpdatePanel的,其內容必須嵌套在第二UpdatePanel的。(不推薦)

沒有必要在這兩種方法申報AsyncPostBackTrigger。

好運。

+0

你好,Perseus,我試過這個,也嘗試刪除兩個更新的面板,然後運行這個代碼,然後它不斷刷新頁面。 –

+0

嗨VIPUL,我想你應該改變你的Page_Load代碼。 (如下): 'if(!Page.IsPostBack){if(Session [「admin」]!= null)lblAdmin.Text =「歡迎來到」+ Session [「admin」 ]的ToString(); else Response.Redirect(「Login.aspx」); if(Session [「hotelID」]!= null)ddlHotel.SelectedValue = Session [「hotelID」]。ToString(); ddlHotel.DataSource = bll.getHotel(); ddlHotel.DataTextField =「HName」; ddlHotel.DataValueField =「HotelID」; ddlHotel.DataBind(); ddlHotel.Items.Insert(0,「Select Hotel」); }' – Perseus

+0

我試過這個,但是在第二個下拉列表中不顯示 –

0

區分也許下面的代碼解決該問題:

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!IsPostBack) 
    { 
     if (Session["admin"] == null) 
      Response.Redirect("Login.aspx"); 
     else 
     { 
      lblAdmin.Text = "Welcome " + Session["admin"].ToString(); 

      ddlHotel.Items.Clear(); 
      ddlHotel.Items.Add(new ListItem("Select Hotel", "")); 
      ddlHotel.AppendDataBoundItems = true; 
      ddlHotel.DataSource = bll.getHotel(); 
      ddlHotel.DataTextField = "HName"; 
      ddlHotel.DataValueField = "HotelID"; 
      ddlHotel.DataBind();  

      if (Session["hotelID"] == null) 
       ddlHotel.SelectedIndex = 0; 
      else 
       ddlHotel.SelectedValue = Session["hotelID"].ToString(); 

      ddlHotel_SelectedIndexChanged(null,null); 
     } 
    } 
} 

protected void ddlHotel_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    ddlCategory.Items.Clear(); 
    ddlCategory.Items.Add(new ListItem("Select Category", "")); 
    ddlCategory.AppendDataBoundItems = true; 
    ddlCategory.DataSource = bll.getCategoryByID(ddlHotel.SelectedValue); 
    ddlCategory.DataTextField = "RCategoryNameBed"; 
    ddlCategory.DataValueField = "RCategoryID"; 
    ddlCategory.DataBind(); 
} 

如果沒有,我對不起,我無法幫助你。

0

我改變了這一點,但它無法正常工作。

protected void Page_Load(object sender, EventArgs e) 
    { 
     if (Session["admin"] != null) 
     { 
      lblAdmin.Text = "Wel Come " + Session["admin"].ToString(); 
     } 
     else 
     { 
      Response.Redirect("Login.aspx"); 
     } 

     if (!Page.IsPostBack) 
     { 
      if (Session["hotelID"] != null) 
      { 
       ddlHotel.SelectedValue = Session["hotelID"].ToString(); 
      } 
      ddlHotel.DataSource = bll.getHotel(); 
      ddlHotel.DataTextField = "HName"; 
      ddlHotel.DataValueField = "HotelID"; 
      ddlHotel.DataBind(); 
      ddlHotel.Items.Insert(0, "Select Hotel"); 
     } 
    } 
    protected void ddlHotel_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     ddlCategory.DataSource = bll.getCategoryByID(ddlHotel.SelectedValue); 
     ddlCategory.DataTextField = "RCategoryNameBed"; 
     ddlCategory.DataValueField = "RCategoryID"; 
     ddlCategory.DataBind(); 
     ddlCategory.Items.Insert(0, "Select Category"); 
    } 

所以請檢查並回復我。