Change the Access Modifiers of ASP.NET controls

放肆的年华 提交于 2021-02-18 23:47:44

问题


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?


回答1:


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.



回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/1165321/change-the-access-modifiers-of-asp-net-controls

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!