Statement cannot appear within a method body. End of method assumed
<script language=”vbscript” runat=”server”></script>
–
Happy Programming
Text Box Enter Key in ASP.NET
One of the common requests in ASP.NET is to submit a form when visitor hits an Enter key. That could be a case if, for example you want to make Login Screen. It is expected that user just hit enter when he insert a user name and password instead to of forcing him to use a mouse to click login button. If you want to make search function on your web site, it is frequently required to give a possibility to hit enter after you insert a search terms instead of mouse click on a Search button.
In HTML or classic ASP pages is not hard to submit forms using the enter key on keyboard. Programmer use a to make a default button. If web site visitor click on that button or press enter key, the form will be submited.
Of course, you can have a more than one form on your page and individual submit button for every form.
You don’t want to submit a form with Enter key?
Rarely, you will need to disable an Enter key and avoid to submit form. If you want to prevent it completely, you need to use OnKeyDown handler on tag of your page. The javascript code should be:
if (window.event.keyCode == 13)
{
event.returnValue=false;
event.cancel = true;
}
Common ASP.NET problems with Enter key
If you try to use Enter key in ASP.NET, according to your browser’s type, you can get really weird results. For example, try to place one ASP.NET textbox and a button to the web form. Write a code on a OnClick event of a button. That could be something simple, like:
Response.Write(“The button was clicked!”);
Now start debugging and write something to textbox. If you press enter while focus is on textbox, form will submit, but your code for button’s click event will not be executed.
Stop the debuging and place one more simple HTML textbox to the form. You will not write anything in this textbox, so you can even make it invisible. Just place it somewhere inside of your form tag.
Start debugging again. You cannot see the second textbox, and everything looks like before. Try again to write something in first textbox. If you press enter now, form will submit, and your code for button’s click event will now be executed. This is extremely different behavior, and you did nothing except you placed one invisible textbox on web form. :)
Maybe it is not best practice, but placing invisible textbox could be simple solution for you if you have only one button on your web form. But, what if you have a different situation? What if you have a few buttons with only one textbox, or more than one text box with only one button, or many text boxes and many buttons with different code for each button, and all that on one form?
Different browsers have a different behavior in these cases. In case that you have more buttons, only first button will be “clicked” every time. So, we need some other approach to get an universal solution.
Enter Key in ASP.NET
One of the common requests in ASP.NET is to submit a form when visitor hits an Enter key. That could be a case if, for example you want to make Login Screen. It is expected that user just hit enter when he insert a user name and password instead to of forcing him to use a mouse to click login button. If you want to make search function on your web site, it is frequently required to give a possibility to hit enter after you insert a search terms instead of mouse click on a Search button.
In HTML or classic ASP pages is not hard to submit forms using the enter key on keyboard. Programmer use a to make a default button. If web site visitor click on that button or press enter key, the form will be submited.
Of course, you can have a more than one form on your page and individual submit button for every form.
You don’t want to submit a form with Enter key?
Rarely, you will need to disable an Enter key and avoid to submit form. If you want to prevent it completely, you need to use OnKeyDown handler on tag of your page. The javascript code should be:
if (window.event.keyCode == 13)
{
event.returnValue=false;
event.cancel = true;
}
Common ASP.NET problems with Enter key
If you try to use Enter key in ASP.NET, according to your browser’s type, you can get really weird results. For example, try to place one ASP.NET textbox and a button to the web form. Write a code on a OnClick event of a button. That could be something simple, like:
Response.Write(“The button was clicked!”);
Now start debugging and write something to textbox. If you press enter while focus is on textbox, form will submit, but your code for button’s click event will not be executed.
Stop the debuging and place one more simple HTML textbox to the form. You will not write anything in this textbox, so you can even make it invisible. Just place it somewhere inside of your form tag.
Start debugging again. You cannot see the second textbox, and everything looks like before. Try again to write something in first textbox. If you press enter now, form will submit, and your code for button’s click event will now be executed. This is extremely different behavior, and you did nothing except you placed one invisible textbox on web form. :)
Maybe it is not best practice, but placing invisible textbox could be simple solution for you if you have only one button on your web form. But, what if you have a different situation? What if you have a few buttons with only one textbox, or more than one text box with only one button, or many text boxes and many buttons with different code for each button, and all that on one form?
Different browsers have a different behavior in these cases. In case that you have more buttons, only first button will be “clicked” every time. So, we need some other approach to get an universal solution.
How to make a default button in ASP.NET
We need to specify exactly which button will be “clicked” when visitor press Enter key, according to which textbox currently has a focus. The solution could be to add onkeydown attribute to textbox control with this code:
TextBox1.Attributes.Add(“onkeydown”, “if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {document.getElementById(‘”+Button1.UniqueID+”‘).click();return false;}} else {return true}; “);
This line of code will cause that button Button1 will be “clicked” when visitors press Enter key and cursor is placed in TextBox1 textbox. On this way you can “connect” as many text boxes and buttons as you want.
Default buttons in ASP.NET 2.0 and ASP.NET 3.5
ASP.NET 2.0 makes this problems easier and introduce a concept of a “default button”. New defaultbutton attribute can be used with or control. What button will be “clicked” depends of where actually cursor is and what button is chosen as a default button for form or a panel.
———
In asp.net
We can use the Panel/Form Default Button Property to make the button to default press when we press the enter key..
For Panel in asp.net
<asp:Panel ID=”Panel1″ runat=”server” DefaultButton=”">
<asp:TextBox ID=”TextBox1″ runat=”server”></asp:TextBox>
<asp:button ID=”btnDefault” runat=”server” Text=”Default Button Panel” OnClientClick=”btnDefault_Click” />
</asp:Panel>
for Form tag in asp.net
<form id=”form1″ runat=”server” defaultbutton=”btnDefault”>
<asp:TextBox ID=”TextBox1″ runat=”server”></asp:TextBox>
<asp:button ID=”btnDefault” runat=”server” Text=”Default Button Form” OnClientClick=”btnDefault_Click” />
</form>
After making the default button, press enter key on textbox you will notice that without pressing Button click events, btnDefault’s btnDefault_Click is fired.
—
Enjoy Programming
URL rewriting in asp.net
what is URL rewriting ?
URL rewriting is the process of intercepting an incoming Web request and redirecting the request to a different resource. When performing URL rewriting, typically the URL being requested is checked and, based on its value, the request is redirected to a different URL.
http://msdn.microsoft.com/en-us/library/ms972974.aspx
why URL rewriting?
1) Make User Friendly and Secure URL
Example : In your application, you create a page which display category information and it’s relevant category. At that time we normally passing a value via query string,
http://Site.com/category.aspx?categoryid=1. Any user play with URL, you can also do the URL Encryption. but sometime case is complex at that time it is not possible.
2) Make SEO Friendly URL
3) Usability & Maintainability
Use Of Browser file?
ASP.NET uses .browser files to determine the capabilities of the browser, and how to render markup to that browser.
Browser files are used to reduce the load of the page made by the view state by storing it in a server side session variable.
In particular Rewriting Module: Handling Post back with URL Rewriting.
what is ControlAdapter?
for more detail please check :
http://msdn.microsoft.com/en-us/library/system.web.ui.adapters.controladapter.aspx
Create demo project for URL Rewriting:
Step 1:
Create Web application with (Asp.net 2.0 with c#)
Step 2:
Put two button in the form (default.aspx)
Put Below code for Button1_Click Event :
Response.Redirect(“Default.aspx?id=1″);
Put Below code for Button2 _Click Event:
Response.Redirect(“/urlrewriting/FirstSection/Default.html”);
Step 3: Run Application
once you press Button2 it give Error page.
Step 4:
Create Class FormRewriterControlAdapter.cs
1: using System;
2: using System.Data;
3: using System.Configuration;
4: using System.Web;
5: using System.Web.Security;
6: using System.Web.UI;
7: using System.Web.UI.WebControls;
8: using System.Web.UI.WebControls.WebParts;
9: using System.Web.UI.HtmlControls;
10:
11: public class FormRewriterControlAdapter : System.Web.UI.Adapters.ControlAdapter
12: {
13: protected override void Render(HtmlTextWriter writer)
14: {
15: base.Render(new RewriteFormHtmlTextWriter(writer));
16: }
17: }
18:
19: public class RewriteFormHtmlTextWriter : HtmlTextWriter
20: {
21: public RewriteFormHtmlTextWriter(HtmlTextWriter writer)
22: : base(writer)
23: {
24: this.InnerWriter = writer.InnerWriter;
25: }
26:
27: public RewriteFormHtmlTextWriter(System.IO.TextWriter writer)
28: : base(writer)
29: {
30: base.InnerWriter = writer;
31: }
32:
33: public override void WriteAttribute(string name, string value, bool fEncode)
34: {
35: if (name == “action”)
36: {
37: HttpContext Context = HttpContext.Current;
38: if (Context.Items["ActionAlreadyWritten"] == null)
39: {
40: value = Context.Request.RawUrl;
41: Context.Items["ActionAlreadyWritten"] = true;
42: }
43: }
44: base.WriteAttribute(name, value, fEncode);
45: }
46: }
Create Class MyHttpHandler.cs
1: using System;
2: using System.Data;
3: using System.Configuration;
4: using System.Web;
5: using System.Web.Security;
6: using System.Web.UI;
7: using System.Web.UI.WebControls;
8: using System.Web.UI.WebControls.WebParts;
9: using System.Web.UI.HtmlControls;
10:
11: public class MyHttpHandler : IHttpModule
12:
13: {
14: public MyHttpHandler()
15: {
16: //
17: // TODO: Add constructor logic here
18: //
19: }
20:
21: #region IHttpModule Members
22:
23: public void Dispose()
24: {
25:
26: }
27:
28: public void Init(HttpApplication app)
29: {
30: app.BeginRequest += new EventHandler(Application_BeginRequest);
31: }
32:
33: private void Application_BeginRequest(object sender, EventArgs e)
34: {
35: System.Web.HttpApplication app = (System.Web.HttpApplication)sender;
36: string requestedUrl = app.Request.Path.ToLower();
37: string realUrl = GetRealUrl(requestedUrl);
38: if (!String.IsNullOrEmpty(realUrl))
39: app.Context.RewritePath(realUrl, false);
40: }
41:
42: private string GetRealUrl(string requestedUrl)
43: {
44: // Implement your own logic here
45: MyURL obj = new MyURL();
46: return obj.GetRealPath(requestedUrl);
47: }
48: #endregion
49: }
create Class MyURL.cs
1:
2: using System;
3: using System.Data;
4: using System.Configuration;
5: using System.Web;
6: using System.Web.Security;
7: using System.Web.UI;
8: using System.Web.UI.WebControls;
9: using System.Web.UI.WebControls.WebParts;
10: using System.Web.UI.HtmlControls;
11: using System.Collections.Generic;
12:
13: /// <summary>
14: /// Summary description for MyURL
15: /// </summary>
16: public class MyURL
17: {
18: public MyURL()
19: {
20: //
21: // TODO: Add constructor logic here
22: //
23: }
24:
25: public string GetRealPath(string requestedUrl)
26: {
27: string path = “”;
28: Dictionary<string, string> paths = GetPathsFromDatabase();
29: if (paths.ContainsKey(requestedUrl))
30: paths.TryGetValue(requestedUrl, out path);
31: return path;
32: }
33:
34: private static Dictionary<string, string> GetPathsFromDatabase()
35: {
36: Dictionary<string, string> paths = new Dictionary<string, string>();
37: paths.Add(“/urlrewriting/FirstSection/Default.html”.ToLower(), “/urlrewriting/Default.aspx?SectionID=1″);
38: paths.Add(“/urlrewriting/SecondSection/Default.aspx”.ToLower(), “/urlrewriting/Default.aspx?SectionID=2″);
39: paths.Add(“/urlrewriting/FirstSection/Page1.aspx”.ToLower(), “/urlrewriting/Default.aspx?SectionID=1&Item=1″);
40: paths.Add(“/urlrewriting/FirstSection/Page2.aspx”.ToLower(), “/urlrewriting/Default.aspx?SectionID=1&Item=2″);
41: paths.Add(“/urlrewriting/SecondSection/Page1.aspx”.ToLower(), “/urlrewriting/Default.aspx?SectionID=2&Item=1″);
42: paths.Add(“/urlrewriting/SecondSection/SubSection/AnotherOne/Page5.aspx”.ToLower(), “/urlrewriting/Default.aspx?SectionID=2&Item=5″);
43: paths.Add(“/urlrewriting/Default.aspx”.ToLower(), “/urlrewriting/Default.aspx”);
44: return paths;
45: }
46: }
Register Http Handler in to Web.Config
1: <system.web>
2: <httpModules>
3: <add name=”MyHttpHandler” type=”MyHttpHandler”/>
.Browser File
Add New .browser file from Add New Item.
1: <browsers>
2: <browser id=”NewBrowser” parentID=”Mozilla”>
3: <identification>
4: <userAgent match=”Unique User Agent Regular Expression” />
5: </identification>
6:
7: <capture>
8: <userAgent match=”NewBrowser (?’version’\d+\.\d+)” />
9: </capture>
10:
11: <capabilities>
12: <capability name=”browser” value=”My New Browser” />
13: <capability name=”version” value=”${version}” />
14: </capabilities>
15: </browser>
16:
17: <browser refID=”Mozilla”>
18: <capabilities>
19: <capability name=”xml” value=”true” />
20: </capabilities>
21: </browser>
22: <!–FormRewrite Control Adapter–>
23: <browser refID=”Default”>
24: <controlAdapters>
25: <adapter controlType=”System.Web.UI.HtmlControls.HtmlForm”
26: adapterType=”FormRewriterControlAdapter” />
27: </controlAdapters>
28: </browser>
29: </browsers>
step 5: Run Application
Now Press Button 2:
http://localhost:2696/urlrewriting/FirstSection/Default.html and it is run.
Now made some change in to system. Remove browser file and click button2, after that made postback at that time above URL is change to original URL.
Reference Site :
For Detail Example :
http://weblogs.asp.net/scottgu/archive/2007/02/26/tip-trick-url-rewriting-with-asp-net.aspx
Hope this help you.
Thnx
What’s the difference between Div and span tags?
<span> and <div> tags both allow a Web designer to style text and add other formatting attributes to their Web page. They are not interchangeable tags, though. <div> tags are block-level elements, whereas <span> tags are not. This article explains this, and other differences, between <span> and <div> tags.
Both <span> and <div> tags allow you to apply CSS styles
<span> and <div> tags are both fine for applying inline CSS formatting. Consider the following code:
<div style=”color:#000000;font-weight:bold;font-size:14px”>Here’s some text in between div tags</div>
The output of this code is the same as:
<span style=”color:#000000;font-weight:bold;font-size:14px”>Here’s some text in between span tags</span>
<div> tags are block elements that allow you to position elements contained within them
<div> tags are block elements. This means they can “hold” other HTML elements, and they can be positioned anywhere on the screen. For this reason, <div> tags can be used for replacing table-based Web designs.
<span> tags are NOT block elements
Unlike <div> tags, <span> tags cannot hold other HTML tags. They should only be used for inline styling of text. Consider the following code:
<span style=”font-size:14px”>
<p>This is some text sized at 14 pixels.</p>
</span>
This code will probably render OK in most browsers, but it is actually incorrect. Remember, since the <span> tag is not a block-level element, it cannot contain other HTML tags. The correct way to write this code would be:
<p><span style=”font-size:14px”>
This is some text sized at 14 pixels.
</span></p>
While this code is correct, it can be written more efficiently by removing the <span> tag altogether:
<p style=”font-size:14px”>
This is some text sized at 14 pixels.
</p>
<div> tags create line breaks, <span> tags do not
Since <div> tags are block elements, they create line breaks. This is the same effect as a <p> tag. <span> tags do not create line breaks, which makes them perfect for styling text in the middle of a sentence. Here’s an example of a <span> tag used to style text in the middle of a sentence. The same code using <div> instead of <span> would produce undesired results.
<p>It’s snowing in the Northeast today. Some towns received as much as <span style=”font-size:18px;font-style:italic;font-weight:bold;”>6 inches</span> of snow.</p>
Summary of <span> vs. <div> tags
<span> tags are useful if you need to apply specific formatting styles to text in the middle of a sentence, or in one place on your Web page. They are far less powerful than <div> tags, though, as a <div> will allow you to create blocks of content (like table-based Web designs) and position elements on the screen.
-
Archives
- April 2011 (1)
- May 2010 (1)
- April 2010 (1)
- February 2010 (3)
- January 2010 (5)
- December 2009 (6)
- November 2009 (13)
- October 2009 (10)
- September 2009 (6)
- August 2009 (7)
- July 2009 (7)
- June 2009 (3)
-
Categories
-
RSS
Entries RSS
Comments RSS