Thursday, April 30, 2009

event.keycode problem Firefox

Solution for event.keycode problem for Firefox and Chrome


I was trying to identify which key is pressed by user  so that I can perform operation accordingly.  keyup event works perfectly with IE, but it was not behaving as expected for Firefox and Chrome.

Following is solution i came across to solve event.keycode problem for firefox (Fix to identify which key is pressed by user on IE, Firefox and Chrome)

function WhichKeyPress(e) {
 if (!e) {
  //if the browser did not pass the event 
  //information to the function, 
  //we will have to obtain it from the 
  //event register
  if (window.event) {
       //Internet Explorer
        e = window.event;
     } else {
       //total failure, we have no 
      //way of referencing the event
       return;
     }
   }
   if (typeof (e.keyCode) == 'number') {
      //DOM
      e = e.keyCode;
    } else if (typeof (e.which) == 'number') {
      //NS 4 compatible
      e = e.which;
    } else if (typeof (e.charCode) == 'number') {
     //also NS 6+, Mozilla 0.9+
      e = e.charCode;
    } else {
      //total failure, we have no way of obtaining the key code
      return;
    }
}

I have called the above function in following manner.
<body onkeyup="WhichKeyPress();">

click Event Problem Firefox Solution

click Event in Firefox and Chrome doesn't work as expected, while it works perfectly with IE.


I have a following Javascript code which was not working
document.getElementById('hlHome').click()
Where "hlHome" is ID of Home Link

Example: I have defined link like following 
<a id="hlHome" runat="server"></a>

Following piece of code was working perfect with IE, but Firefox and Chrome didn't recognise this event.

Solution for Click Event Problem for Firefox, Chrome.
Wherever you are using click event as shown above replace that line with following.
window.location.href = document.getElementById('hlHome').href;

Wednesday, April 22, 2009

Textbox Set Focus on Load while using AJAX Controls on Page

Textbox Set Focus on Load while using AJAX Controls on Page


Whenever you are using AJAX Controls on Page, always remember that
normal way of making set Focus to textbox controls won't work.

Solution to this Problem:
protected void Page_Load(object sender, EventArgs e)
{
AjaxControlToolkit.Utility.SetFocusOnLoad(txtEmailId);
}
Where, txtEmailId is control name.

In every other case where you are not using AJAX Control on Page load and your
.Net Application is in VS.Net 2005 or above version, you can try using following

protected void Page_Load(object sender, EventArgs e)
{

this.Form.DefaultFocus = this.txtEmailId.ClientID;
}

Note: There are various techniques i have discuss most commonly used
technique to set focus for textbox control.

Saturday, April 18, 2009

Twitter Posting from Asp.net Website - Twitterizer Open Source Tutorial

Please refer to new way of posting tweet using Twitter Api, following is old way of posting twitter message and this method is no more supported by twitter.

http://dotnetguts.blogspot.com/2010/05/tweet-posting-from-aspnet-using-oauth.html

Before started discussing about Twitter Programming.  Following is small description about Twitter.  "Twitter is a service for friends, family, and co–workers to communicate and stay connected through the exchange of quick, frequent answers to one simple question"  As Twitter is gaining more and more popularity, it is necessary for web master to put all the websites update on Twitter to attract more audience. 

This post will explain, How to post message from your asp.net website to Twitter.

To demonstrate this, I would be taking help of Open Source Project which easy, fast and efficient to use.  

Open Source Project for Twitter Programming in .NetTwitterizer


Practical Example for Twitter Programming in .Net


Step2: Create New Asp.net Website. 

Step 3: Arrange the Web Controls as shown in Figure.

Step 4: Add Twitterizer Reference.
Right Click Website in Solution Explorer, and select "Add Reference" from Popup dialog.
Add the Twitterizer.Framework.dll which you have downloaded in Step1.

Step 5: Write Code.
Add Namespace: using Twitterizer.Framework;

Add following code on Shout button click event.
protected void btnShout_Click(object sender, EventArgs e)
{   
string TwitterUserName = "twitasp";
string TwitterPassword = "mypassword";
string TwitterMessage = txtShout.Text;

if (TwitterUserName != string.Empty && TwitterPassword != string.Empty)
{
try
{

Twitter twitter = new Twitter(TwitterUserName,TwitterPassword);

string TwitterMsg = txtShout.Text;
if (txtShout.Text.Length > 120)
{
TwitterMsg = TwitterMsg.Substring(0, 130) + "... For more update logon to DailyFreeCode.com";
}
else
{
TwitterMsg = TwitterMsg + "... For more update logon to DailyFreeCode.com";
}
twitter.Status.Update(TwitterMsg);
lblTwitMsg.Text = "Your have shout successfully on http://twitter.com/" + TwitterUserName;

}
catch (Exception ex)
{
Response.Write("<b>" + ex.Message + "</b><br>" + ex.StackTrace);
}
}
}

Lets Post one Message from Asp.net Website Developed.
Type your Message in Textbox and Press Shout button, you can then check your Twitter website to check whether update is applied or not.


Now Press "Shout on Twitter!" button


Now check the Twitter Website for Profile we have Shout on.
http://twitter.com/twitasp


Helpful links for Twitter Programming in .Net
Twitter API Documentation:http://apiwiki.twitter.com/
Twitter Open Source Example: http://code.google.com/p/twitterizer/
Online Discussion Group for Twitterizer: http://groups.google.com/group/twitterizer/


Most Recent Post

Community Updates

Subscribe Blog via Email

Enter your email address:



Disclaimers:We have tried hard to provide accurate information, as a user, you agree that you bear sole responsibility for your own decisions to use any programs, documents, source code, tips, articles or any other information provided on this Blog.
Page copy protected against web site content infringement by Copyscape