更改 ASP.NET 控件的访问修饰符

发布于 2024-07-27 11:55:44 字数 995 浏览 11 评论 0原文

如果我像这样将控件放入 .aspx 文件中;

<asp:TextBox ID="protectedTextBox" runat="server">Some info</asp:TextBox>

我在页面的 .aspx.designer.cs 文件中获得了一个已声明的控件;

protected global::System.Web.UI.WebControls.TextBox protectedTextBox;

但我想将控件的访问修饰符更改为public。 是否有任何属性或类似的属性可以设置来更改访问修饰符?

这就是我想要这样做的原因。 我试图让跨页面回发工作得很好而且很整齐。 我有两个页面:

FirstPage.aspx
    MyTextBox : textbox
    MyButton  : button, @PostbackUrl=Secondpage

SecondPage.aspx
    MyLabel : label

当用户单击 FirstPage.MyButton 时,我想将 FirstPage.MyTextBox.Text 的值写入 SecondPage.MyLabel.Text 中。 我可以使用 Page.FindControl 来完成此操作,但这似乎是一个糟糕的替代方案,无法将上一页转换为 FirstPage 对象并直接引用其上的 MyTextBox 控件。 像这样的东西;

// on the page_load of SecondPage.aspx;
var previousPage = this.PreviousPage as FirstPage;
this.MyLabel.Text = previousPage.MyTextBox.Text;

有什么办法可以改变访问修饰符吗?

If I put a control in a .aspx file like this;

<asp:TextBox ID="protectedTextBox" runat="server">Some info</asp:TextBox>

I get a declared control in the page's .aspx.designer.cs file;

protected global::System.Web.UI.WebControls.TextBox protectedTextBox;

But I'd like to change the access modifier of the control to public. Is there any attribute or similar that I can set to change the access modifier?

Here's why I want to do it. I am trying to have cross-page postbacks work nice and neatly. I have two pages:

FirstPage.aspx
    MyTextBox : textbox
    MyButton  : button, @PostbackUrl=Secondpage

SecondPage.aspx
    MyLabel : label

When the user clicks FirstPage.MyButton, I want to write the value of FirstPage.MyTextBox.Text into SecondPage.MyLabel.Text. I could do it with Page.FindControl, but this seems like a poor substitute to casting the previous page as a FirstPage object and referring directly to the MyTextBox control on it. Something like this;

// on the page_load of SecondPage.aspx;
var previousPage = this.PreviousPage as FirstPage;
this.MyLabel.Text = previousPage.MyTextBox.Text;

Is there any way to change the access modifier?

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

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

发布评论

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

评论(3

小嗲 2024-08-03 11:55:44

您可以从设计器中删除声明并将其放在后面的代码中。

围绕该声明的评论说要这样做。

/// To modify move field declaration from designer file to code-behind file.

You can just delete the declaration from the designer and put it in your code behind.

The comments around the declaration say to do this.

/// To modify move field declaration from designer file to code-behind file.
浅语花开 2024-08-03 11:55:44

我考虑过的一个选择是编写一个公开原始页面的公共属性;

public TextBox PublicTextBox { get { return this.MyTextBox; } }

这可以完成工作,但看起来很老套。

One option I've considered is writing a public property which exposes the original page;

public TextBox PublicTextBox { get { return this.MyTextBox; } }

Which would get the job done, but seems hacky.

七度光 2024-08-03 11:55:44

史蒂夫,如果您需要操作这些控件,那么公开该页面的控件是有意义的,但在您的情况下,您只需将一些数据(该字符串)传递给其他处理程序,所以我将公开它而不是控件本身。

Steve, exposing that page's controls would make sense if you'd need to manipulate those controls, but in your case you just need to pass some data (that string) to the other handler, so I would expose that and not the control itself.

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