0

I have a MasterPage that contains a hidden field control. I want to get the current value of the hidden field and set the value of it from pages that use the MasterPage.

I have the following code so far: (in one of the pages)

   //Get the textbox and set it's value
   TextBox txt1 = new TextBox();
                txt1 = (TextBox)this.Master.FindControl("txtHiddenField");
                txt1 .Text = "true";

The above code does not seem to work. What code would I need to get the hidden field control and set it's value? (and get it's value)

Theomax
  • 6,584
  • 15
  • 52
  • 72
  • is txtHiddenField a HiddenField? then use HiddenField type. You don't need to use a constructor at all, why should I create an instance to get a reference to another instance? – Adrian Iftode Apr 27 '12 at 09:50
  • I guess it should be as ((TextBox)this.Master.FindControl("txtHiddenField")).Text = "True"; – Rajesh Apr 27 '12 at 10:04

4 Answers4

5

I would recommend to provide a public property/method in your MasterPage, that you can use to set/get the HiddenField's value.

in your Master(assuming it's type is called SiteMaster):

public String HiddenValue { 
    get{return txtHiddenField.Value;}
    set{txtHiddenField.Value = value;}
}

In your page:

SiteMaster master = (SiteMaster)Page.Master;
master.HiddenValue = "true";

This approach is straight-forward, less prone to errors and easily readable. You could even change the control in your master without needing to change the pages(f.e. if you want to replace the hidden-field with a TextBox).

Assuming that your "true" value indicates that you actually want to store a boolean, i would recommend to use a bool as data-type for the property and a self-explanatory name. Then you can store it in the hiddenfield but the client(the page) does not need to know.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
2

HiddenField sets its text as VALUE, while TextBox has a TEXT property. Of course casting one to the other and setting text property won't help.

Do this instead:

HiddenField hiddenField = (HiddenField)Master.FindControl("txtHiddenField");
hiddenField.Value = "true";
walther
  • 13,466
  • 5
  • 41
  • 67
1

Assuming you have added hidden field control like this ->>

<input type="hidden" ID="hiddenFieldID" runat="server" />

you can access it like -->>

HtmlInputHidden hiddenfield = (HtmlInputHidden)this.Master.FindControl(
rt2800
  • 3,045
  • 2
  • 19
  • 26
1

May be you are missing ContentPlaceHolder

Try something like this

  ContentPlaceHolder mpContentPlaceHolder;
    TextBox mpTextBox;
    mpContentPlaceHolder = 
      (ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1");
    if(mpContentPlaceHolder != null)
    {
        mpTextBox = 
            (TextBox) mpContentPlaceHolder.FindControl("TextBox1");
        if(mpTextBox != null)
        {
            mpTextBox.Text = "TextBox found!";
        }
    }

Read more about Reference ASP.NET Master Page Content

huMpty duMpty
  • 14,346
  • 14
  • 60
  • 99