System.NullReferenceException - 调用辅助页面

发布于 2024-10-30 16:54:01 字数 1567 浏览 2 评论 0原文

我的组件(ascx):

这个组件是我页面的一部分,通过他,我打开一个新页面(下一个代码)。

    public void LoadSeller(string name, string id)
    {
        this.txtSeller.Text = name;
        this.hdnSellerID.Value = id;
    }

我的弹出窗口 (aspx):

使用此代码,我进行了一些搜索并在 GridView 上选择一项,将其发送回我的 ascx。

    protected void btnSave_Click(object sender, EventArgs e)
    {
        Components.BasicData basicData= new Components.BasicData();

        Button save = (Button)sender;
        basicData.LoadSeller("TEST", save.CommandArgument);
    }

回发有效,我收到了正确的数据(我检查了调试)。但 txt 和隐藏都是空的(就像它们没有加载一样),它们在没有我的弹出窗口的情况下工作,但我需要我的弹出窗口。

有什么想法吗?

PS:我的viewState是UP。

我调试了整个代码,它停在

this.txtSeller.Text = 名称;

LoadSeller 的第一行。这说“this.txtSeller”为空,我不知道为什么。我检查了我的 .designer.cs 并声明了 this.txtSeller 。 (如果我不尝试从其他页面加载,它也可以工作)

Obs:我想知道我的代码是否以某种方式破坏了我其他页面上的负载(或类似的东西)。

感谢您的帮助^.^

编辑:

我的结构是这样的:

页面

Control1

Control2

    **Open pop up**

Control3

POP UP

Search

Gridview with buttons

的按钮

Goes to my second code "my pop up (aspx):"

Close pop up

Gridview中 Control2 我有我的第一个代码“我的组件(ascx):”

副标题:Control == userControl

面包屑导航:

  • 解决方案
    • 组件(文件夹)
      • BasicData.ascx
    • PopUp.aspx

my component (ascx):

This component is part of my page, with him, i open one new page (the next code).

    public void LoadSeller(string name, string id)
    {
        this.txtSeller.Text = name;
        this.hdnSellerID.Value = id;
    }

my pop up (aspx):

With this code i made some search and select one item on GridView, sending this back to my ascx.

    protected void btnSave_Click(object sender, EventArgs e)
    {
        Components.BasicData basicData= new Components.BasicData();

        Button save = (Button)sender;
        basicData.LoadSeller("TEST", save.CommandArgument);
    }

The post back works, i'm receiving the right data (i checked with my debug). but the txt and hidden are both null (like they are not loaded), they are working without my pop up, but i need mine pop up.

Any ideas ?

PS: My viewState is UP.

I debug my entire code and it stopped at

this.txtSeller.Text = name;

First line of LoadSeller. This said that "this.txtSeller" is null, and i don't know why. i checked my .designer.cs and this.txtSeller is declared. (it also work if i'm not trying load from other page)

Obs: I was wondering if maybe my code is somehow destroying the load (or something like this) on my other page.

Thanks for your help ^.^

EDIT:

My structure is this:

PAGE

Control1

Control2

    **Open pop up**

Control3

POP UP

Search

Gridview with buttons

Button in gridview

Goes to my second code "my pop up (aspx):"

Close pop up

in my Control2 i have my first code "my component (ascx):"

Subtitle: Control == userControl

Breadcrumb navigation:

  • Solution
    • Components(folder)
      • BasicData.ascx
    • PopUp.aspx

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

段念尘 2024-11-06 16:54:01

这行 Components.BasicData basicData= new Components.BasicData(); 不是创建自定义控件及其所有内部控件,也不是指向现有控件的指针。

如果您已经准备好将您的控件包含在此页面上,那么您可以通过控件的 id 直接调用此函数。这个控件已经和你的页面一起存在了,只要调用它即可。

protected void btnSave_Click(object sender, EventArgs e)
    {
        Button save = (Button)sender;
        YourCustomControlID.LoadSeller("TEST", save.CommandArgument);
    }

现在,如果您希望将数据发送到下一页,您首先创建一个类来保存您的信息,并将其从一个页面移动到另一个页面并填充数据。

在每个页面中,您都有对该类的引用,一个变量,该变量创建一个新的以太,从上一页获取它。您将其存储在 ViewState

Now 中,从 Page1 -> Page2。

您可以通过设置将其从Page1发送到

PostBackUrl="Page2.aspx"

您设置的可以获取信息的On page2.aspx

<%@ PreviousPageType VirtualPath="~/Page1.aspx" %>

,然后通过...获取它们...

if (Page.PreviousPage != null)
{
    if(Page.PreviousPage.IsCrossPagePostBack == true)
    {
        GetTheClass = PreviousPage.MyDataClass;
    }
}

另一种手动方法是使您的类可串行化,将其作为xml发送通过帖子,并在下一页对其进行解码。

This line Components.BasicData basicData= new Components.BasicData(); is NOT create the custom control and all the inside controls of him, nether is a pointer to the existing ones.

If you all ready include your control on this page, then you can direct call this function it by its id of the control. This control all ready exist together with your page, just call it.

protected void btnSave_Click(object sender, EventArgs e)
    {
        Button save = (Button)sender;
        YourCustomControlID.LoadSeller("TEST", save.CommandArgument);
    }

Now if you wish to send data to the next page you first create a class that keep your information and its going to move from page to page and fill with data.

In every page you have a reference to this class, a variable, that ether create a new ether get it from the previous page. You store it on ViewState

Now from Page1 -> Page2.

You send it from Page1 by set to the

PostBackUrl="Page2.aspx"

On page2.aspx you set where you can get informations

<%@ PreviousPageType VirtualPath="~/Page1.aspx" %>

and you get them by...

if (Page.PreviousPage != null)
{
    if(Page.PreviousPage.IsCrossPagePostBack == true)
    {
        GetTheClass = PreviousPage.MyDataClass;
    }
}

And one other manual way is to make your class seriable, send it as xml via post, and decode it on the next page.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文