Showing posts with label ASP.NET. Show all posts
Showing posts with label ASP.NET. Show all posts

Sunday, March 14, 2010

Remove Empty Records from DataTable

The content of this post has been moved here. Please check this link to view the contents.

Sorry for the inconvenience.

Thanks!

Thursday, September 17, 2009

Avoid numbers in Textbox using ASP.NET AJAX

An old question which was recently asked for an interview.

How can you prevent numbers from being entered into a Textbox, using Microsoft ASP.NET AJAX Framework. Thinking about UpdatePanel’s? Be clever, I have some client-side codes, using the framework itself.

[Client-Side]

   1: <script type="text/javascript">
   2:     function pageLoad() {
   3:         var tbox = $get("Text1");
   4:         $addHandler(tbox, 'keypress', text1_keypress);
   5:     }
   6:  
   7:     function text1_keypress(e) {
   8:         var code = e.charCode;
   9:         if (code >= 48 && code <= 57)
  10:             e.preventDefault();
  11:     }
  12:  
  13:     function pageUnload() {
  14:         var tbox = $get("Text1");
  15:         $removeHandler(tbox, 'keypress', text1_keypress);
  16:     }
  17: </script>

[Markup-Code]

   1: <form id="form1" runat="server">
   2: <asp:ScriptManager ID="ScriptManager1" runat="server">
   3: </asp:ScriptManager>
   4: <div>
   5:     Enter text value:
   6:     <input id="Text1" type="text" />
   7: </div>
   8: </form>

Thanks.

What are Mashups?

It’s been a long time, I’ve been hearing about mashups in the web world. Surprisingly, I came across the same when I’s reading Manning’s ASP.NET AJAX in Action.

A mashup is a web application that consumes content from more than
one external source and aggregates it into a seamless, interactive experience
for the user.

If you ask me a live example, here it is PageFlakes.

Saturday, August 8, 2009

Changing Multiple GridView Row values using RowUpdating event

This is an extended sample of my previous post titled Change GridView Row values using RowUpdating Event. Recently, I got comment from NobleMule asking how he can edit multiple column/cell values.

I have 4 DDL's in a gridview. The first one is updating (mostly thanks to your code) but I can't get the other three to update. I'm a fairly new to C# and have never used the DictionaryEntry before. I naively tried just adding more if's (one for each ddl) within the foreach loop but that didn't work. Any help would be greatly appreciated.

I hope the 4 DDL’s are in 4 different Cells of a GridView. And wants to update the new values selected in those DDL’s into the database. As he’d mentioned my previous post sample is only meant for updating the DDL present in one Cell of your GridView.

I’ll break-up the concept here, so that you can deal with any number of Cells in the GridView. These are the tips to be kept in mind before start coding.

#1. Identify the Row in the GridView for updating, obtained via e.RowIndex

#2. Identify the Cell (column) in the GridView for updating.
     This value will be entered manually. Keep in mind, Cell indexing starts from zero.

#3. Locate the control (DDL here) using FindControl() method and cast it to appropriate type (ie, DDL type here).

These tips help you to find a Control in a particular (row,cell) of a GridView.

Now, the real case. You want to update the DDL in 4 different Cells of a GridView. All you’ve to do is to:
(a) Identify each Cell of GridView
(b) Access the Control (DDL here), from each Cell
(c) Get the Selected Value from DDL
(d) Update with new value

Lets assume that we’ve a Table having cells as:

IdColumn1Column2Column3Column4

And, Column1 having DropDownList1, Column2 having DropDownList2, Column3 having DropDownList3 and Column4 having DropDownList4.

Now, the rough piece of code looks like:

   1:  
   2: protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
   3:     {
   4:         foreach (DictionaryEntry entry in e.NewValues)
   5:         {
   6:             DropDownList ddl;
   7:  
   8:             if (entry.Key.ToString() == "Column1")
   9:             {
  10:                 ddl = (DropDownList)GridView1.Rows[e.RowIndex].Cells[1].FindControl("DropDownList1");
  11:                 e.NewValues[entry.Key] = Server.HtmlEncode(ddl.SelectedItem.Text);
  12:             }
  13:             if (entry.Key.ToString() == "Column2")
  14:             {
  15:                 ddl = (DropDownList)GridView1.Rows[e.RowIndex].Cells[2].FindControl("DropDownList2");
  16:                 e.NewValues[entry.Key] = Server.HtmlEncode(ddl.SelectedItem.Text);
  17:             }
  18:             if (entry.Key.ToString() == "Column3")
  19:             {
  20:                 ddl = (DropDownList)GridView1.Rows[e.RowIndex].Cells[3].FindControl("DropDownList3");
  21:                 e.NewValues[entry.Key] = Server.HtmlEncode(ddl.SelectedItem.Text);
  22:             }
  23:             if (entry.Key.ToString() == "Column4")
  24:             {
  25:                 ddl = (DropDownList)GridView1.Rows[e.RowIndex].Cells[4].FindControl("DropDownList4");
  26:                 e.NewValues[entry.Key] = Server.HtmlEncode(ddl.SelectedItem.Text);
  27:             }
  28:         }
  29:     }

Hope this makes some sense.


Thanks.

Friday, May 8, 2009

No jQuery intellisense in Visual Studio Workaround

If you are using jQuery 1.3.2 in Visual Studio 2008 or Visual Web Developer 2008 (SP1), you may come across scenario where you wont get the jQuery intellisense support, even if you add jquery-1.3.2-vsdoc2.js file to the aspx page.

The workaround is simple. All you’ve to do, is to rename the jquery-1.3.2-vsdoc2.js to jquery-1.3.2-vsdoc.js.

It looks like:

   1: <script src="Scripts/jquery-1.3.2.js" type="text/javascript"></script>
   2: <script src="Scripts/jquery-1.3.2-vsdoc.js" type="text/javascript"></script>

Note: For making VS/VWD to support jQuery intellisense, you’ve to apply a patch from the VS Development team & can be downloaded from here.

Thursday, April 30, 2009

Gravatar using ASP.NET

I’m always attracted with the fancy features in web. This time, I fall in love with Gravatar. Its a global recognition of “YOU”, with the help of an e-mail Id.

If you don't have a Gravatar account, create on here.

The SRC attribute a Gravatar Image has the below mentioned syntax:

http://www.gravatar.com/avatar/[MD5_Hash_Value]?s=[Image_Size]

For loading your Gravatar image, all you’ve to do is to mention the Hash Value of your email id and the size of your Gravatar image.

Before start, drag-drop an Image control, TextBox (for accepting email Id) and Button control.

[Source-View]

   1: <body>
   2:     <form id="form1" runat="server">
   3:     <div>
   4:         <asp:Image ID="Image1" src="" runat="server" /><br />
   5:         <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
   6:         <asp:Button ID="Button1" runat="server" Text="Button" />
   7:     </div>
   8:     </form>
   9: </body>



[Code-View]





   1: // Page Load
   2: protected void Page_Load(object sender, EventArgs e)
   3: {
   4:     // Add SRC attribute to Iage1 at runtime
   5:     Image1.Attributes.Add("src", GetGravatarImageURL(TextBox1.Text.Trim(), 80));
   6: }
   7:  
   8: // Return Gravatar Image URL
   9: public static string GetGravatarImageURL(string emailId, int imgSize)
  10: {
  11:  
  12:     string hash = string.Empty;
  13:     string imageURL = string.Empty;
  14:  
  15:     // Convert emailID to lower-case
  16:     emailId = emailId.ToLower();
  17:  
  18:     hash = FormsAuthentication.HashPasswordForStoringInConfigFile(emailId, "MD5").ToLower();
  19:  
  20:     // build Gravatar Image URL
  21:     imageURL = "http://www.gravatar.com/avatar/" + hash + ".jpg?s=" + imgSize;
  22:  
  23:     return imageURL;
  24: }



Now just run/build(F5) your page. The image will looks like below:



noGravatar_Sample



This is the default Gravatar image.



noGravatar 



Now just, enter your email Id used for creating the Gravatar account. & hit the button.



Hurray you’re done. Just integrate at the comments section of your blog.





 myGravatar





This is my gravatar image



That's it & Thanks.

Saturday, April 25, 2009

Change GridView row values using RowUpdating event


The contents of this post has been moved to here. Please visit the link to view the contents.

Sorry for the inconvenience.

Thanks!

Wednesday, April 1, 2009

Programmatically accessing RSS Feed using RSSToolkit Control

An awesome RSS Feed control (Dmitry’s RSSToolkit) was released years before, I came to knew about ASP.NET.

Last months, I’s spending some time with the control.  I must say, its really cool. One problem I encountered; lack of documentation (or my lack of knowledge), What ever it may. But, it was really nice to write some cool codes using the control.

Ok! In this article, I’ll show small piece of codes demonstrating how to retrieve RSS Feeds from sites programmatically.

Before we start, please download RSSToolkit 2.0 from codeplex, and add the control to your Visual Studio toolbox (?).

Step 1: Drag-drop ‘RssDataSource’ control from your toolbox to the webpage.

Choose RSSDataSource

Now just keep an eye on the source-view. When you drag-drop the RssDataSource, you’ll get a Register tag in the source-view like:

<%@ Register assembly="RssToolkit" namespace="RssToolkit.Web.WebControls" tagprefix="cc1" %>

Step 2: Drag-drop a TextBox for  accepting the feed-url.

Step 3: Drag-drop a Button to trigger fetching the feeds. Double-Click the button to generate the Click event.

Step 4: Drag-drop a Label control to show an error when you enter an invalid feed-url.

[Design-View]

Design-View

[Source-View]

<body>
<form id="form1" runat="server">
<div>
<cc1:RssDataSource ID="RssDataSource1" runat="server">
</cc1:RssDataSource>
<br />
Enter Feed URL:
<asp:TextBox ID="TextBox1" runat="server" Width="300px"></asp:TextBox>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Fetch" />
<br />
<asp:Label ID="Label1" runat="server" Font-Bold="True" ForeColor="#FF3300"></asp:Label>
</div>
</form>
</body>

[Code-View]

using RssToolkit.Rss;

// Fecth RSS
protected void Button1_Click(object sender, EventArgs e)
{
string feedData = string.Empty;
// Accepts Feed URL
RssDataSource1.Url = TextBox1.Text.Trim();

try
{
// Iterate through the RSS feed Items
foreach (RssItem item in RssDataSource1.Rss.Channel.Items)
{
// Feed Author
feedData = item.Author;
// Feed Title
feedData = item.Title;
// Feed Link
feedData = item.Link;
// Feed Description
feedData = item.Description;
// Feed Publish Date
feedData = item.PubDate;

// Print the output
Response.Write(feedData + "<br/><hr/><br/>");
}
}
catch (UriFormatException exp)
{
Label1.Text
= "Invalid Feed URL";
}
}

[Demonstration]
a) copy the url of an RSS feed, say http://feeds2.feedburner.com/DidYouSaynet.

b) Paste it on the TextBox & Hit the button. That's all.

c) See How the feeds are displayed.

Hoping it will help someone.

Note:

a) Do not forget to include the namespace using RSSToolkit.RSS.

b) One of the common question you may encounter is how to fetch feeds with a count limit?. That means you only want to fetch last 3 feeds, 5 feeds, etc. At this point I don't have the answer. Even though the RSSDataSource has a property named MaxItems ( where you can set the feed limit), it was not working when tried programmatically. Only way I believe you can do; is to insert a count value in the foreach statement  & break the loop when you feed-count limit is reached.

c) Next question will be how can I download updated feeds only from a site?. The answer will be my next post.

Also you can find some cool articles of RSSToolkit @ ScottGu’s blog and @ CodeProject, which inspired me to write this.

Thanks for taking time to read the post.

Adding Controls to Visual Studio Toolbox

 

The contents of this post has been moved to http://blogs.cametoofar.com/post/Adding-Controls-to-Visual-Studio-Toolbox.aspx.

Sorry for inconvenience.

Thanks

Friday, March 20, 2009

HOW TO: Get the IP Address of a visitor

Just simple:

Request.ServerVariables(“REMOTE_ADDR”)

Manning’s ASP.NET AJAX

Manning's ASP.NET AJAX

I’m getting some free time, now a days. So I started reading the book ASP.NET AJAX from Manning. On my further post I’ll be posting some good tips/tricks/must-haves from this book.

Thanks.

Thursday, March 19, 2009

Control.ClientID in ASP.NET 4.0

One of the repeated question in ASP.NET forums is “How to access the server-control from client-side?” I’ve wrote an article regarding it. You can find it here.

With the release of ASP.NET 4.0, MSFT development team is introducing an alternate approach for accessing the server-side controlID from client-side. With the release of 4.0, a new property called ClientIDMode has been introduced to control the behavior of the ID displayed in client-side.

The ClientIDMode property has 4  modes and are as follows:

a) Legacy 
In this mode, the ControlID behaves as in framework 2.0, 3.0 and 3.5). That means a textbox in a Master-Page may looks like ctl00_MasterPageBody_ctl01_Textbox1.

b) Inherit 
This is the default value for every control. This mode looks at the controls parent to get its value for ClientID.

c) Static
As the name suggests, it makes the ClientID static. But keep in mind, if you declare 2 controls-id as same; you’ll be popped-out.

d) Predictable.
This models commonly used along with databound controls. The framework traverse the control hierarchy pre-fixing the supplied ID along with its Parent’s ControlID until it reaches a control in the hierarchy whose ClientIDMode is static. For a control placed in a databound control, a suffix with a value that identifies that instance will be added to the supplied ID . The clientIDRow suffix property is used to control the value that will be used as a suffix, e.g: Gridview1_Label1_0.

I know you’ll be eager to see some examples. A more detailed explanations along with the examples can be found at ASP.NET  Weblogs.

Thursday, March 12, 2009

Response.Redirect vs Server.Transfer

The Response.Redirect method sends a message to the requesting client to request a new page. This requires a round trip between the browser and the server, but allows the user to see the new URL in the browser address bar.

Server.Transfer is a quicker approach which simply loads the specified page without the round trip. As a result, the browser’s address bar is not updated.

ASP.NET Postback flow

There is a nice pictorial demonstration of the ASP.NET Postback cycle flow in Randy Connolly’s website.

Have a look.

Postback

Server-side comment & Client-side comment

While programming, I use the comment/un-comment menu in the Visual Studio toolbar. The commented portion looks like, for example

<%-- Some Comment/Code here --%>

Later on only, I paid attention to it; as what makes it different from our normal HTML comments like;

<!-- Some Comment/Code here -->

The difference is simple. The former is called server-side comment & the latter is client-side comment. Serer-Side comments are not returned to the browser, while the client-side comments are returned to the browser.

Realizing, how I strained/managed to learn complex codes, without paying attention to such a silly thing. We all need Big without realizing what is Small.

Wednesday, March 11, 2009

EnableDecompression in ASP.NET WebService

Suppose that the Web Server from which a Web-Service client is requesting Web-Service supports compression; then the web-service client will receive respone as compressed data.

We can disable the decompression by setting the EnableDecompression property to true.

// on client web-application
Service srvc1 = new Service();
srvc1.EnableDecompression = true;
 
// call your web-service method here (example)
string dtime = srvc1.ReturnDateTime();
:
:
:

Tuesday, March 10, 2009

I’m on 1000+

profile

Just now only I noticed that i scored a 1000+ points in the Microsoft official ASP.NET forum. Happy to see that I’ve helped some developers. Also happy, I got some new techies too.

Thanks.

Edit/Update/Delete GridView automatically

There is an easy way to edit/delete/update rows in a GridView data control.

All you have to do is:

a) Configure SQLDataSource data-control.

b) Select a table.

c) Select column-field(s) including the primary-key column.

d) To the right, click on the Advanced button & select ‘Generate INSERT, UPDATE and DELETE statements’.

sqldatasource_auto_updatedeleteedit

c) Click Next; test-query & click Finish.

Now, go back to your GridView. On the smart-tag, select ‘Enable Editing’ and ‘Enable Deleting’.

gridview_ebanle_autohandling

That's it. Now run the page, try to editing/updating/deleting the records. Leave everything to SQLDataSource. You are hands free now.

How easy, isn't it ?

Monday, March 9, 2009

C# Code Formatter (Online)

There is an online C# code formatter, at Manoli. The output confirms to be HTML 4.01 specification & is color-coded. Also easier to read. It also supports VB, T-SQL, HTML, etc. So, have a look @ Manoli.

Tuesday, February 17, 2009

ASP.NET vs PHP

Finally, I came across the battlefield. Which is better?

Woo….!!!

A never-ending Hot debate.

Check out these links:

http://geekswithblogs.net/vivek/archive/2008/07/02/123516.aspx

http://www.oracle.com/technology/pub/columns/hull_php2.html

 
Best viewed in Internet Explorer 8.