Confirm() Function in javascript
A CONFIRM box is used when you are trying to get a YES / NO answer from the visitor. The OK button acts as the YES response the CANCEL button acts as the NO response.
To get the CONFIRM actions to perform, we have to use a VARIABLE situation to control it. That means, we have to use a bit of algebra to temporarilly assign a value to the visitor's answer choice. After the value (answer) is determined, the script then carries out the set of instructions accordingly. This is usually done with an IF...ELSE statement. IF the answer is true, do this. ELSE the answer is false, so do this instead.
Examples
- JavaScript Confirm() - example of using a JavaScript confirm() method. Returns a boolean true or false.
Ex: <a href="javascript: confirm('When finished, click OK'); void('')">JavaScript Confirm()</a> - Results Example - Showing how to capture the results. Returns a boolean true or false.
Ex: <a href="javascript: alert('Your choice was: ' + confirm('When finished, click OK') ); void('')">Results Example</a> - If Example - Showing how to capture the results. Returns a boolean true or false.
Ex: <a href="javascript: if (confirm('Continue?')) { alert('You chose true') } else { alert('You chose false.') }; void('')"> - Confirm OK, then goto URL (uses onclick()) - Confirm, then next web page - If you press 'OK', then you will launch another web page.
Ex: <a href="ex_confirm_continuepressed.htm" onclick = "if (! confirm('Continue?')) return false;"> - Confirm OK, then popup a new window (uses onclick()) - Confirm, then next web page - If you press 'OK', then you will launch another web page.
Ex: <a href="ex_confirm_continuepressed.htm" onclick = "if (! confirm('Continue?')) return false;"> - Confirm OK, then goto a URL (uses window.location.href) - Confirm, then next web page - If you press 'OK', then you will launch another web page.
Ex: <a href="javascript: if (confirm('Continue?')) { window.location.href='ex_confirm_continuepressed.htm' } else { void('') }; "> - Confirm OK, then popup a new window (uses window.open()) - Confirm, then next web page - If you press 'OK', then you will launch another web page.
Ex: <a href="javascript: if (confirm('Continue?')) { window.open('ex_confirm_continuepressed.htm'); void('') } else { void('') }; "> - The normal html would look like:
blnAnswer = confirm('When finished, click OK')
Use a javascript confirm box to ask the user if they want to delete
Use a javascript confirm box to ask the user if they want to delete
<script>
function confirmDelete(delUrl) {
if (confirm("Are you sure you want to delete")) {
document.location = delUrl;
}
}
</script>
<a href="javascript:confirmDelete('delete.page?id=1')">Delete</a>
Another way
<a href="delete.page?id=1" onclick="return confirm('Are you sure you want to delete?')">Delete</a>
Just a smart demo :)
Posted in: programming asp.net | Tags: javascript confirm demoNew feature: Routing in ASP.NET 4
ASP.NET 4 adds built-in support for using routing with Web Forms. Routing lets you configure an application to accept request URLs that do not map to physical files. Instead, you can use routing to define URLs that are meaningful to users and that can help with search-engine optimization (SEO) for your application. For example, the URL for a page that displays product categories in an existing application might look like the following example:
http://website/products.aspx?categoryid=12
By using routing, you can configure the application to accept the following URL to render the same information:
http://website/products/software
Routing has been available starting with ASP.NET 3.5 SP1. However, ASP.NET 4 includes some features that make it easier to use routing, including the following:
· The PageRouteHandler class, which is a simple HTTP handler that you use when you define routes. The class passes data to the page that the request is routed to.
· The new properties HttpRequest.RequestContext and Page.RouteData (which is a proxy for the HttpRequest.RequestContext.RouteData object). These properties make it easier to access information that is passed from the route.
· The following new expression builders, which are defined in System.Web.Compilation.RouteUrlExpressionBuilder and System.Web.Compilation.RouteValueExpressionBuilder:
· RouteUrl, which provides a simple way to create a URL that corresponds to a route URL format within an ASP.NET server control.
· RouteValue, which provides a simple way to extract information from the RouteContext object.
· The RouteParameter class, which makes it easier to pass data contained in a RouteContext object to a query for a data source control (similar to FormParameter).
Routing for Web Forms Pages
The following example shows how to define a Web Forms route by using the new MapPageRoute method of the Route class:
public class Global : System.Web.HttpApplication
{
void Application_Start(object sender, EventArgs e)
{
RouteTable.Routes.MapPageRoute("SearchRoute",
"search/{searchterm}", "~/search.aspx");
RouteTable.Routes.MapPageRoute("UserRoute",
"users/{username}", "~/users.aspx");
}
}
ASP.NET 4 Beta 2 introduces the MapPageRoute method. The following example is equivalent to the SearchRoute definition shown in the previous example, but uses the PageRouteHandler class.
RouteTable.Routes.Add("SearchRoute", new Route("search/{searchterm}",
new PageRouteHandler("~/search.aspx")));
The code in the example maps the route to a physical page (in the first route, to ~/search.aspx). The first route definition also specifies that the parameter named searchterm should be extracted from the URL and passed to the page.
The MapPageRoute method supports the following method overloads:
· MapPageRoute(string routeName, string routeUrl, string physicalFile, bool checkPhysicalUrlAccess)
· MapPageRoute(string routeName, string routeUrl, string physicalFile, bool checkPhysicalUrlAccess, RouteValueDictionary defaults)
· MapPageRoute(string routeName, string routeUrl, string physicalFile, bool checkPhysicalUrlAccess, RouteValueDictionary defaults, RouteValueDictionary constraints)
The checkPhysicalUrlAccess parameter specifies whether the route should check the security permissions for the physical page being routed to (in this case, search.aspx) and the permissions on the incoming URL (in this case, search/{searchterm}). If the value of checkPhysicalUrlAccess is false, only the permissions of the incoming URL will be checked. These permissions are defined in the Web.config file with settings such as the following:
<configuration>
<location path="search.aspx">
<system.web>
<authorization>
<allow roles="admin"/>
<deny users="*"/>
</authorization>
</system.web>
</location>
<location path="search">
<system.web>
<authorization>
<allow users="*"/>
</authorization>
</system.web>
</location>
</configuration>
In the example configuration, access is denied to the physical page search.aspx for all users except those who are in the admin role. When the checkPhysicalUrlAccess parameter is set to true (which is its default value), only admin users are allowed to access the URL /search/{searchterm}, because the physical page search.aspx is restricted to users in that role. If checkPhysicalUrlAccess is set to false and the site is configured as shown in the previous example, all authenticated users are allowed to access the URL /search/{searchterm}.
Reading Routing Information in a Web Forms Page
In the code of the Web Forms physical page, you can access the information that routing has extracted from the URL (or other information that another object has added to the RouteData object) by using two new properties: HttpRequest.RequestContext and Page.RouteData. (Page.RouteData wraps HttpRequest.RequestContext.RouteData.) The following example shows how to use Page.RouteData.
protected void Page_Load(object sender, EventArgs e)
{
string searchterm = Page.RouteData.Values["searchterm"] as string;
label1.Text = searchterm;
}
The code extracts the value that was passed for the searchterm parameter, as defined in the example route earlier. Consider the following request URL:
http://localhost/search/scott/
When this request is made, the word "scott" would be rendered in the search.aspx page.
Accessing Routing Information in Markup
The method described in the previous section shows how to get route data in code in a Web Forms page. You can also use expressions in markup that give you access to the same information. Expression builders have been described as a "hidden gem of ASP.NET" (see the entry Express Yourself With Custom Expression Builders on Phil Haack's blog). It is unfortunate that they are not better known, because expression builders are a powerful and elegant way to work with declarative code.
ASP.NET 4 includes two new expression builders for Web Forms routing. The following example shows how to use them.
<asp:HyperLink ID="HyperLink1" runat="server"
NavigateUrl="<%$RouteUrl:SearchTerm=scott%>">Search for Scott</asp:HyperLink>
In the example, the RouteUrl expression is used to define a URL that is based on a route parameter. This saves you from having to hard-code the complete URL into the markup, and lets you change the URL structure later without requiring any change to this link.
Based on the route defined earlier, this markup generates the following URL:
http://localhost/search/scott
ASP.NET automatically works out the correct route (that is, it generates the correct URL) based on the input parameters. You can also include a route name in the expression, which lets you specify a route to use.
The following example shows how to use the RouteValue expression.
<asp:Label ID="Label1" runat="server" Text="<%$RouteValue:SearchTerm%>" />
When the page that contains this control runs, the value "scott" is displayed in the label.
The RouteValue expression makes it very simple to use route data in markup, and it avoids having to work with the more complex Page.RouteData["x"] syntax in markup.
Using Route Data for Data Source Control Parameters
The RouteParameter class lets you specify route data as a parameter value for queries in a data source control. It works much like FormParameter, as shown in the following example:
<asp:sqldatasource id="SqlDataSource1" runat="server"
connectionstring="<%$ ConnectionStrings:MyNorthwind %>"
selectcommand="SELECT CompanyName,ShipperID FROM Shippers where
CompanyName=@companyname"
<selectparameters>
<asp:routeparameter name="companyname" RouteKey="searchterm" />
</selectparameters>
</asp:sqldatasource>
In this case, the value of the route parameter searchterm will be used for the @companyname parameter in the Select statement.
Posted in: programming asp.net | Tags: asp.net asp.net 4.0 routeurl routecontext routeparameter httprequest.requestcontext page.routedata http routetable.routes.mappagerouteComplex class declaration confusing forms designer?
I have a class declared as:
public class Foo<T> : Panel where T : Control, IFooNode , new()
{
...
}
Could not find type 'FooTestNameSpace.Foo'. Please make sure that the assembly that contains this type is referenced. If this type is a part of your development project, make sure that the project has been successfully built.
Interestingly, I ALSO get a warning that is similar, but includes the generic type I used in my declaration of the variable. Warning is:
Could not find type 'FooTestNameSpace.Foo<FooTestNameSpace.FooNodeType>'. Please make sure that the assembly that contains this type is referenced. If this type is a part of your development project, make sure that the project has been successfully built.
The declaration in my simple Form1 class is:
private FooTestNameSpace.Foo myFoo;
(FooNodeType is really just a subclass of Label that has one auxiliary property not yet being used; it does implement IFooNode).
So my question... With this type of set up, how can I get Foo to be displayed on the Forms designer, or at least get it to acknowledge that Foo is legit? Is there any Designer attribute that can handle this? I don't really care if my Foo control appears as an empty box, as long as it appears at all!
Answer:
class ConcreteFooCtl : FooCtl<FooNodeType>
{
}
class FooCtl<T> : Panel where T : Control, IFooNode , new()
{
}
The problem seems to be that the Forms designer can't handle any generic control. The forms designer can't handle FooCtl, but has no problems with ConcreteFooCtl. Not an ideal solution, but I think it's at least workable. Maybe the next version of VS will prompt the user to specify a generic type when adding a generic control to a form... ;)
Posted in: programming problems and solutions | Tags: foo forms designer declaration complexProper way to dispose a BitmapSource in .NET
How are you supposed to dispose of a BitmapSource ?
// this wont work because BitmapSource doesnt implement IDisposable
using(BitmapSource bitmap = new BitmapImage(new Uri("myimage.png")))
{
}
You do not have to Dispose() a BitmapSource. Unlike some other "image" classes in the Framework, it does not wrap any native resources.
Just let it go out of scope, and the garbage collector will free its memory.
Posted in: programming | Tags: .net gc bitmapsource bitmapimage garbage