更改 ASP.NET 控件的访问修饰符
如果我像这样将控件放入 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以从设计器中删除声明并将其放在后面的代码中。
围绕该声明的评论说要这样做。
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.
我考虑过的一个选择是编写一个公开原始页面的公共属性;
这可以完成工作,但看起来很老套。
One option I've considered is writing a public property which exposes the original page;
Which would get the job done, but seems hacky.
史蒂夫,如果您需要操作这些控件,那么公开该页面的控件是有意义的,但在您的情况下,您只需将一些数据(该字符串)传递给其他处理程序,所以我将公开它而不是控件本身。
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.