Null Reference Literal Control - c#

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.

Related

Accessing Content Page id with dynamic generated id

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;

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.

Displaying Text in ASP Content from C# Code

This question most likely has an easy solution, but I cannot figure it out. I have a .aspx page with the following code:
<%# Page Title="Update ASV Information" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="UpdateASV.aspx.cs" Inherits="COAF_Process_to_ASV_Relation_Tool.UpdateASV" %>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server" >
<asp:PlaceHolder ID="PlaceHolder1" runat="server">
</asp:PlaceHolder>
<asp:Button runat="server" ID="asv_update" OnClick="asvUpdate_Click" Text="Update ASV" />
</asp:Content>
I want to be able to write text from the C# code behind this page (UpdateASV.aspx.cs). Whenever I try:
Response.Write("some text");
It puts the code behind the content2 placeholder. I want it inside. Is there an easy way to do this?
Response.Write directly modifies the response. You don't want to do that in your web forms application. You should be modifying the contents of a Label, Literal, or PlaceHolder control from the code-behind.
Use Label to put text into the page
Use Literal to put raw html into the page
Use PlaceHolder to add new controls to the page dynamically.
Either way, the placement of the control(Label, Literal, or PlaceHolder) on your page determines where on the page your output will be rendered.
If you are determined to stick to server side controls:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostback)
{
Literal myText = new Literal()
{
Text = "some text"
};
Content2.Controls.Add(myText);
}
}
Otherwise, just put the code inline
<%=Response.Write("some text") %>

ASP.NET 4.0 C# "state information is invalid for this page and might be corrupted." MasterPage->Aspx->Ascx

This is my first SO post, so please forgive any faux pas. I feel as though I've tried everything on every post related to this error (disable viewstate, noCache, etc) and I'm at my wits end.
I have a blank project with a single master page, a single page, and a single control.
The page (aspx) loads using the master page. This works fine. There is a button on the page which loads the control (the ascx) onto the aspx in the section called divRightMainView. This also works fine (which is where my problem seems to differ from all others I've found...). There is a button on the ascx which is supposed to call the code-behind of the ascx - this is where I get the "state information" error.
This is the aspx:
<%# Page Title="" Language="C#" MasterPageFile="~/Site.Master" EnableEventValidation="false" AutoEventWireup="true" CodeBehind="Outer.aspx.cs" Inherits="TestProject.Outer" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
<script type="text/javascript">
function loadUserView(viewToLoad) {
PageMethods.RenderControl(viewToLoad, onSuccess, onError);
}
function onSuccess(results) {
alert(results);
var command = results.split('##')[0];
if (command == 'loadView') {
var htmlToLoad = results.split('##')[1];
$get('divRightMainContentView').innerHTML = htmlToLoad;
}
}
function onError(error) {
alert('error ' + error);
}
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<asp:ScriptManager ID="ScriptMgr" runat="server" EnablePageMethods="true">
</asp:ScriptManager>
<div id="leftSideContent">
<input type='button' id="clickToLoadASCX" value="Click Me" onclick="loadUserView('/inner.ascx');"/>
This is the "Outer.aspx" page which will hold the ascx after the button is clicked
</div>
<div id="divRightMainContentView">
</div>
</asp:Content>
Aspx code behind (which renders the ascx)
[WebMethod]
public static string RenderControl(String controlName)
{
Page page = new Page();
Control control = page.LoadControl(controlName);
HtmlForm form = new HtmlForm();
form.Controls.Add(control);
page.Controls.Add(form);
StringWriter writer = new StringWriter();
HttpContext.Current.Server.Execute(page, writer, false);
return "loadView##" + writer.ToString();
}
This is the ascx called inner.ascx
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="inner.ascx.cs" Inherits="TestProject.inner" %>
<div>
This is the inner text. When I click the button below I should proceed to inner.ascx.cs to the "PageBehindCall_Submit".
<asp:Button ID="innerButton" runat="server" onclick="PageBehindCall_Submit" Text="Submit"/>
</div>
And finally, the ascx code-behind
protected void PageBehindCall_Submit(object sender, EventArgs e)
{
string str = "This call does not work!";
}
I'm hoping to be able to use the "PageBehindCall_Submit" to process data, grab inputs from the ascx, etc. Let me know if there is a way to make this happen, or another possible work-around? Any help would be appreciated!
Why not load the ascx on page load of the ascx but turn the visibility off rather than manually rendering user control html. This is very weird that it does not hook up with the event in ascx as the control belongs to ascx. But I think you should add it in aspx then try to turn on visibility whenever you need it.

Locate HTML Element when it does not have the runat="server" attribute

I have an ASP.NET C# website and I am attempting to retrieve the inner html from a HTML in my page when a button is clicked.
My Problem: I use the function FindControl() but it returns null when it should find the element. Why is this happening, what am I doing wrong?
My HTML page:
<%# Page Title="test" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeBehind="test.aspx.cs" Inherits="test" %>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="ActualContent">
<div class="regMainContainer mainContainer container">
<!-- Attempting to get the below div -->
<!-- Knowtice it does not have runat="server",
I would prefer not to do that if I dont need to -->
<div id="courseDiv" class="courseContainer">
</div>
</div>
</asp:content>
My Code that executes when a button is clicked(inside test.aspx.cs file:
HtmlGenericControl d = (HtmlGenericControl)FindControl("courseDiv");
FindControl can only find controls that has runat="server" attributes. The good news is that you can easily change you div to a panel, and add data that way.
the bad news is that this still won't let you access the innerhtml of the div, .net sees it as just a container for other controls.
The best way to do this would be some form of ajax, since your client side script would be able to read that contents and pass it to a server side method
You won't be able to use FindControl for something that's not marked with runat="server". This is because any of the elements not marked with runat="server" get parsed as one giant text literal at runtime. You can add runat="server" to any arbitrary html element, though.

Categories