Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 582
Steve Cooper Avatar asked Jul 22 '09 13:07

Steve Cooper


2 Answers

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.
like image 116
Robin Day Avatar answered Sep 20 '22 16:09

Robin Day


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.

like image 24
Steve Cooper Avatar answered Sep 19 '22 16:09

Steve Cooper