ASP.NET best control to hide data inside it? - asp.net

I would like to use a control which will contain links to other pages and put all these links inside a control (EG table) so that I will be able to hide it with code (show only for administrator)
Other than a table which control should I use? Should I use a panel or something else?

If you want to hide some link use simple Hyperlink control and set visible property only for administrators. You can use Repeater control to render table and and in ItemTemplate set Hyperlink.
Otherwise you can use Placeholder and place table in it, and set visible property of Placeholder, Placehodler wont render anything.
And after all you can use that same Table, add ID and runat="server" attributes and set visible from Code behind.
for example :
<table ID="myTable" runat="server">
...
and then in code hide whole table :
protected void Page_Load(object sender, EventArgs e)
{
myTable.Visible = false;
}

I would use a <asp:PlaceHolder because it is just a placeholder.. and has no other semnatic value like table,panel or this things..

Depends on your goals, as Antonio Bakula said, you can go through and set each individual link as non-visible.
Or if there are a lot of them, throw them in a panel that you can switch on and off wholesale.

You can use a panel to insert all your other controls in it (hyperlinks, buttons...etc)
and in your code you can show and hide your panel and all what's inside by calling
panel.visible=true
panel.visible=false

A panel is a good choice.
An <asp:Panel /> is rendered in HTML as a <div>. If you aren't familiar with HTML, a <div> is just a box, and you can put anything (including a <table>) inside of one. For example, each answer on StackOverflow is shown inside of a <div>.
If you set Visible="false", then the entire <div> will be removed from your HTML output, even though it exists on the .aspx page. Basically, you can very effectively hide the links from non-administrators using this approach.
However, this is stil not very secure. It is very common for malicious users to attempt to find Administrator links just by guessing them (e.g.: /admin/default.aspx, /admin/admin.aspx, etc...). You should use roles to prevent users from accessing your Admin links. Roles prevent certain types of users (e.g.: Users who haven't logged in, or non-admin users) from even visiting these pages. Instead of seeing the page, the users will be shown an error message instead.

Related

Application wide Modalpopupextender

I'm trying to build a modalpopupextender, along with a panel and content, and need to make it application-wide. I am thinking about creating it on the Masterpage, so it's accessible on all pages, but I need the content inside the panel (anything that I may need to add there) to be visible and editable from outsite the masterpage.
For now, I'm working on this, but haven't figured out how to make it accessible to other pages and classes, and so would like to have some help on it.
Basically, what I want is to work more on the idea in a near future in order to make something consistent to be used on any web application, and to be fully customizable. What I'm having trouble is with "basics", like making it accessible to the application, allow customization of some controls inside the panel from both server and client sides, and will improve everything from there.
I have tried creating a user control for it, but didn't seem to work. I'm not an expert on asp.net (few years of experience), and even less on ajax, so any help is appreciated.
Please let me know if anybody have any questions.
EDIT:
I have now succeeded somehow creating the moodal within a user control and it's almost done.
At this moment, there are 2 issues I couldn't fix:
The damn flickering that happens on Firefox 3.5 (Corporate version, can't touch this). Ocasionally during page load (Somewhere near Page_Init or Page_PreInit events, not sure), the modals I have blink quickly on the screen, only when a postback happens. I have already done some workaround, like setting style display:none, but the issue remains. Need some help on the matter.
I need to have a modal that have 2 behaviors, like windows popups. One is information, likee only showing the message with some buttons, and the other is question. For questions, I'll need to use the ConfirmButtonExtender, and so would need to tell this confirm extender and the modal that an external button (Means a button that isn't within the user control, and by that means it's outside the same UpdatePanel as the confirm extender and modal extender) will be their TargetControlID. For now, I couldn't solve this, so I thought about creating a button inside the UC and UpdatePanel that will always be the TargetControlID. When the popup is informational, it will work as a dummy hidden button (information messages are called on server-side through methods), and when it's a question, it will receive the response. The method to be executed by the button will be set through a delegate, and therefore any method may be run when it's clicked and the Yes button on the modal is pressed (It's not ready yet, and I'm not sure it will work, or even if it's a good idea).
Any thoughts on this second option is appreciated.
It's easy for elements on the masterpage to be visible and editable from outsite the masterpage.
In this example the masterpage has a label that you want to read/write from other pages
<asp:Label ID="lblSubTitle" runat="server" Text="sub title"></asp:Label>
In the codefile for the masterpage, create a property for the subtitle:
public partial class MainMasterPage : System.Web.UI.MasterPage
{
public string SubTitle
{
get
{
return lblSubTitle.Text;
}
set
{
lblSubTitle.Text = value;
}
}
}
Then any page that uses this masterpage as its MasterPageFile can find the subtitle property and read or write it.
// get a reference to the masterpage
MainMasterPage master = (MainMasterPage)Master;
//set it to the value you want.
master.SubTitle = "Custom sub title";
I have solved the issue and created a user control containing the modalpopup for showing customized messages.
Many aspects are public, so it allows high customization, and the modal and it's customized buttons work like a charm. I still have a problem regarding the flickering, but it's for another question.

Accessing app setting from aspx and add concatenated text

I've got a project where I'm tasked with changing hard-coded domain references from one domain to another in a group of legacy C#/VB web projects. I want to parameterize the domains as much as possible instead of just replacing with a different hard-coded value. The problem is that there are over 800 of these references in about 30 different solutions, so creating variables in each code-behind to bind to would take forever.
I have added the new domains to the appSettings section of the web.config file, and this works:
<asp:HyperLink Text="Link" NavigateUrl="<%$appSettings:DomainX %>" runat="server" />
But I need to be able to do something like this:
<asp:HyperLink Text="Link" NavigateUrl="<%$appSettings:DomainX %>/newPage.aspx" runat="server" />
But when I add the "/newPage.aspx" the page doesn't compile anymore. I don't really care if this is done with an asp:HyperLink tag or just a tag.
Any ideas on how I can accomplish this?
Thanks.
I think you have two options. The easiest is to just use a plain old anchor tag, if you're not doing anything with the HyperLink server side:
Link
Alternatively, you could set the NavigateUrl in the Page_Load, since <%= will not work properly within the HyperLink server tag:
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
link1.NavigateUrl = string.Concat("http://",
ConfigurationManager.AppSettings["DomainX"], "/newPage.aspx");
}
You could also see if you can make a custom binding, something like $myBinding:DomainX, but I don't know if that's possible off the top of my head (I would assume it is though).
EDIT
That $appSettings:DomainX code is called an ASP.NET Expression, and can you can create custom expressions. This post from Phil Haack covers how to set them up, in case you are interested.
How about something along the lines of:
<%=ConfigurationManager.AppSettings["DomainX"].ToString() + "/newPage.aspx" %>
I would go with one of two different approaches that would save you the need to change the NavigateUrl in the .aspx itself.
One option is to inherit from HyperLink class and overriding the NavigateUrl property, adding the ConfigurationManager.AppSettings["DomainX"] in the getter method. Having this, just change <asp:HyperLink ... to <UC:MyLink ...
Second option is adding small function to each page (can be in one shared place and just called from each page) that will iterate over all the hyperlinks controls and dynamically add the domain. To find all controls of certain type you can use such code for example.

single template server control

Is it possible to have a custom server control with a single template (meaning the user can put any text they want) without having to require the an "ItemTemplate" like in a FormView control?
I would want the control in Source View to look like this
<foo:mycontrol runat="server" id="controlid">
User puts whatever html content they want here
</foo:mycontrol>
INSTEAD OF THIS
<foo:mycontrol runat="server" id="controlid">
<ItemTemplate>
User puts whatever html content they want here
</ItemTemplate>
</foo:mycontrol>
My custom server control needs to add 2 asp.net panel controls and the ajax collapsiblepanel control. one panel will be the expand/collapse panel and the other panel is what I would want to put the user text into and then have the collapsible panel collapse and hide the panel.
I know how to do this (at least I think I do) creating a composite server control and using ITemplate but that requires the child <ItemTemplate> tag in source view.
Any ideas?
I haven't tested this but I would think you could do this by inheriting from the literal or label control and then reading/writing to the Text property.
p.s. next time when you post a question check the preview to see if it's readable and format code with 4 spaces in-front so it's actually shown and syntax highlighted.
Hmm why don't you use an approach where you just specify the controls to be collapsed. For instance your declaration could look like
<foo:mycontrol runat="server" id="controlid" TargetControlId="pnlToCollapse" />
Internally, your mycontrol gets an instance of the specified TargetControlId by using the FindControl method (here's a recursive version). The same could be done for the 2nd panel you need.
Your server control does therefore just take configuration info and doesn't render anything, but controls the rendering of the other panels in this case. It is a much more flexible solution in my eyes.
Juri,
I should have clarified that this control might be used by non-developers or developers who we do not want to require too much knowledge of setting properties so I'm trying to develop a control where they would drag it on like a panel and just enter text and maybe set one property which would be the title. I was able to create a compositecontrol which I created a title panel, the collapsible panel and utilized the asp.net ajax collapsiblepanel control. I had to add an Template though which I would prefer not to do.
Darrell

How do I access Page controls from within my custom server control?

I am building a modal box as a custom server control and I want to have a property on the modal box TargetControlID that specifies the element that will show the modal when clicked. I've set the property up in the modal box and in the code behind I use the following code block (which I've tried in several different places
If (_targetControlId <> "") Then
Dim targetControl As WebControl = Me.Page.FindControl(_targetControlId)
targetControl.Attributes.Add("onclick", "test1();")
targetControl.Attributes.Add("onclick", "test2();")
End If
What happens is that targetControl always winds up to be NULL, and causes the page to crash when I tried to add attributes to it. I've double checked the spelling of the targetControlId and I am specifying a control that is runat="server". What is the proper way for a server control to access other controls on its containing page?
Thanks,
Mike
First of all, I should point out that the behavior you're looking for already exists in the ModalPopupExtender that comes with the free, open-source AjaxControlToolkit. I'd recommend you just use that. If you're still sure you want to write your own, then I'd recommend at least taking a look at their code to see how they go about it. ExtenderControlBase.FindControlHelper is a good place to start.

Access html control from server side

How can i access html control from code behind in asp.net. I do not want to use "runat=server" as it is causing problems.
I have made an empty html table and from my code behind, I want to add <td> to it.
Please help.
Thanks,
Prachi
This is not possible without runat="server".
You say this was causing problems. Please say what problems it was causing.
You say you were accessing the table using getElementById? The ID of a server control changes based on what controls it's inside of. You need to get the changed ID to use:
var tab = getElementById("<%= myTable.ClientId %>");
or something to that effect.
You should use runat=server attribute if you want to access it at code behind.
But maybe you can generate some javascript code that adds the td/tr to your table at codebehind. and you can register it yo your page to run at startup.
runat="server"
to the table and the tr elements, there is no way the code behind can access them (as this tells the frame work to expose the elements to the server).
What are you actually trying to do? Have you looked at either DataGrid, GridView or simple Repeater control? These allow you to define a table structure and dynamically add controls to it (usually through data binding, although there are ways to add items to thier ItemCollections).
one way that you could do this is to add some inline scripting and a public string variable. in your code behind, make a public class-level variable:
public String myColumns
then in your Page_Load event, create your HTML as a string and save it in your myColumns variable:
protected void Page_Load() {
//do stuff
myColumns = someStringWithTDTagsInIt
}
Then in your .aspx page, do the following:
<table id=maintable"><tr><%=myColumns %></tr></table>
It should render your HTML as you wanted it to in your Page_Load event (or whatever event you want, if you want it to load on a button click or something) without using a "runat=server" tag.
Please note that this is not the recommended way to achieve this (controls are typically more efficient) but this should be a valid solution to the question you posed.
simple example.
just you place the runat="server" then you can access .
html controls are cannot access at server side. if you place runat server then it access in the server.

Resources