Referencing Parent class from custom control

Results 1 to 2 of 2

Thread: Referencing Parent class from custom control

  1. #1
    Join Date
    Dec 1969

    Default Referencing Parent class from custom control

    Hi - I&#039;ve been stuck on this for the last few hours even though it *has* to be something ridiculously simple that I&#039;m missing<BR><BR>I have a Asp.Net C# page ("AddEdit.aspx") that contains a table with approx 50 rows, each of which contains a textbox and a button. Each button calls the same method in the code-behind ("GetAllTextboxValues") that goes through every textbox on the page, and puts the values from all the textboxes into a string.<BR><BR>Earlier this afternoon, whilst building this page, I decided that rather than having 50 buttons, each with the "OnCommand" <BR>property set to the name of the method, it would be much easier to create one custom button, that has the OnCommand hardcoded <BR>to be the method I need (from AddEdit.aspx) that way obviously, if the method changes I only have to change it once etc<BR><BR>I created the button as a web control project within the same solution as the "AddEdit" project, and added a reference to the "AddEdit" project at the top of the custom button class, so I could reference the method I needed ("GetAllTextboxValues").<BR><BR>This works fine, the AddEdit page contains 50 instances of the custom button, and the method from AddEdit is called when the button is clicked. <BR><BR>The problem is that the method is supposed to go through all the textboxes on the page, and check for a value, so I was previously<BR>calling the method as follows:<BR><BR>public void btnClicked(Object sender, CommandEventArgs e)<BR>{<BR>string TextBoxId = "txt" + e.CommandArgument.ToString();<BR>GetAllTextboxValu es(this, TextBoxId, Convert.ToInt32(e.CommandArgument.ToString()));<BR >}<BR><BR><BR>public void GetAllTextboxValues(Control parent, string TextBoxId, int RowId)<BR>{ <BR>foreach (Control c in parent.Controls)<BR>{<BR>GetValue...etc<BR>}<BR>} <BR><BR>using the "this" keyword to reference the page itself. <BR><BR>The problem is that now the "btnClicked" method is being called by the custom button control within the page, "this" seems to be referencing the custom button control, not the parent page that contains it, so the "GetAllTextboxValues" method gets no results. There has to be an easy way round this?<BR><BR>GetAllTextboxValues(this, TextBoxId, Convert.ToInt32(e.CommandArgument.ToString())); &#060;&#060;-- "this" has to reference the parent page, not the custom control calling it<BR><BR>hope this makes sense and isn&#039;t too long winded! thanks for any help :)<BR><BR>james

  2. #2
    Join Date
    Dec 1969

    Default RE: Referencing Parent class from custom control

    I may be a bit confused about where your events are, but I think all you need to do is replace &#039;this&#039; with &#039;Page&#039;.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts