I have an ASP.NET(2.0, C#) web application, and I wanted to know how to display all the general errors that could occur on the master page using divs.
For example if there is an 'add new user' page, all the fields that had problems will be shown something like this:
The following Error(s) Occured:
...
...
I am using a master page, so I wanted to know how I could use a div in there, with a label maybe, to display errors from any of the content pages.
Thank you.
I would recommend using a Panel.
<asp:Panel runat="server" id="pnlErrors" Visible="false">
The following Errors(s) Occurred:
<asp:BulletedList id="lstErrors" runat="server">
</asp:BulletedList>
</asp:Panel>
Then just add the errors to lstErrors programmatically if they occur and set the visibility to true.
EDIT: I originally didn't read the part about the Master page. One issue you're probably going to run into is finding that control from your content page. Here is one way you can do so:
BulletedList lstReference = (BulletedList) this.Master.FindControl("lstErrors");
lstReference.Items.Add("Error occured contacting database.");
lstReference.Items.Add("Error occured processing payment.");
Panel panReference = (Panel) this.Master.FindControl("pnlErrors");
panReference.Visible = true;
If you use in the built ValidationSummary control, you don't have to do anything other than put on on the Master page:
<div class="error">
<asp:ValidationSummary ID="vldSummaryMaster" runat="server" />
</div>
This of course assumes that you are using the built in validation controls in order to capture errors.
Related
I have created a web application - form, along with backend SQL insertions and queries.
Currently I am displaying all user error messages:-
<div style="padding: 1em;">
<asp:DataList ID="dtlERA_Errors" runat="server" Visible="false">
<ItemTemplate>
-
<asp:Label ID="Label4" runat="server" Text='<%#Eval("Description") %>'></asp:Label></ItemTemplate>
</asp:DataList>
</div>
this div is at the bottom of my aspx page and it lists all errors in order.
However my goal is to create custom error messages for each element, displayed next to the elements (say a textbox) with a * and then the custom error message. I have seen a lot of modern web forms apply this technique.
Is this ajax/jquery based.... Can anyone provide me with some literature or a name (library) that I should look into? I would appreciate this.
You should read up on asp.net validation controls
http://msdn.microsoft.com/en-us/library/aa479013.aspx
For jQuery, I recommend the jQuery Validation Plugin.
For ASP.NET AJAX, I recommend the ValidatorCallout.
On our ASP.NET/C# web application, we are handling error/notifications with a div in our Master page that is set to Visible="false". What we are doing is, from our aspx pages when we want to create a notification message using the following code:
public static void DisplayNotificationMessage(MasterPage Master, string message)
{
if (Master.FindControl("divmsgpanel") != null)
{
Master.FindControl("divmsgpanel").Visible = true;
}
TextBox thetxtbox = (TextBox)Master.FindControl("txtboxmsgcontents");
if (thetxtbox != null)
{
thetxtbox.Text = message;
}
}
And the HTML in the Master page:
<%--start msgpanel--%>
<div id="divmsgpanel" runat="server" visible="false">
<div id="msgpanelheader">
</div>
<div id="divmsgpanelmainstage">
<asp:TextBox ID="txtboxmsgcontents" runat="server" TextMode="MultiLine"
Width="249px" SkinId="messageboxcontents"></asp:TextBox>
<asp:Label ID="lblmsgcontents" runat="server" Text="Label"></asp:Label>
</div>
<div id="msgpanelfooter">
<asp:Button ID="btnmsgcloser" runat="server" Text="Close"
onclick="btnmsgcloser_Click" />
</div>
</div><%--end msgpanel--%>
This is working quite well so far --- it offers us a centralized way of handling error messages for all pages, while allowing us to easily style the message box to the style of the overall site. We can just call this DisplayNotifcationMessage method from any page in one line. The div contains a "Close" button as well, which again makes it Visible="false".
The only problem we are having (or at least, the only problem we are aware of, anyway!) is with forms submission. On our "Add Employees" page, for example, after you have successfully added an employee, you get a nice notification message using the above method, which says "Employee successfully added." The problem is, is that after you hit the message box's "Close" button, you can press the browsers back button to go Back to the page's previous state (which had the message box's Visible attribute set to true).
I found some guides that detail "tricks" to "disable" the back button (such as A Thorough Examination of "Disabling The Back Button". Ultimately though, these "tricks" are not real solutions to the problem, but rather workarounds that aren't guaranteed.
Bottom line questions:
1) Is this method of having a centralized message box hidden in the Master page a viable method of handling error/notifications messages? Are there any other potential pitfalls that we just haven't noticed yet?
2) How can we handle the "Back" button dilemma?
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
This is the scenario:
I have a Master Page,
In the Master Page I have a user control
In the User Control there is a code that looks like this
<div>
<a id="p1" runat="server">
<asp:Image ID="Img" runat="server" ImageAlign="AbsMiddle" Style="position:relative;
bottom: 0px;" /></a> -<a id="p2" runat="server">
<asp:Label ID="lblRate" runat="server"></asp:Label>
Rates</a>
</div>
Now, when the user clicks on "Rates" it should focus on an item that is another User Control back in the Master Page.
How can I do this?, this is existing code and can not modify the current structure but what is currently happening is that when "Rates" is clicked it postsback with a parameter goto=rates but doesnt do anything.. I need to make so that when it is clicked it focuses on another UserControl in the Master Page. (but this "Rates" link is User Control too)
I tried in my Master Page the following
if(!Page.Postback)
{
if(Request.Params["goto"] == "rates"){
uControl.Focus();
}
}
No luck, please help :(
So to recap:
MasterPage has UserControl -> UserControl has a link that postsback with parameter goto=rates -> back in the MasterPage I need to focus the screen on another UserControl.
I tried to be as clear as I could
Thank you
not all controls supports focus like this. for your user control, you may need build javascript to setup focus.
Looks like you have a not opperator where you should. You should change if(!Page.Postback) to if(Page.Postback).
I am working on an asp.net web application where I have predefined panel in my project with
CSS. Now i want to create another panel with same design and CSS at run time at multiple times. I have a button control when i will click that button it will add another panel.
Please help me to add another panel with same criteria.
If it is something that you plan on reusing, I'd suggest you utilize a user control for this. You can them simply add a new instance of the control on your page.
A few things worth looking into:
https://web.archive.org/web/20210707024005/http://aspnet.4guysfromrolla.com/articles/081402-1.aspx
http://aspalliance.com/565
http://msdn.microsoft.com/en-us/library/c0az2h86.aspx
If you wanted to accomplish this with a postback to the page, add this to your event...
//MyControl = Custom User Control
var myControl = (MyControl) Page.LoadControl("MyControl.ascx");
this.ControlContainer.Controls.Add(myControl);
Like RSolberg said, you could write a User Control and add it multiple times:
<my:UserControl id="MyControl1" runat="server" />
<my:UserControl id="MyControl2" runat="server" />
<my:UserControl id="MyControl3" runat="server" />
Of course, your User Control can be as simple or as complex as you like, thus having repeated functionality on your page.
However, depending on your exact needs you might want to consider something like an ASP.NET Repeater, or ListView, or DataGrid control. With something like a Repeater, you can bind data to it, and have that data be displayed in a list/grid, that has a common look and feel. You can give your Repeater a HTML/CSS template for the header, items, and footer sections too to make it look consistent and professional.
<asp:Repeater id="MyRepeater" runat="server">
<HeaderTemplate>
<h1>Products</h1>
</HeaderTemplate>
<ItemTemplate>
<p>
Product name: <%# Eval("ProductName") %>
</p>
</ItemTemplate>
</asp:Repeater>
and in your code just do this:
MyRepeater.DataSource = products;
MyRepeater.DataBind();
There are many ways of doing what you're asking in ASP.NET - be a bit more specific and we'll be able to give you more specifc help.
Couldn't you just create a new panel in the code behind on the button's "On_Click" event? That would be my suggestion. You may need to have a placeholder to add the panel into something so it appears on the page.