Author: Victor Chen | Date: 16.4.2008 | Category:
C#,
Javascript
It is natural for web users to hit the enter key to submit a form or otherwise activate an action. There have been several cases where the form does not submit on the enter key. The below example works.
<body>
<form id=”form1″ runat=”server”>
<div>
<asp:TextBox ID=”txtFirstName” runat=”server”></asp:TextBox>
<asp:Button ID=”Button1″ runat=”server” Text=”Button” onclick=”Button1_Click” />
<asp:Label ID=”lblOutput” runat=”server” Text=”"></asp:Label>
</div>
</form>
</body>
The Button1_Click action simply copies the input text from txtFirstName into the label lblOutput. Because this simple example works, one would expect this to work in every case. That is not the case. If hitting the enter key does not submit the form, try one of the following below:
- Try putting an invisible HTML Input (Submit) button. Though this has worked for others, it did not work for my situation.
<input type=”text” style=”display:none”/>
- A second option to try is to use a onKeyDown event. This second option will append a moderately long javascript script to check for the enter key on each button press. If the button is pressed, it activates the form submit. To use this, place this script in the head. Finally, on the last input textbox, use the following:
onKeyPress=”return submitFormWithEnter(this,event)”
The script is below goes in the within html head tags:
<script type=”text/javascript”>
function submitFormWithEnter(myfield,e)
{
var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
else return true;
if (keycode == 13)
{
myfield.form.submit();
return false;
}
else
return true;
}
</script>
- This last situation, which worked best for me also checks for the enter key using javascript, but instead of simply activating the form’s action, the function to be run is directly specified. Below, EmailTextBox is the last input TextBox element. Next, replace LoginButton with the ASP Submit Buttton. This script needs to go in the Page_Load section of the codebehind.
EmailTextBox.Attributes.Add(”onKeyPress”, “javascript:if (event.keyCode == 13) __doPostBack(’” + LoginButton.UniqueID + “‘,”)”);
Author: Victor Chen | Date: 10.4.2008 | Category:
C#
Defining the @ symbol prior to the assigning a string value will prevent the need for doubling the backslash (\) in C#. The at symbol (@) simply ignores escape characters. This is useful when accessing a directory local to the server. For example, you may want to store the local directory path in a string. Without using the at symbol (@), it would look similar to the following:
string directoryPath = “C:\\uploadedFiles\\pdfs”;
Note that in the above example, each backslash is needs to be doubled. However, with the at symbol (@), it is both easier to read and type because we can keep the original single backslash format.
string directoryPath = @“C:\uploadedFiles\pdfs”;
Just another trick in C# that may not be blatantly obvious, but very useful. Note that using the at symbol (@) can can be used sparing as in the demonstrated example below:
string directoryPath = “C:\\uploadedFiles” + @“\pdfs”;
In the example above, the at symbol (@) was not used in the first half, but was used in the second half. Not sure why you would use this third example in this case, but just remember that it is an option.
Author: Victor Chen | Date: 8.4.2008 | Category:
C#
Web-based applications involve pages connected by links. Therefore, verifying the authenticity of links becomes a crucial factor. For example, one situation may involve verifyng a link before storing it into a database. Pass to the function a url as a string and it will return true (the link works) or false (the link did not work).
public bool checkUrlLink(string url)
{
HttpWebRequest request = HttpWebRequest)WebRequest.Create(url);
request.Proxy = null;
try
{
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
return true;
}
catch
{
return false;
}
}
This code snippet uses HttpWebRequest and HttpWebResponse functions. Therefore, the snippet above requires the following using statement:
using System.Net;
Also note that this code snippet will also check wheather a url is properly formatted. If the format is not correct, it will return false (simply because the url did not receive a response).