I'm getting a list of string from db like:
AddCustomer,
AddUser,
ListCustomer,
ListUser
These are the prefix of asp pages.
I need to hide and show certain pages in the page. Following is the html snippet:
<li>Customer Management
<ul>
<%if (AddCustomer) //how to check whether my string is present or not, is it possible?
{ %><li>Add Customer</li><%} %>
<li>List Customer</li>
</ul>
</li>
Have you tried writing method then calling it from aspx page?
<%# YourMethodName((string)Eval("AddCustomer")) %>
Related
I searched a way to include a file in a web application (like a menu, so I won't have to edit it on all pages when applying changes), but haven't found something as simple as
<?php include "Menu.html"; ?>
Can you please help?
Have you looked into Master Pages? They would certainly help you add the same layout across several pages.
Or perhaps you want a reusable User Control (that you write yourself)?
We don't use "include page" in asp.net, even though it is possible (with a different syntax of course). Instead, have a look at Master page concept.
MasterPages allow you to maintain a parent/child relationship between a master page which contains content that wraps around any number of child content pages.
Similarly, UserControls allow you to re-use whatever content you want on whatever page you want, whether it's a MasterPage or ContentPage:
<%# Page Language="C#" %>
<%# Register TagPrefix="uc" TagName="Spinner"
Src="~/Controls/Spinner.ascx" %>
<html>
<body>
<form runat="server">
<uc:Spinner id="Spinner1"
runat="server"
MinValue="1"
MaxValue="10" />
</form>
</body>
Methods (C#)
Executable code:
Page include
<!--#include file="a.aspx"-->
Execute independently inside a page
<% Server.Execute("a.aspx"); %>
Non-executable code:
<% Response.WriteFile("a.inc"); %>
I believe this is what you are looking for.
<!--#include file="wisdom.aspx"-->
I use C#.net
<% Response.WriteFile("YourPage.aspx"); %>
and this works real well for me!!
I also use your line,
<!--#include file="wisdom.aspx"-->
when I am in HTML mode.
I have two master pages in my C# MVC application. What I would like to be able to do is use one, or the other depending on the users 'role'. Something similar to this (obviously with a little more validation etc):
<% if(User.IsInRole("One")) { %>
<%# Page Language="C#" MasterPageFile="~/Views/Shared/One.Master"
Inherits="System.Web.Mvc.ViewPage<MyApp.Data.ProductData>" %>
<% } else if { %>
<%# Page Language="C#" MasterPageFile="~/Views/Shared/Other.Master"
Inherits="System.Web.Mvc.ViewPage<MyApp.Data.ProductData>" %>
<% } %>
I've seen answers where this can be done to elements of a page, for example a menu, an image, etc. Is it possible to do it for the entire master page? In my situation, depending on the role, different css, images, colours will be used so it is necessary to use a different master page.
If anyone could help I'd be very grateful, or if anyone has any alternative (and probably better) solutions I'd also be grateful.
Thanks.
As you are using ASPX View in ASP.net MVC Application.
ASP.net MVC ASPX ( Webform) view still derive from Page class so you can use following code in
your aspx view.
<%# Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
<script language="C#" runat="server">
protected void Page_PreInit(object sender, EventArgs e)
{
if (User.IsInRole("Admin"))
{
this.MasterPageFile = "~/Views/Shared/Site2.Master";
}
else
{
this.MasterPageFile = "~/Views/Shared/Site.Master";
}
}
</script>
You can change it dynamically via ViewMasterPage.MasterPageFile.
I would suggest making the selection in your Masterpage file rather than selecting which masterpage file to use.
I have a webpage with server accessible controls, see 'FileIconLink' below:
<body>
<p class="FirstTitle style5">Downloads:</p>
<div id="BreadcrumbDiv">
<p style="padding-left:5px; ">Page Loading...</p>
</div><!--/BreadcrumbDiv-->
<div id="DirLinksDiv">
<p><span class="SecondTitle">Files:</span></p>
<a runat="server" href="#" id="FileIconLink">File</a>
<% WriteFileLinks(); %>
<p><span class="SecondTitle">Folders:</span></p>
<a runat="server" href="#" id="FolderIconLink">Folder</a>
</div><!--/DirLinksDiv-->
</body>
<%RemoveHTMLTemplates(); %>
Both 'FileIconLink' and 'FolderIconLink' are templates of web controls which are copied by my code - such as <% WriteFileLinks(); %> above. How could these templates be permanently removed from the web page at run-time on the server without causing the error:
The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).
Thanks in advance!
This is because you have <% %> inside the control you're trying to change. Instead of using <% %> in the aspx page, I would modify the code behind to add a literal control or something to the div, like:
DirLinks.Controls.Add(new LiteralControl(WriteFile()));
You should then be able to modify your control form the code behind.
Your inline code is executed during render.
But you probably want to get rid of the templates during Load.
Which means that the two techniques conflict.
Ultimately I realised my approach was wrong, as Cade Roux was alluding to, I needed to make up my mind where the templates were going to be used.
My solution was as follows:
Make controls for containing the results of my (previously inline) code.
Use templates in Page_Load to fill the controls described above.
Delete templates in Page_Load.
Do nothing inline.
The Page object has another function apart from Page_Load function called Page_PreRender, this function gets executed before Page_Load. So please try remove logic in this Page_PreRender function.
Please refer this link http://msdn.microsoft.com/en-us/library/system.web.ui.control.prerender.aspx
I have several links that look like the following:
<ul>
<li><asp:HyperLink ID="lnk1" NavigateUrl="~/section/sub-section/page1" runat="server">My Link</asp:HyperLink></li>
<li><asp:HyperLink ID="lnk2" NavigateUrl="~/section/sub-section/page2" runat="server">My Link</asp:HyperLink></li>
</ul>
What I want to do is in the code-behind is add a class of selected if the link url matches the url of the page that is currently being viewed.
How would I do this? Thanks
Place your hyperlinks in Panel like this
<asp:Panel id="pnl" runat="server">
<ul>
<li><asp:HyperLink ID="lnk1" NavigateUrl="~/section/sub-section/page1" runat="server">My Link</asp:HyperLink></li>
<li><asp:HyperLink ID="lnk2" NavigateUrl="~/section/sub-section/page2" runat="server">My Link</asp:HyperLink></li>
</ul>
</asp:Panel>
Then in your code behind iterate through each HyperLink control:
foreach (Control lnk in pnl.Controls)
{
if (lnk is HyperLink)
{
HyperLink href = (HyperLink)lnk;
if (Request.Url.AbsoluteUri.Equals(href.NavigateUrl))
href.Attributes.Add("class", "selected");
}
}
Hope this will help..
in the Page.Request object you do have properties to get the RawUrl of the current page, doing a loop in the Page.Controls you could find all your hyper links, you can then compare the NavigateUrl attributes with the page url and you are set.
Keep in mind that if you want to change an attribute of the server controls from code behind you better do it only in the Page_PreRender method because if you it before your changes could be overwritten...
You can get current file name on Master page
string currentpage = Request.FilePath;
It will be something like (you'll need to tweek the equality comparison):
if (Request.Url.AbsoluteUri == lnk1.NavigateUrl) { lnk1.Attributes.Add("class", "selected"); }
I use asp.net 4 and C#.
I have some nested Master Pages; I display in my Content Page a list of Links using a repeater.
This is a sample of code generated by ASP.NET as read in the Source code in the Browser.
As you can see the ID is very lengthy.
My question:
How can I have control on the ID generated, so I can chose another format much shorter?
Please keep in mind that I cannot get rid of Master Pages for my layout.
Thanks for your help on this!
<li>
<a id="ContentBody_ContentColumn2_latestArticle_uxRepeaterLatestArticles_uxLink_0" href="Category.aspx?CategoryId=8">AAAAA</a>
</li>
<li>
<a id="ContentBody_ContentColumn2_latestArticle_uxRepeaterLatestArticles_uxLink_1" href="Category.aspx?CategoryId=12">BBBBB</a>
</li>
I would like instead an ID like:
ID="CB_CC_LA_R_0"
ID="CB_CC_LA_R_1"
Useful article:
http://www.west-wind.com/weblog/posts/2009/Nov/07/ClientIDMode-in-ASPNET-40
http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientidmode.aspx
http://beyondrelational.com/blogs/hima/archive/2010/07/16/all-about-client-id-mode-in-asp-net-4.aspx
Replace the asp:HyperLink with plain HTML anchor tag and use following markup for it:
<a id='CB_CC_LA_R_<%# Container.ItemIndex %>' href='<%# Eval("IndexPropertyName", "Category.aspx?CategoryId={0}") %>' >
<%# Eval("TextPopertyName") %>
</a>