Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将值从UserControl传递到ParentPage控件时工作不正常_C#_Asp.net_User Controls_Webforms - Fatal编程技术网

C# 将值从UserControl传递到ParentPage控件时工作不正常

C# 将值从UserControl传递到ParentPage控件时工作不正常,c#,asp.net,user-controls,webforms,C#,Asp.net,User Controls,Webforms,我已经创建了一个照片库用户控件,可以在一个奇特的框中显示摄影师 我还在多个页面上使用此控件来显示与页面相关的照片。在隐藏photogallery用户控件中,如果没有相应页面的照片。为此,我通过了公开活动 public event SendMessageToThePageHandler showGallery; 传递布尔值以在页面上隐藏和显示usercontrol 页面运行时没有任何错误,并显示库。但问题是寻呼机的工作方式和我的编码方式不一样。当我单击第2页或图库时,它会生成NullRefer

我已经创建了一个照片库用户控件,可以在一个奇特的框中显示摄影师

我还在多个页面上使用此控件来显示与页面相关的照片。在隐藏photogallery用户控件中,如果没有相应页面的照片。为此,我通过了公开活动

 public event SendMessageToThePageHandler showGallery;
传递布尔值以在页面上隐藏和显示usercontrol

页面运行时没有任何错误,并显示库。但问题是寻呼机的工作方式和我的编码方式不一样。当我单击第2页或图库时,它会生成
NullReferenceException

我想推荐一种更专业的方法来解决这个问题,在这种情况下,我可以将passvalue正确地传递给父页面,以便正确地隐藏/显示

我还在usercontrol中使用UpdatePanel。下面是来自父页面和用户控制页面的示例代码

PageOne.aspx

 <uc1:PhotoGallery ID="ucPhotoGallery" runat="server" />
<%@ 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>
照片库.ascx

 <uc1:PhotoGallery ID="ucPhotoGallery" runat="server" />
<%@ 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>
解决问题

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

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

//我已经把所有的用户控制代码都放在这一块了
如果(ds.Tables[0].Rows.Count>0)
{
//展览廊(真实);
pnl_uc_Gallery.Visible=true;
}
其他的
{
//展览廊(假);;
pnl_uc_Gallery.Visible=false;
}
此代码:

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

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

您可以发布“showGallery”函数吗?它不是一个函数,而是一个委托。我在一些文章中看到了这种传递值的方式。'public delegate void sendmagestothepagehandler(bool showGallery);'你说得对,让我试试看能不能把它修好。。将代码移到post back块之外没有任何区别。。如果我做得对,请告诉我c。它仍然会给我与问题部分截图中显示的错误相同的错误
System.NullReferenceException未由用户代码处理
尝试将页面加载代码移动到页面初始化;可能在应用事件处理程序之前,首先触发UC代码。