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

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.

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.

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;

Related

Getting hidden field value from content-page after cross-page post

I have the following situation:
Page1.aspx is based on a master page and has a hidden field called "hdFlotaID" in it. On the same page I have a button for which I set PostBackUrl="Page2.aspx". When I click the buton, the Page2 is loaded but I can't get the hidden field. I tried both (as I saw on msdn or other posts):
this.PreviousPage.Controls[0].FindControl("hdFlotaID")
and
this.PreviousPage.FindControl("hdFlotaID")
but they return null.
This.PreviousPage returns a value, but Controls[0] of that value seems to return the masterpage and I want the content page.
I also checked and the hidden field has runat server value and ClientID mode is set to static (I even checked the generated HTML and the ID is correct)
Can you please help me! Thank you
SOLUTION: Ok,so based on your help I got it to work like this
this.PreviousPage.Controls[0].FindControl("CPH").FindControl("hdFlotaID")
where CPH is the ID of the ContentPlaceHolder from the master page.
Also the ideea with the public property is very good allthough in my case adding the PreviousPageType directive was giving me some errors about namespaces. If I removed the directive and cast it in codebehind it works fine.
Thank you all very much
FindControl searches only one level i.e. top level container and all the controls of your content page are not directly in Master control collection rather inside master's content control collection.
To achieve this
1) you need to write a recursive version of FindControl. try like this (did not tested it):
((Page1)this.PreviousPage).FindControl("hdFlotaID")
or
2) Typecast the previous page to the type of page it is. Then you can access controls.
Set up a property in page1.aspx that returns value of hidden field using this.Page.MasterPage.FindControl("hdFlotaID"). In Page2.aspx, add "PreviousPageType" tag in ASPX file. This way you can access previos page properties in a typesafe way e.g. this.PreviousPage.hdFlotaID_property

ObjectDataSource methods can't refernce master page methods

So I have ObjectDataSource that has an update and delete method associated with it and the DS is conntected to a gridview. When I call the methods, I want to repopulate a dropdown in the Master page that contains the same data as the grid, So i am trying to call the methods as follows:
MyApp MasterPage = (MyApp)Page.Master;
MasterPage.getData();
Now, if I do an insert from the grid (which is handled by the RowCommand event and not the DS) this works just fine, but thru the DS i get the following error: "Object reference not set to an instance of an object."
I can see certain fields on the aspx not being available yet from a postback, but how could a master page not be available to a page that is dependent on it? Shouldn't i be able to reference it? is there some backdoor trick to this? I know I could get rid of the DS, but I want to use it cause it makes paging and sorting so easy.
Thanks
Check the order of events in the masterpage-contentpage life-cycle. You might be able to move your code to an event that fires after the master page has access to the dropdown box.
EDIT: an alternative
If you can't move the code, try to store the data in a property of the master page, then, once the dropdown has been initialised, go back to the master page and get the data.
protected object SomePropertyICreatedToStoreData {get;set;}
void GetData()
{
// get the data (this part works already)
var theData = WhereTheDataComesFrom
// bind to dropdownlist - doesn't work, so...
this.SomePropertyICreatedToStoreData = theData;
}
... then later
this.MyDropDownList.DataSource = this.SomePropertyICreatedToStoreData;
You need to add the Master directive:
<%# MasterType TypeName ="MasterPageClassName" %>
or
<%# MasterType virtualpath="~/Masters/Master1.master" %>
http://msdn.microsoft.com/en-us/library/xxwa0ff0.aspx
Update: Separated TypeName and virtualpath as pointed out in the comments.

Access Masterpage control on a basepage using FindControl

I have an ASP.NET website application on .NET 4.0. There is one Masterpage that contains the header and footer for all the aspx pages. The content comes from the individual aspx pages. I have BasePage.cs which all the aspx pages inherit from.
Now to the problem:
I have a HTML Select control on the masterpage, whose value I am trying to retrieve in the BasePage.cs using the below code
string language = ((System.Web.UI.HtmlControls.HtmlSelect)Master.FindControl("cmbLanguage")).Value;
I am using this inside the InitializeCulture method, which would set the Culture info for the website.
protected override void InitializeCulture()
{
string language = ((System.Web.UI.HtmlControls.HtmlSelect)Master.FindControl("cmbLanguage")).Value;
Thread.CurrentThread.CurrentCulture =
CultureInfo.CreateSpecificCulture(language);
Thread.CurrentThread.CurrentUICulture = new
CultureInfo(language);
base.InitializeCulture();
}
While debugging, I can see that the expected value is set in the language variable. The problem is when the page renders, the content inside the ContentPlaceHolder for the aspx page is not being rendered.
I can see that it is the code involving FindControl which is the cause, because if I set the language to a string, everything works as expected.
string language = "de-DE";
What am I doing wrong?
UPDATE:
If there is some content on the ContentPlaceHolder on the MasterPage, then it gets rendered instead of the page ContentPlaceHolder.
InitializeCulture is called before even PreInit in the page life cycle, which means the controls haven't been setup, and the Value of that control is likely coming through as an empty string.
You need to likely change how the culture is read, through a cookie, session value, or some other method. I'm not familiar with doing it, so i don't have a great suggestion or best practice.
As Doozer correctly noted that control is unlikely to have the correct value set up into it at the time of InitializeCulture. I will suggest that you read the value from POST data in the request and back it via some default value. For example,
string language = Request.Form[Master.FindControl("cmbLanguage")).UniqueID];
language = string.IsNullOrWhiteSpace(language) ? "de-DE" : language;
In order to access MasterPage controls its better to use MasterType directive. When used you are going to be able to access master page in a strongly typed way. In this case you will be able to create a property on master page like this:
public string SelectedCulture
{
get
{
return cmbLanguage.Value
}
}
And on page itself you will be able to run code like this:
protected override void InitializeCulture() {
string language = this.Master.SelectedCulture;
}

ASP.Net - How to access masterpage Object from regular page?

In a project I'm working on, the master page codebehind does a load of complex checks and confirmations that decide the navigation list displayed on the TreeView of the page. Now, I need a way to access this list from another front-end page, such as "frontpage.aspx".
This serves two purposes. One, the masterpage will hide pages on the navigation list the user shouldn't have access to, but the user can still enter the page by typing the page name into the URL manually. By being able to browse through the TreeView, I can isolate the whole authorization into a single method by simply checking if the page name exists within the currently used TreeView.
Two, this will allow me to easily change the displayed content of any page without checking the database or storing sessions for whatever specific rights the current user has, as I can just look if the TreeView contains "Products Admin" for example, and then use that to hide or display the section of the page that has to do with "Product Admin" functionality.
So, any tips on how to do this, or if it's even possible?
Assuming that frontpage.aspx is a content page you can definitely access the master page from it.
For example this code will find a TextBox and Label controls that are on the master page. You should be able to adapt it to find your TreeView:
// Gets a reference to a TextBox control inside a ContentPlaceHolder
ContentPlaceHolder mpContentPlaceHolder;
TextBox mpTextBox;
mpContentPlaceHolder =
(ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1");
if(mpContentPlaceHolder != null)
{
mpTextBox = (TextBox) mpContentPlaceHolder.FindControl("TextBox1");
if(mpTextBox != null)
{
mpTextBox.Text = "TextBox found!";
}
}
// Gets a reference to a Label control that is not in a
// ContentPlaceHolder control
Label mpLabel = (Label) Master.FindControl("masterPageLabel");
if(mpLabel != null)
{
Label1.Text = "Master page label = " + mpLabel.Text;
}
For more info see - http://msdn.microsoft.com/en-us/library/c8y19k6h.aspx
You should be able to access it through the Master property, i.e.:
TreeView tv = Master.MyTreeViewControl;
or
TreeView tv = (TreeView)Master.FindControl("MyTreeViewControl");
This page on MSDN has more information about working with master pages programmatically.
You can access any public functions from the masterpage refering to Page.Master and casting this property to your master-page;
((Styles_Master)Page.Master).IsMyProperty = "new value";

Using javascript on an aspx page that uses a master page, which contains the page in a form?

I have a master page which contains everything that inherits it within a form. A page inheriting from it needs to run some javascript to act on a text field on a page. However, I can't seem to reference that text field through the javascript, since the form begins on the master page.
The following line will come up bogus:
document.form1.txtFindUser.value = blah.responseText;
This is because form1 is defined on the master page, while txtFindUser is on the current page.
How can these situations be handled?
How about that :
document.getElementById('txtFindUser').value = blah.responseText;
But if txtFindUser is your server control's id, then you should use the code above like that :
document.getElementById('<%= txtFindUser.ClientID %>').value = blah.responseText;
Page elements don't always have the same name at runtime than they do in design time in .NET. View the source of your page when you ran it and see what txtFindUser is called after it was interpreted by IIS.
any server controls on the page will render with a different name since that page is derived from a master page. (I guess this removes complexities which can arise out of using same names for controls in your master page as well as in the derived page).
var ctrl = '<%= txtBox1.ClientID %>';
document.getElementById(ctrl).value='Hello world';
Refer to this post for more info

Resources