使用updatepanel的DropDownList中的问题

时间:2013-10-25 07:18:37

标签: c# asp.net drop-down-menu updatepanel

当我从第一个下拉列表中选择值然后在第二个下拉列表中显示相关的数据时,我有两个带有更新面板的DropDownList。但是在运行时存在问题,它无法显示下拉列表并不断刷新此页面。

所以请帮助解决这个问题。

这是我的代码ASPX PAGE

<%@ 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");
    }
}

所以请尽快回复

由于

4 个答案:

答案 0 :(得分:1)

不是你调用页面加载函数,这是ASP.NET的工作方式。当页面上的任何服务器控件被触发时(页面设置为回发),页面会发布到自身,从而调用page_load函数。

您需要做的是进行一些检查以区分初始页面加载和回发

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

答案 1 :(得分:0)

UpdatePanel控件旨在仅刷新其内容。

出于过滤目的,您可以选择以下方法之一:

  • 所有相关的DropDownLists必须放在一个UpdatePanel中。

  • 第一个UpdatePanel及其内容必须嵌套在第二个UpdatePanel中。(不推荐)

无需在两种方法中声明AsyncPostBackTrigger。

祝你好运。

答案 2 :(得分: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();
}

如果没有,我很抱歉,我帮不了你。

答案 3 :(得分: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");
    }

所以请检查并回复我。