Asp.net div still not accessible after runat="server" - asp.net

I have a div element in my HTML. I added a id and runat attributes to the element:
<div id="footer" runat="server">
After rendering, viewing the HTML shows:
<div id="ctl00_footer">
However, I cannot access it from the page's .aspx.cs code:
footer.InnerHtml += "test";
How do I access that element from the C# code?

you can use FindControl("footer"), and cast it to HtmlGenericControl.
HtmlGenericControl footer = (HtmlGenericControl)FindControl("footer")

One of the reasons can be that you don't have designer file for that page. So you can't access element by it's ID.
Just add class with name [your page name].aspx.designer.cs, open it, remove all code, save it, go to your view and click save - designer must generate code of all elements from your view. After this you can access element by ID.

There should be no problem accessing <div id="footer" runat="server"></div> the way you are doing. Strange though, my generated markup keeps the div id unchanged as footer.
Make sure you don't have any compile errors, and that you can access other elements running server-side in the same scope you are trying to access this div.

You need to set the ClientIDMode property of the page or control to Static:
http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientidmode.aspx
This will prevent the "ctl00_" from being appended to the ID which is what is causing you the problem.

I have encountered this problem before. You may have the target div inside another div that does not have the runat="server" attribute. All nested divs should have the runat attribute in order to be able to access the inner elements.
<div id="divContainer" runat="Server">
<div id="yourDiv" runat="Server">
</div>

If you're going to code ASP.NET and you want to access the control from the server-side, you may as well use the provided controls.
Use a Panel instead of a Div. The ASP:Panel control renders as a div in the generated html anyway. The Panel doesn't have a .Text property, but you can add controls to it from code-behind (such as a Label or a LiteralControl.

Is there possibly a chance that the page was copy/pasted when being created? If so, make absolutely sure that all references to the old page are changed to the name of the new page. I've done this before within the code at the top of the ASPX page, as well as the namespace of the designer page.

I had the same problem and found out that due to a "copy" and "paste" my function had a "static" declaration.
Removed it since static functions can't access non-static identifiers and it was fixed.

I have the same issue.
My solution was to have 'ID' instead of 'id' for the div element (i.e. the casing was the reason).

Related

Access web part properties from within CMSRepeater Template

Is there any way I can access a webpart's properties from withing a repeater's template (or vice versa)?
<div ID="RepeaterWrapper" runat="server">
<cms:CMSRepeater ID="repItems" runat="server">
<ItemTemplate>
<div class="col-sm-4">
<!-- I want to access this div in my code behind or else have it access a property from the code behind-->
</div>
</ItemTemplate>
</cms:CMSRepeater>
</div>
I want to set the inner div's bg color and I can't use classes as the property is given as a hexadecimal color so it would mean a few thousand classes!
Worst case scenario I can do it with some js but would rather a "purer" way of doing it if it exists.
Thanks in advance
Assuming your datasource has that background color in the returned data, once you bind your datasource to the repeater you have access to that within the item templates. Simply use something like this:
<div class="col-sm-4 <%# Eval("BgColorColumnName") %>">
Now if you want to set a value from the actual webpart itself, you need to make sure the property is a public property then you can use something like:
<div class="col-sm-4 <%# YourPublicPropertyName %>">
Are all the items going to have the same color? If its per item, then modify the items you are pulling to include the value.
If this was in portal method you could grab the XML from the Page Template table and get values from it. Since it's purely from code, and it's a repeater, usually you need to store the data somewhere outside the repeater itself (in the items you repeat, or in the current page form data).
If you can access it anywhere from a Macro, then you can use the CMS.MacroEngine.MacroContext.Current.ResolveMacro() to resolve that and get the value.
Can you give us a little more info on where the div BG color would be stored? why it has to be in the repeater itself?

How can I load data into an HTML div tag?

I am trying to create a product review web page. The product review has product image, title, description, etc, in a SQL database. How can I load this datas into an HTML div tag, using ASP.NET?
Add the following to your div
<div runat="server" Id="dataDiv"></div>
And then in your CodeBehind you can call your div with its Id
dataDiv.InnerHtml= YourData;// this can be anything from database or a variable
hope it helped.
Thanks,
Aneef
You can add an ID property and runat="server" to <div> to be able to reference it from code behind, and add HTML using the InnerHtml property.
You can also use an <asp:Panel> control, which emits a <div>, and insert your HTML by adding a Literal control from code behind that contains your markup. That approach also allows you to add controls to the <div>, should you need to.

Control that doesn't render during design

I'm sure this has a simple answer, but I can't find it. I need to create an ASP.NET control that doesn't render anything. This is similar to an ObjectDataSource that shows up as a gray box in the aspx design mode. Until now I have only created controls that DO render and I can't find what property, attribute, override, etc. will prevent rendering during design. Any pointers?
Edit: Just to clarify, by simply inheriting from Control, it renders [ TypeName "ControlId" ]. I want it to render as the gray box that says TypeName - ControlId.
OK well I played around with it and you're right, it's not trivial.
Turns out VS doesn't actually execute .ascx user control code when it's shown in the designer, it only parses the markup, so you can't conditionally change the control.
However, that limitation is not active when you use a real ASP.NET Server Control. Just add a new project to your solution of that type and in your .cs file, overrite RenderContents:
protected override void RenderContents(HtmlTextWriter output)
{
if (DesignMode)
output.Write("<table style='border-color:Black;background-color:Gray'><tr><td width=300px height=100px style='vertical-align:middle;text-align:center'>I'm your design time box.</td></tr></table>");
else
output.Write("Runtime text lalala");
}
Then in your .aspx file, you just add the control:
<body>
<form id="form1" runat="server">
<div>
<cc1:ServerControl1 ID="ServerControl1" runat="server" />
</div>
</form>
</body>
Design time result:
Run time result:
The property you are looking for is called DesignMode

adding runat="server" changes the behaviour of the layout

I have a page with some controls, usercontrols etc.
when I change a div from plain <div id="foo"> to a <div id="foo" runat="server">
the layout complete changes.
why is that and how can I prevent it?
I'm using 2.0 .NET framework
Is it because .NET changes my id, which obviously I don't want?
If you're targetting the ID of the div control in CSS and then running the control at server, you'll find it no longer applies the style.
This is because ASP.NET has a built in mechanism (INamingContainer) to ensure than you don't have multiple controls named the same. It does this by adding container prefixes so you end up with:
<div id="ctl00_ctl00_myDivName" runat="server" />
The easiest way around this is to change it from working on an ID to working on a class:
<div class="myDiv" runat="server"></div>
Alternatively, I believe that XHTML requires that Divs have closing tags so use
<div runat="server">Some content</div>
When you add runat="server" to a div, the system automatically generates the ID for it. It's referred to as ID mangling. Unfortunately there isn't much that you can do in the 2.0 framework for divs that I'm aware of (without it being a pain anyway), but in 4.0 we're getting an override... On custom controls though (in 2.0) you can override the ClientID and UniqueID fields. So if you created a MyDiv class that used the div as a base and then created the ClientID/UniqueID fields you should be ok.
Your other option would be to update your CSS/javascript to use the mangled ID. It's fairly static based on the position within the page as ASP.Net uses it to find a control during postback.
Add ClientMode="static" this will make sure your id is not changed to the clientside id for your control.

RegisterClientScriptBlock without form tag

After trying to understand why client code is not rendered in a page (injected by user control) I found this link, it turns out you must have a form tag for it to work (Page.RegisterClientScriptBlock did declare this but ClientScriptManager.RegisterClientScriptBlock which I use does not say anything regarding this).
I am using Visual studio 2005.
Does anyone know if this has been solved?
Edit:
To clarify, I want my control to add javascript code to the head section of the page without having to use the
<form runat="server"
I have tried adding it using:
HtmlGenericControl x = new HtmlGenericControl("script");
x.InnerText = "alert('123');";
Page.Header.Controls.Add(x);
But this did not work for me.
As far as I know this functions the same in current versions, you can test it very simply though.
Update
per discussion in the comments, the only "workaround" that I could think of would be for your to manually insert the script into the "head" section of the page on your own, using a runat="server" declaration on the Head element.
Got it!
My mistake was not doing it in the OnPreRender method (I used the Render method).
Now all that is needed is - like Mitchel Sellers wrote, set the header to runat server and than add to it's controls:
HtmlGenericControl x = new HtmlGenericControl("script");
x.InnerText = GetScriptSection();
Page.Header.Controls.Add(x);
Thanks for pointing me to the right direction!
The MSDN Page for registerclientscriptblock here says:
The client-side script is emitted just
after the opening tag of the Page
object's <form runat= server> element.
The script block is emitted as the
object that renders the output is
defined, so you must include both tags
of the <script> element.
If you do not want to include a form, than you will basically need to build your own implementation of it.
Minor clarification for anyone seeing this:
The form tag must have the runat="server" attribute set, e.g.
<form id="theform" runat="server">
Just placing a regular HTML form tag in the page will not help.

Resources