I've got a Master Page with multiple ContentPlaceHolders. One of them will sometimes be empty. If they both have content, I'd like to make visible a buffer so there is some space between them, but this should remain hidden otherwise.
My question is, how can I determine from the C# code-behind of my Master Page whether the ContentPlaceHolder of a child page has any content assigned to it? All I really want is a boolean yes/no answer.
Thanks!
perhaps
YourContentPlaceHolder.Controls.Count > 0
Would that work for you?
Maybe I am missing something, but it seems you just need to get spacing. Why not just wrap the first ContentPlaceHolder in a div with a padding-bottom? There may be a more efficient way to do this, but you get the idea.
You could programmatically add a div to your first ContentPlaceHolder with something like ...
<div class="spacer" />
and in your css have this ...
div.spacer { margin-bottom: 5ex; }
you may need to put a   in the div to get around some browser bugs :)
Related
My Question
I managed to answer myself, however the same set of functionality has another problem. For some reason the first postback of the save event of the posted file hits the Ol' Object not set to an instance of an object error, but on the second attempt of uploading a file and firing my save event (converts to byte[] an stored as SQL Server BLOB) it does everything is supposed to do.
Same problem here
There is a good suggestion of using the AJAX AsyncUpload control however I am a firm believer of removing the cause and not treating the problem. I will continue down this route to best my understanding of asp.net etc.
Would there be a wizrd amongst you that could help me identify why I get "object ref not set to inst of obj" error on first postback but on second it works fine. Content page has a master page which wraps content page in an update panel. Not my decision to do this. There is also an update panel with postback triggers targeting my save event.
What are your thoughts people?
The problem (as seen here http://forums.asp.net/t/1060363.aspx) seems to be when you use the visibility property on the surrounding panel (as it seems you are from the linked question).
The suggested workaround is to use CSS visibility instead so use this to make it invisible -
<asp:Panel ID="pnlUpload" runat="server" class="workerDetailsPanelLeft" style="display:none">
The explanation for this from the thread is
If your container is set to invisible, the upload control is not actually rendered as HTML, causing the form's enctype not to be set to enctype="multipart/form-data", causing the file upload control not to post the selected file back to the server. The workaround is either to make sure the FileUpload control is rendered to HTML (by setting its style to display:none in stead of Visible=false), or by manually setting the enctype
So another workaround would be to alter your form tag to this
<form id="form1" enctype="multipart/form-data" runat="server">
I think either one of those should solve your problem.
You could do the same thing via code on Page_Load event.. Enter this code and it will solve the issue.
Page.Form.Attributes.Add("enctype", "multipart/form-data");
My question: How to create an on hover effect with for my ASP.net menu.
The problem is that since the standard ASP.net menu doesnt have an on hover function, its quite hard/impossible(?) to create.
After doing my homework i saw some people who tried it with css, jquery and other methods... but i would prefer 'normal' html or css without incredibly complicated solutions.
Should that really be impossible, is there any clear way how to use it?
Most of the css options failed, or were incredibly complicated.
What exactly should happen:
A menu item for example
<asp:MenuItem NavigateUrl="~/Pages/Test.aspx" Text="Test" Value="Test" Selectable="true"></asp:MenuItem>
Should get a different background because of on hover effect.
Maybe changing the CSS from within the C# code into something else?
Thanks for your answer!
~Solved~
Thank you very much for your help! The problem is solved thanks to both of the answers.
I tried inplementing that menu, when i noticed that in this menu the hover effect also didnt work. What was the problem? I use 2 css files. And because the first css file contained some instruction as to how to handle the a:hover, all of the other hover effects were ignored.
Now that i know why the hover effect wasnt working, i can solve this problem. Thank you both very much!
Alright guys, it took me quite some time to figure out how to create the on hover effect with the ASP.net menu. So, i found an easy way, without all the hard stuff or lots of code.
If you look at your asp page in your browser, go to page source. When you look at your menu, you will notice that it looks like this:
<li>
<a class="level1" href="Home.aspx">
<img src="../Images/home.png" alt="" title="" class="icon" />Home</a></li>
<li>
<a class="level1" href="Page2.aspx">
<img src="../Images/homehover.png" alt="" title="" class="icon" />Page2</a><ul class="level2">
The solution would be to change your css:
a:hover[href*="Home"][class*="level1"]
{
color: black;
}
This piece of CSS selects the A element, where (in the HTML) the attributes href and class contain(or are equal to) Home and level1.
It might be a bit different if you have a more complex menu then i do, for i have just 2 levels deep. (menu and submenu)
Hope it helps you guys!
Then this is the end of my first question/answer on stackoverflow.
Thanks for your help!
ps: If it doesnt work, remove all a:hover statements in all related css files to the page with the menu. It might also be a problem if you use the StaticHoverStyle/DynamicHoverStyle.
I'm very new to ASP.net. I have a c# content page, in which I want to inset this code half way down within the HTML:
<%
HttpResponse r = Response;
r.Write(HttpContext.Current.Request.ServerVariables["SERVER_NAME"]);
%>
But when I view the page, this content comes out first, before even the tag.
Any ideas on how to get this code inline instead?
Thanks!
EDIT
I'd just like to add a note to all who answered this question to explain what you've done.
You spared your valuable time to help me, a stranger to you, solve a difficult problem at work, which allowed me to get out of the office on Friday night, just in time to catch the last bus to my home 50 miles away, and see my wife who was sick in bed. You didn't just answer my question, you made my day SO much better. THANK YOU so much!
Steven
Because you are doing a Response Write, that will push out before everything else. If you want to just imbed something at a specific point you can do:
<%= HttpContext.Current.Request.ServerVariables["SERVER_NAME"]) %>
This <%= %> will write any string to that exact location in the HTML.
You could also use a Literal control and assign its Text property in your codebehind or use a Label if you require formatting.
You can put a label on the page where you want the text to appear and then set the label's text instead of doing like this. Simply put an asp label on the page and frmo your code behind do
myLabel.Text = HttpContext.Current.Request.ServerVariables["SERVER_NAME"].ToString();
where myLabel is the ID of your label in your HTML Markup.
Define a DIV on the page like this, where you want the outputted string to be displayed:
<div id="myDIV" runat="server" />
Then, instead of r.Write() you can simply set the inner text of the DIV to be what you want:
myDIV.innerText = HttpContext.Current.Request.ServerVariables["SERVER_NAME"];
when you use Response.Write("..."); its shows up before page header , instead of using Response.Write you can put a label on the form wherever you want to see the message and set label's Text property.
Or if you just want the text, with no html markup.
Use a literal.
In aspx file
<asp:Literal ID="MyLiteral" runat="server" />
in code-behind
MyLiteral.Text = HttpContext.Current.Request.ServerVariables["SERVER_NAME"].ToString();
The Scenario: I have an asp.net website where I show a div popup on page load for taking a few user details. When a user inputs the details, or closes the popup, I set up a flag cookie so that the popup is not displayed again for the user. The div is in the MasterPage so that it is displayed no matter on which page a user lands first time. The div contains an UpdatePanel which has all the controls required for taking the details. This whole functionality is working fine.
The Problem: Now this div popup is not showing(by setting display:none) on subsequent postbacks(which I want), but the html markup is still loading with the page unnecessarily adding to the page size. What I would idealy want to do is: Check if flag cookie is set. If no, show the popup, else remove the popup's markup from the page.
Now since the div is not a server control, I cannot possibly remove it and the all the controls inside it. So, I thought of removing the UpdatePanel from the page:
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Cookies["flag"] != null)
{
if (Page.Controls.Contains(updpnl_contact))
{
Page.Controls.Remove(updpnl_contact);
updpnl_contact.Dispose();
}
}
}
But I guess this tends to work with dynamically added controls only, and since the control is added at Design Time, it is not being removed.
Is there any way I can achieve this?
If you add a runat="server" attribute to your <div> element, it will be available in the code-behind. You'll need an id on it as well. Then you can just toggle the Visible property. If this property is false, the control won't be rendered to the client (i.e. no HTML markup).
What you're trying to do is not at all the usual workflow. I tend to think that it will not work as it would mess up control tree, maybe even corrupt the viewstate and so on.
As a possible solution, you can put it's visibility to hidden in the code behind. This, in the contrary to the usual 'gut feeling', doesn't work like the css propery 'display:none' for example - instead the control will not even be rendered into the page when it's not visible. This may be the workaround for you.
Happy coding.
A more efficient approach would be to create the panel as a UserControl and load it dynamically in codebehind when it's needed, then add it to your page. E.g, in code:
MyPopupControl popup = (MyPopupControl)Page.LoadControl("/path/to/usercontrol.ascx");
PopupPanel.Controls.Add(popup);
Where PopupPanel is an empty <asp:Panel>. Then, not even the markup will need to be loaded/processed except when its needed.
There is no reason that all the code you use to display and process this panel couldn't also be in the usercontrol, isolating it from the master page.
Can you build the panel dynamically, based on the cookie setting?
I have run in to a bit of a problem and I have done a bit of digging, but struggling to come up with a conclusive answer/fix.
Basically, I have some javascript (created by a 3rd party) that does some whizzbang stuff to page elements to make them look pretty. The code works great on single pages (i.e. no master), however, when I try and apply the effects to a content page within a master, it does not work.
In short I have a master page which contains the main script reference. All pages will use the script, but the parameters passed to it will differ for the content pages.
Master Page Script Reference
<script src="scripts.js" language="javascript" type="text/javascript" />
Single Page
<script>
MakePretty("elementID");
</script>
As you can see, I need the reference in each page (hence it being in the master) but the actual elements I want to "MakePretty" will change dependant on content.
Content Pages
Now, due to the content page not having a <head> element, I have been using the following code to add it to the master pages <head> element:
HtmlGenericControl ctl = new HtmlGenericControl("script");
ctl.Attributes.Add("language", "javascript");
ctl.InnerHtml = #"MakePretty(""elementID"")";
Master.Page.Header.Controls.Add(ctl);
Now, this fails to work. However, if I replace with something simple like alert("HI!"), all works fine. So the code is being added OK, it just doesn't seem to always execute depending on what it is doing..
Now, having done some digging, I have learned that th content page's Load event is raised before the master pages, which may be having an effect, however, I thought the javascript on the page was all loaded/run at once?
Forgive me if this is a stupid question, but I am still relatively new to using javascript, especially in the master pages scenario.
How can I get content pages to call javascript code which is referenced in the Master page?
Thanks for any/all help on this guys, you will really be helping me out with this work problem.
NOTES:
RegisterStartupScript and the like does not seem to work at any level..
The control ID's are being set fine, even in the MasterPage environment and are rendering as expected.
Apologies if any of this is unclear, I am real tired so if need be please comment if a re-word/clarification is required.
Put a ContentPlaceHolder in the head section of the master page, then add a asp:Content control on the content page referring to the placeholder and put your script in that control. You can customize it for each page this way.
Also, the reference by ID may not be working because when you use Master Pages, the control IDs on the page are automatically created based on the container structure. So instead of "elementID" as expected, it may be outputting "ctl00_MainContentPlaceHolder_elementID" View your source or use firebug to inspect your form elements to see what the IDs outputted are.
Isn't it possible to do with clean javascript ?-)
-- just add something similar to this inside the body-tag:
<script type="text/javascript">
window.onload = function(){
MakePretty("elementID");
}
</script>
By the way the script-tag has to have an end-tag:
<script type="text/javascript" src="myScript.js"></script>
Why not use jQuery to find all the controls? Something like this:
$(document).ready(function(){
$("input[type='text'], input[type='radio'], input[type='checkbox'], select, textarea").each(function(){
MakePretty(this);
});
});
This way you'll get all elements on the page, you can wait until the page is ready (so you don't modify the DOM illigally). The jQuery selector can get the elements in a bit more of a specific format if you need (ie, add a root element, like the ID of the body div).
It'd also be best to modify the MakePretty method so it takes the element not the ID as the parameter to reduce processing overhead.
Once you use Master Pages, the ids of controls on the client side aren't what you think they are. You should use Control.ClientID when you generate the script.
When using master pages, you need to be careful with the html attribute ID, since .NET will modify this value as it needs to keep ids unique.
I would assume your javascript is applying css styles via ID, and when you are using master pages the ID is different than what is in your aspx. If you verify your javascript is always being added, your answer needs to take into account the following:
ALWAYS set your master page id in page load (this.ID = "myPrefix";)
Any HTML element in your master page will be prefixed by the master page id (i.e.: on the rendered page will be "myPrefix_myDiv")
Any HTML element in your content place holder id will be prefixed with an additional prefix (i.e. myPrefix_ContentPlaceHolderId1_myDiv)
Please let me know if I can clarify anything. Hope this helps!