11626

ASP.NET expose controls to other class

I have been battling this for some time and I need some guidance.

I'm coding in ASP.NET 4.0 WEBFORMS.

<strong>Question is: How to expose a textbox, Label or any other control to another class.</strong>

I have a webform (see below).

public partial class WebForm1 : System.Web.UI.Page { }

<strong>This is then referenced and sent to another class.</strong>

public class SearchInitializer { private WebForm1 _webform1; public SearchInitializer(WebForm1 Webform1) { _webform1 = Webform1; } public void ChewSettings() { _webform1 //can't find any control in here?! } }

First I thought of <strong>creating a public property</strong> which I thought I could access from the reference I sent to the new class.. But nooo!

public partial class WebForm1 : System.Web.UI.Page { public string KeywordBox1 { get {return txt_keyword.Text;} set {txt_keyword.Text = value;} } }

The I tried to <strong>inherit the Webform into the other class</strong>. Making the the property available but no luck there.

public class SearchInitializer : Webform1 { private WebForm1 _webform1; public SearchInitializer(WebForm1 Webform1) { _webform1 = Webform1; } public void ChewSettings() { _webform1 //can't find any control in here?! } }

Okay an abstract class migth be of use here, inheriting everything. But I think I got that wrong to. I have events and static classes, so they can talk with the page. But I really would like not to use a static class as a container to save all the info in my controls. So these are the examples I have tried and they all failed. So this is me basicly trying to expand what I know ;) Thanks for reading!! Why have they failed and how should I do it?

EDIT AS REQUESTED!

public partial class WebForm1 : System.Web.UI.Page { protected void btn_click(object sender, EventArgs e) { SearchInitializer searchIni = new SearchInitializer(this); } }

Answer1:

To expose the controls there are two methods I can think of that you can employ.

You can remove the following statement from the myPage.designer.cs file and place it in your code behind as a public declaration:

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

becomes

public System.Web.UI.WebControls.TextBox myTextBox;

This should make it immediately accessible. My preferred method is to add a property for each specific control that you want to provide access to.

public System.Web.UI.WebControls.TextBox MyTextBoxElement { get { return myTextBox; } }

This allows to provide supplementary access controls if you need them or other conditionals. In any case, to access either the field or the property, the consuming object must reference this by your specific page type.

Answer2:

Not sure what you are trying to do, but to access a base class within an inherited calss you need to use the base keyword, not declare an instance there of.

public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } public string KeywordBox1 { get { return txt_keyword.Text; } set { txt_keyword.Text = value; } } } public class SearchInitializer : WebForm1 { public SearchInitializer() { } public void ChewSettings() { // Works base.KeywordBox1 = "Red"; } }

If intellisense is not showing the property, try rebuilding the solution. It will then refresh the list of available properties and it should show.

Answer3:

Your original approach must work. I suggest you create a small test project with a form with text box and SearchInitializer class and see that it works, after that figure out what is different in your current project.

Recommend

  • How to Find UserControls Control value in Jquery Asp.net?
  •  characters has occurred an error when i use the HttpUnit
  • Getting data out of a Silverlight control
  • C# adding click events to a list box
  • Does it make a difference where to put [super viewDidLoad] in my UIViewController? [duplicate]
  • Extending the attributes inspector in interface builder
  • Dropdown menu with the dropdown-menu-right class does not align to the right
  • Looking for datastructure that maintains a size & purges older elements in the process
  • View/Download Pdf Files in React - Router 4
  • Ant: fileset “dir” attribute with a runtime expanded full path
  • Fixed Background Works in Chrome but Not Firefox?
  • What is the reason that Policy.getPolicy() is considered as it will retain a static reference to the
  • Magento site down due to mysql error General error: 1030 Got error -1 from storage engine
  • uml Composition relationships to RDF and OWL
  • Azure table store snapshot/backup capability
  • WPF ICommand CanExecute(): RaiseCanExecuteChanged() or automatic handling via DispatchTimer?
  • xtable package: Skipping some rows in the output
  • Display images in Django
  • How to test if a URL from an Eclipse bundle is a directory?
  • ActiveRecord query for a count of new users by day
  • Word Open XML Mail Merge
  • Set the selected item in dropdownlist in MVC3
  • How to create a file in java without a extension
  • Spring Data JPA custom method causing PropertyReferenceException
  • Change JButton Shape while respecting Look And Feel
  • Projection media query: browser support and workarounds?
  • Splitting given String into two variables - php
  • What is Eclipse's Declaration View used for?
  • Counter field in MS Access, how to generate?
  • Possible to stop flickering java tooltip in heavyweight mode?
  • sending/ receiving email in Java
  • How to set my toolbar fixed while scrolling android
  • AT Commands to Send SMS not working in Windows 8.1
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • How to set the response of a form post action to a iframe source?
  • Change div Background jquery
  • Qt: Run a script BEFORE make
  • apache spark aggregate function using min value
  • Sorting a 2D array using the second column C++
  • reshape alternating columns in less time and using less memory