0

I am using this tutorial for getting the content of Masterpage to content page. https://stackoverflow.com/a/13620436/4725592

But when I try to get the content of the master page with this syntax, I gets the following error

The type or namespace name could not be found

The syntax :

string getProcess = ((MyMasterPage)this.Master).ProcessDropDownListValue;
Community
  • 1
  • 1
Harshit
  • 5,147
  • 9
  • 46
  • 93
  • I don't see where you use `name`. Apart from that, don't use this approach to get the references of your controls in the master form the content-page. Instead use public properties which return the `DropDownList`, or even better, which only return what is needed, f.e. `int SelectedProcess`. Normally the content should not have direct access to the master's controls. That's hardwiring both which is against the purpose of a masterpage. You also cannot change the type of the control easily without breaking all of your pages. – Tim Schmelter May 08 '15 at 11:06

1 Answers1

0

What I have understand that you want to access masterpage control in the content page. The simple way to do this is, e.g you have a label control on master page to acess it on content page use:

 Label MasterPageLabel = this.Master.FindControl("lbl") as Label;
    Response.Write(MasterPageLabel.Text);
killer
  • 592
  • 1
  • 9
  • 31
  • But never use this code in production code. You are hard-wiring the master with it's content. You cannot change ID's later without breaking code at runtime. You cannotchange the type of the controls later. You fail anyway if the control is in a child control like `Repeater`. The right way is to provide a property with a meaningful name in the master that returns the relevant information and no more. – Tim Schmelter May 08 '15 at 12:23
  • @Tim why you down rated this answer. If it is helpful for the questioner what is the problem? Also questioner didn't ask you for teaching him efficient way of doing this, it was a syntax issue. If this method is not useful at all in every situation why Microsoft has made it available in c# asp.net . – killer May 08 '15 at 12:30
  • It's helpful if you have to get the reference to a control in a very specific situation. For example if you are in a handler of a button-click event and that button sits in a `RepeaterItem` or `GridViewRow`. Here you can get any other control in the same item/row via (pseudo-code): `sender.NamingContainer.FindControl("OtherControlID")`. Otherwise you don't have a chance to get that control. The "global" `FindControl` is just a bad approach which is used far too often in production code . It is a ticking time bomb. – Tim Schmelter May 08 '15 at 12:35
  • For a new learner ,these should be tried in order to get better understanding of the structure even though it may not be recommended...but could not understand why to down rate.... – killer May 11 '15 at 05:43
  • Beginners should start with the right approach otherwise they use the wrong forever or until the boss says that they should. I see this wrong far too often, it gets upvotes everytime. I have set it back to 0 to prevent that many starters think that its the best approach. – Tim Schmelter May 11 '15 at 07:03
  • Everyone starts like this....but if you did not like the question you should have down rated question not the answer..May be you wanted to answer this question so you down rated it. – killer May 11 '15 at 10:17