简体   繁体   中英

Change the Access Modifiers of ASP.NET controls

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?

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.

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.

如果您需要操作这些控件,那么暴露该页面的控件是有意义的,但在您的情况下,您只需要将一些数据(该字符串)传递给另一个处理程序,因此我会公开它而不是控件本身。

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