Accessing Content Page id with dynamic generated id - c#

I have an asp.net content page as below and I currently access the controls using FindControl. Is there a way i can access these controls using ctl00_ContentPlaceHolder1_id. Aka using the id which is dynamically generated on the webpage rather than using find control. My concern being is that id will be constant or it keeps changing for each user/ based on server or any other param etc.,
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:Button runat="server" ID="id" OnClick="id_Click" Text="text"/>
</asp:Content>

To access a control on server side you can use ID if the control is server control, in other words if it has part runat="server". Suppose that ID of the Button in your example is MyButton, this would be the way to access it in code behind and, for instance, set Enabled property:
MyButton.Enabled = true;

Related

Getting A page can have only one server-side Form tag Error

I have done my MasterPage and create a new .aspx page with this MasterPage . but whenever I try to add a RadioButton to the .aspx page it's generate to me this error:
Control 'head_ctl00' of type 'RadioButton' must be placed inside a
form tag with runat=server.
and I have tried to add a form tag and this error came out !
A page can have only one server-side Form tag.
I'm confused how I can solve this problem ! any idea ?
in case you need my .aspx page :
<%# Page Title="" Language="C#" MasterPageFile="~/MySite.Master" AutoEventWireup="true" CodeBehind="Registration.aspx.cs" Inherits="MyWebsite.Registration" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<form id="frm" runat="server">
<asp:RadioButton runat="server"></asp:RadioButton>
</form>
</asp:Content>
You have inserted your RadioButton inside the head placeholder that has not the form tag in your MasterPage file. You should place RadioButton in another PlaceHolder like MainContent or FeaturedContent. Also you don't need the form tag in your ContentPages, The form tag on the master page will be sufficient.

Contentholder as reference to web form in asp.net

I have got a master page with three content holders. Say; Header, Menu and Content.
I can use Iframes or regular frames, but is there a way to specify the three content holders as follows and redirect them to three different web forms?
The reason I ask this, is because I do not want to specify my menu over and over again for every single web form.
<asp:Content ID="Content1" ContentPlaceHolderID="Header" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="Menu" runat="server">
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="Content" runat="server">
</asp:Content>
You've missed the point of a master page. A master page should contain the markup/code that is common to the content pages or nested master pages that reference it. Put the menu code into the master page.
<!-- Menu code here -->
<ul class="menu">
<li>Home"</li>
<li>Contact"</li>
</ul>
!-- End menu code -->
<asp:ContentPlaceHolder ID="MenuPlaceHolder" runat="server">
<!-- On the content page, your page specific menu code would go in the <asp:Content> that references this -->
</asp:ContentPlaceHolder >
As an alternative to (or in conjunction with) master pages, you can use user controls. You place your menu code in the user control and embed it in master pages or content pages. But if you always want it in the same place, then a master page makes more sense because of the Don't Repeat Yourself principle.
There is absolutely no reason to use an iframe for this.

multiple runat=server forms for different processes in masterpage and inherited webform

I have a masterpage and a webform. In my masterpage,I have treeview (for menu) and I put form tag in that with runat=server.
In my webform, I have registration form created and i put form tag for that with runat= server.
When I buid it, its shows error as One form is allowed in a page.
How to use multiple runat=server forms for different processes in masterpage and inherited webform.
<form id="treeview" runat="server">
// treeview code
</form>
You really dont need to add a form tag in the child form i.e. Registration form. During run time both master and Child will be merged together and will render as a single form.
Here goes your Master page:
<form id="form1" runat="server">
<div>
<asp:ContentPlaceHolder ID="MainContent" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
And here is your Content Or Child page:
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
<!-- Any other controls -->
</asp:Content>
The following tutorial may help you:
http://www.codeproject.com/Articles/333650/Beginner-s-Tutorial-on-Master-Pages-in-ASP-NET
http://www.asp.net/web-forms/tutorials/master-pages
Master form do you mean master page?
Then no, you cannot have 2 forms running on the same WebForm

Default Design view, dropping any tool box items goes completely off page?

Ok, so i am doing a project with ASP.net, and using Visual Studio 2012. I created a new master page, in which i wish to make registration. I simply drop the "Create User Wizard" in the Login toolbox onto my page, and it goes completely off its hinges!!
<%# Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/Site.Master" CodeBehind="Register.aspx.vb" Inherits="WebApplication1.Register1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="FeaturedContent" runat="server">
<asp:CreateUserWizard ID="CreateUserWizard1" runat="server" Height="10px" Width="10px">
<WizardSteps>
<asp:CreateUserWizardStep ID="CreateUserWizardStep1" runat="server">
</asp:CreateUserWizardStep>
<asp:CompleteWizardStep ID="CompleteWizardStep1" runat="server">
</asp:CompleteWizardStep>
</WizardSteps>
</asp:CreateUserWizard>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="MainContent" runat="server">
</asp:Content>
I cannot get anything inline like my previous pages.
Any suggestions?
It's not because wizard or something, it's about styles. You have a better browser (Chrome), just open your page in Chrome, right-click on desired element and choose "Inspect element".
Then take a magnifier:
Point this tool to desired element and check out styles on the right:
Try to enable/disable them right there, and you'll see result immediately.

Null Reference Literal Control

I am trying to pass text to a Literal, the parent.master has the ContentTemplateID's and the Child.master has the contentID's, one of them is the literal of form
<asp:Literal runat="server" ID="myLiteral"></Literal>
I am trying to pass it from a UserControl.cs file like this
gooleTrackin = track.GetTrack(track.OrderType.YAHOO);
Literal mpLiteral = (Literal)this.Parent.Page.Master.FindControl("myLiteral");
mpLiteral.Text = gooleTrackin.ToString(); //nullReference here
But it is giving me NulLReference in the last line.
By the way I do not have access to the .cs files of the master pages, it has to be done through the UserControl.
Thank you
ADDITIONAL CODE (THIS IS LOCATED IN THE CHILD.MASTER)
<%# Master Language="C#" AutoEventWireup="true" MasterPageFile="../common/child.master" %>
<%# Register Src="UserControl.ascx" TagName="UserControl" TagPrefix="uc1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" Runat="Server">
<div class="inner"><uc1:UserControl ID="theRceipt" runat="server" Visible="true"/>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="BottomContentTemplate" Runat="Server">
<div style="margin-bottom:30px;">
<asp:Literal ID="myLiteral" runat="server"></asp:Literal>
Back to Home Page
</div>
</asp:Content>
When master page is used, controls from master page are merged into the control hierarchy of page so this can be an issue while finding controls. I will suggest that you try following and see if it works:
Literal mpLiteral = (Literal)this.Page.FindControl("myLiteral");
OR
Literal mpLiteral = (Literal)this.Parent.FindControl("myLiteral");
Otherwise, you may have to try recursive find - see http://www.west-wind.com/weblog/posts/2006/Apr/09/ASPNET-20-MasterPages-and-FindControl
However, I will rather recommend a alternate way - assuming that you have defined your literal control in Child.Master and Child is the name of code behind class for master, you can add an helper method in it such as
public void UpdateMyLiteral(string text)
{
myLiteral.Text = text;
}
And in user control code, invoke the helper method such as
((Child)this.Page.Master).UpdateMyLiteral("xyz");
Note that we are casting master to its code-behind class.

Categories