简体   繁体   中英

Passing Value from UserControl to ParentPage control not working properly

I have create a photo gallery user control that show photographer in a fancy box.

I also use this control on several page to show page related photos. In hide photogallery user control if their are no photos for perticular page. For this i pass public event

 public event SendMessageToThePageHandler showGallery;

to pass a boolean value to hide and show usercontrol on the page.

Page runs without any errors and show the gallery. But problem is with the Pager which doesn't work the way i am coded it. It generate NullReferenceException when i click on the page 2 or the gallery

I would appreeciate a more professional approach to this scenario in which i can pass passvalue to parent page correctly to hide/show correctly.

I am also using UpdatePanel in my usercontrol. Below is the sample code from Parent Page & User Control page

在此处输入图片说明

PageOne.aspx

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

CODE BEHIND

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)
        {
        }
    }

}

Photo-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>

CODE BEHIND

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);
    }

}

WORK AROUND

I found a work around by wrapping my users control in a panel and hide/show panel if i have photos for that page or not. This is working fine but i still want to fix problem i have or even a better way of doing it.

<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;
}

This code:

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

is in a !Page.IsPostback call; move it outside so that you are attaching to the event on every postback, not just the first one, or use a method as the event handler and set the method name in the markup. Either way, the issue is the event handler is established only on the first page load, not subsequent loads, and it needs to be done every time a request hits the server.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM