将值从用户控件传递到父页面控件无法正常工作

时间:2013-11-18 11:48:24

标签: c# asp.net user-controls webforms

我创建了一个照片库用户控件,可以在一个精美的盒子里展示摄影师。

我还在几个页面上使用此控件来显示与页面相关的照片。在hide photogallery用户控件中,如果他们没有针对特定页面的照片。为此我通过公共活动

 public event SendMessageToThePageHandler showGallery;

传递一个布尔值来隐藏和显示页面上的usercontrol。

页面运行没有任何错误并显示库。但问题在于寻呼机不能像我编码那样工作。当我点击第2页或图库

时,它会生成NullReferenceException

我会对这种情况采用更专业的方法,我可以正确地将passvalue传递给父页面以正确隐藏/显示。

我也在我的usercontrol中使用UpdatePanel。以下是Parent Page&amp ;;的示例代码。用户控制页面

enter image description here

PageOne.aspx

 <uc1:PhotoGallery ID="ucPhotoGallery" runat="server" />

代码背后

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        try
        {
            int _LangID = int.Parse(Helper.GetAppSettingValue("EnglishLanguageID"));
            if (string.IsNullOrEmpty(Request["PID"]))
            {

                _LID = int.Parse(HttpContext.Current.Request.RequestContext.RouteData.Values["LID"].ToString());
                _PID = int.Parse(HttpContext.Current.Request.RequestContext.RouteData.Values["PID"].ToString());
                getPageDetails(_PID);
                getPageSubMenus(_PID);
              //  WriteThisMessageToThePage1.sendMessageToThePage += delegate(string message)
                ucPhotoGallery.showGallery += delegate(bool showControl)
                {
                    bool bShowControl = showControl;
                    ucPhotoGallery.Visible = bShowControl;
                };
            }
            else
            {
                Response.Write("Page not be found, Wrong URL");
            }

        }
        catch (Exception ex)
        {
        }
    }

}

照片-Gallery.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Photo-Gallery.ascx.cs" Inherits="Photo_Gallery" %>
<%@ Register Src="~/en/UserControls/PagerControl.ascx" TagName="PagerControl" TagPrefix="uc1" %>
<div class="uc-gallery-wrapper">
<div class="page-title-side-wrapper"><h5><asp:Label ID="lblPageTitleSide" CssClass="lbl-page-title-side" runat="server" Text=" Gallery"></asp:Label></h5></div>
<asp:UpdatePanel ID="updPnlAlbums"  runat="server" UpdateMode="Conditional">
   <ContentTemplate>
   <div class="rptAlbums-wrapper">
        <asp:Repeater ID="rptAlbums" runat="server" >
            <ItemTemplate>
                <div class="uc-album-wrapper">
                    <div class="uc-album-icon"><asp:Image ID="imgAlbumIcon" CssClass="uc-album-img" ImageUrl='<%# getImage(Eval("AlbumIcon")) %>' AlternateText='<%# getTitle(Eval("AlbumName")) %>'  runat="server" /></div>
                    <div class="uc-album-name">
                        <asp:HyperLink ID="hylnkAlbum" CssClass="fancybox-iframe"  runat="server" NavigateUrl='<%# getAID(Eval("AlbumID")) %>'>
                        <asp:Label ID="lblAlbumName"  runat="server" Text='<%# getTitle(Eval("AlbumName")) %>'></asp:Label>

                        </asp:HyperLink>
                    </div>
                </div>
            </ItemTemplate>
        </asp:Repeater>
    </div>
    <div class="uc-gallery-pager-wrapper-o">
        <div class="uc-pager-wrapper-i">
        <uc1:PagerControl ID="PagerControl1" runat="server"  CssClass="gold-pager"  PageMode="LinkButton"  />
        <asp:UpdateProgress ID="UpdateProgress1" runat="server" AssociatedUpdatePanelID="updPnlAlbums" Visible="False">
            <ProgressTemplate>
                <div id="imgLoadingNewsList" class="uc-PagerLoading">
                    <asp:Image ID="imgLoading" runat="server" ImageUrl="~/Images/preloader-160x15.gif" Visible="false" AlternateText="loading"  />
                </div>
            </ProgressTemplate>
        </asp:UpdateProgress>
        </div>
       </div>
    </ContentTemplate>
</asp:UpdatePanel>
<!-- UpdatePanel -->

</div>

代码背后

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;
using System.Globalization;
using System.Data;
using CMS.SqlHelper;
using CMS.DataAccessLayer;
using System.Text;

public delegate void SendMessageToThePageHandler(bool showGallery);

public partial class Photo_Gallery : System.Web.UI.UserControl
{
    int _PageID = 0;

    public event SendMessageToThePageHandler showGallery;

    protected void Page_Load(object sender, EventArgs e)
    {
        PagerControl1.PagerControl_PageIndexChanged += new EventHandler(PagerControl1_PagerControl_PageIndexChanged);
        Page.MaintainScrollPositionOnPostBack = false;

        if (!IsPostBack)
        {
            if (string.IsNullOrEmpty(Request["PID"]))
            {

                _PageID = int.Parse(HttpContext.Current.Request.RequestContext.RouteData.Values["PID"].ToString());
                //_PageID = 3;

                getAlbumByPageID(3);

            }
            else
            {
                Response.Write("Page not be found, Wrong URL");
            }
        }
    }

    public void getAlbumByPageID(int _PageID)
    {
        PagerControl1.PageSize = 2;
        PagerControl1.DisplayEntriesCount = 5;
        //Will show 2 links after ...
        PagerControl1.EdgeEntriesCount = 0;
        DataSet ds = DataProvider.GetAlbumByPageID(_PageID);
        DataView dv = ds.Tables[0].DefaultView;

        if (ds.Tables[0].Rows.Count > 0)
        {
            showGallery(true);
        }
        else
        {
            showGallery(false);
        }

        //pass the datatable and control to bind
        PagerControl1.BindDataWithPaging(rptAlbums, dv.Table);
    }

    void PagerControl1_PagerControl_PageIndexChanged(object sender, EventArgs e)
    {
        _PageID = int.Parse(HttpContext.Current.Request.RequestContext.RouteData.Values["PID"].ToString());
        getAlbumByPageID(_PageID);
    }

}

周围工作

我找到了一个解决方法,将我的用户控件包装在一个面板中,如果我有该页面的照片,则隐藏/显示面板。这工作正常,但我仍然想解决我有的问题,甚至更好的方法。

<asp:Panel ID="pnl_uc_Gallery" runat="server" Visible="false">
// I have PUT all the USER CONTROL CODE In SIDE THIS BLOAK
 </asp:Panel>  


if (ds.Tables[0].Rows.Count > 0)
{
   // showGallery(true);
    pnl_uc_Gallery.Visible = true;
}
else
{
    //showGallery(false);
    pnl_uc_Gallery.Visible = false;
}

1 个答案:

答案 0 :(得分:1)

此代码:

 ucPhotoGallery.showGallery += delegate(bool showControl)
                {
                    bool bShowControl = showControl;
                    ucPhotoGallery.Visible = bShowControl;
                };

正在进行!Page.IsPostback通话;将它移到外面,以便在每个回发时附加到事件,而不仅仅是第一个,或者使用方法作为事件处理程序并在标记中设置方法名称。无论哪种方式,问题是事件处理程序仅在第一页加载时建立,而不是后续加载,并且每次请求到达服务器时都需要完成。