Can not get Request.Form.Get valus from aspx page that wrappe withe ContentPlaceHolder (form error)

3k views Asked by At

I am writing a web app in asp.net I have a master page that contain a ContentPlaceHolder and a form that wrapper the ContentPlaceHolder, In a aspx page I realize the ContentPlaceHolder and have some controls in this page.

Now when I Trying to use Request.Form.Get("my control name") (from the aspx behind code), I get null. If I try to add a form in the aspx page I get an error that say you can have only one form in a page.

How can i get the values in my controls??

thanks for the help.

2

There are 2 answers

0
patmortech On

Request.Form("YourControlName") will not work with server controls because ASP.NET adds some extra stuff to the name of your control when it outputs it to the page. It does this to make sure that the name remains unique across all the controls on the page. So, for example, your control might actually be named something like "ctl00_maincontent_placeholder1_YourControlName" when it gets created on the page.

In ASP.NET this is not usually a problem because you typically do NOT use Request.Forms to get your control values. Instead you use the server control's methods to get the values. So, for a textbox, you would use YourControlName.Text to get the value that was entered into the textbox.

0
Michael Christensen On

If you are just trying to communicate a value between the master and page, assuming the value is on the master you can cast Page.Master to the correct type. On the master page you can wrap controls on the master.

MasterPage

public string MyControlText
{
    get
    {
        return myControl.Text;
    }
    set
    {
        myControl.Text = value;
    }
}

On the page

((MyMasterPage)this.Page.Master).MyControlText = "To master from page";

string fromMasterToPage = ((MyMasterPage)this.Page.Master).MyControlText;