In this article I will go through a technique that you can use when working with a modal popup that lets you store form information from your popup to later use with your main Web form.
In earlier articles I have covered some of the general functions and benefits of using the ShowModalDialog function that I will expand on this article. If you have questions about some of the code I’ve used in this article, then please take a look at my earlier articles that might help explain some of the concepts:
If you are retrieving or adding values from and to your database then you will know the difficulties that can be caused by NULL values. The notorious InvalidCastException is one example of receiving a NULL value from your database into an ASP.NET object that doesn’t know what to do with database NULL values.
Here’s typical example of an InvalidCastException error caused by setting a String variable to a value that is returned as a NULL from your database query:
System.InvalidCastException: Conversion from type 'DBNull' to type 'String' is not valid. at Microsoft.VisualBasic.CompilerServices.Conversions.ToString(Object Value) at ASP.myPageName_aspx.Page_Load(Object sender, EventArgs e) in D:\Inetpub\wwwroot\MySite\myPageName.aspx:line 1xx
Sometimes you will want to do something very specific on a Web page using a combination of popular technologies. However the interaction of client and server-side languages such as JQuery and ASP.NET can become confusing, especially when the server side language starts adding client-side functionality such as through an UpdatePanel control.
In this article I’m going to review a very specialized case. Here are the key points that I want the code to handle:
- I have an ASP.NET DataList that is showing rows of information.
- One column of repeating information in the DataList contains a separate Date and Time component
- The footer of the DataList is being used to add new rows of information, including a Date and a Time field
- The editable time field should display a JQuery DatePicker widget
- When a date is selected on the JQuery DatePicker widget, the current time should be set to a separate text field that is beside the Date field
- The DataList is running inside an UpdatePanel control to only refresh the DataList when Server communications happens
- The DataList has an edit button which the user can press to make a row editable. This involves a refresh of the DataList so that JQuery has to know to re-attach the DatePicker control to the newly editable date field
This is a lot of functionality to cover in one article and I hope it is clear for you to understand how I want the JQuery to interact with the ASP.NET. To further explain things I will give some example code of both the JQuery and the markup tags of the DataList, and I will explain how to address the requirements step-by-step.
Before I start into in-depth explanations in this article, I’d like to mention that if you are interested in reading more about ASP.NET, please check out my series of articles covering ASP.NET development. I also have written a very interesting series of articles about using JQuery to add functionality to your pages that is worth taking a look at.
If you do like this article and find it useful, leave a comment below since I always like feedback. Also, you can check out other JQuery articles I have written that may also be of interest to you.
The Desired Behavior
I would like this example page to be similar to most HTML form pages. I’m including some random fields and text, and intend to add validation logic and a warning to one of the form fields. I intend the result to look like the screen-shot at the start of this article. Here are the requirements in point-form:
- Basically I want the example page to look like a standard HTML form.
- The first form text input field requires a data check so that the value entered by the user shouldn’t be higher than 10.
- If the value is higher than 10 then I want a modal JQuery UI warning dialog to appear when the user moves away from the field (on blur).
- The dialog should be aligned so that it is flush with the field.
Developers will sometimes want the modal popup to be more than just a fancy version of the common alert() function. In this case, one has the option of creating a fully functional Web form in the Web page called from the showModalDialog function.
Hooking up a JQuery Form Dialog Widget to an ASP.NET GridView Control
Let’s face it: the stock ASP.NET GridView control has some behaviors and limitations that aren’t exactly optimal. For starters, the lack of a nice way for users to add new rows to the grid is unfortunate. There are, of course, several different work-arounds for this all of which have their pros and cons.
I have been testing out using the JQuery Modal Form dialog widget as an input mechanism for new GridView rows. This has the benefit of not requiring the use of ASP.NET’s heavyweight ViewState and constant back and forth communication with the Web Server. Also, the modal nature of the widget on the Web form is a benefit.
The obvious drawback of using client side code is that the newly added rows are not stored in ViewState and you will need to handle them yourself. This really isn’t a big deal if you consider using Ajax calls to a Web Service rather than a complete form submission.
If you are looking for a tool to convert an HTML document to a PDF, then the iTextSharp converter is a great tool. iTextSharp provides open source libraries for various languages including Java and .NET. If you wish to commercially use iTextSharp, you will, however, need to pay for the license.
Keep in mind that I am basing my observations on the iTextSharp port to .NET. The Java version may behave differently.
I personally like the iTextSharp tool, and even given its quirks, will continue using it. Please note thatthis article is focussed on observations of the iTextSharp HTMLWorker object. Significant advancements have been made with the iTextSharp XMLWorker object which you can read more about in another article I have written.
Here are some of the main points to consider when planning/writing your HTML to PDF conversion function using the iTextSharp HTMLWorker Object (more…)
GUIDs are useful tools for .NET developers and Windows developers in general. If you need a unique value for your code, then a GUID (Globally Unique Identifier) is one of the best ways to go.
One thing to keep in mind when working with GUIDs is that there is always a chance of getting a collision/duplication between GUIDs, especially if you are working with large numbers of them.
Here is an example 128 bit GUID:
As you can see, based on the number of alphanumeric digits in the example above, the likelihood of getting a duplicate GUID is extremely low. However when using a GUID, there is always a chance that duplicates will appear, especially as the sample size increases, or as the method of generating the GUID changes.
It is because of this that I would suggest using them with small in-memory lists, or to uniquely identify programs in the registry, but I would be leery of using GUIDs as primary keys in a large & permanent SQL Server table (I’m certain there are people who will disagree with me on this point, but I stand firm in my belief that this is bad coding practice).
I have been working with ASP.NET collections and thought a more detailed look into one of the most useful classes also know as the Dictionary collection object is in order.
To review: the Dictionary class is part of the System.Collections.Generic Namespace. If you haven’t already, then it’s worth having a look at the list of collections contained in the namespace to become familiar with the various collections options available to you. The main benefit of the using collections from the Generic namespace is that the collections are strongly typed, giving you better performance and type safety.
The Dictionary collection stores key – value pairs much like a Hashtable from the System.Collections Namespace. However where a Hashtable has an inbuilt re-ordering system based on your provided keys, the Dictionary collection works on a First-In-First-Out (FIFO) basis. This can be a real boon if you want key value pairs that don’t get randomly re-arranged. The Dictionary object is not documented to be FIFO, but it has been working this way since .NET 1.1 so I hope that this will continue to be the case. Similar objects to the Dictionary class are the SortedList and the SortedDictionary classes, but these do have inbuilt sorting similar to the Hashtable’s sorting, so they are only situationally useful.
In addition to the Dictionary object, I like to use ArrayLists and string arrayx. So as part of my example code I show how to store a String array inside a Dictionary object as the value. This can be quite useful in a number of ways depending on your goals, but the flexibility this method offers is something to keep in mind.
If you are interested in reading the API for the Dictionary object then I definitely recommend reading the official Microsoft Documentation. An interesting point that’s covered by the documentation is that using TryGetValue is the most efficient syntax to use rather than ContainsKey or ContainsValue.
In an earlier article I covered how to generate Excel reports on the fly using the Interop.Excel Namespace.
This is a really handy technique that also gives you full control over the minutia of the document you are creating. However the catch in using this technique is that you will need to update the configuration of your Web server to allow Web users to trigger Excel on your Web server. This can mean some pretty drastic security changes and possible loopholes in your Web server’s security so you should do a risk analysis before choosing this method.
In this article I will review the Web server security updates that need to be made to allow using the Interop.Excel Namespace to generate Excel documents for your Web site.
Please note that since I am developing this site for an Intranet, I am not as concerned with locking down the server. If you are working with a server that is exposed to the Web then you will want to review these security changes much more thoroughly.
Choosing the best way to export your data from an ASP.NET Web page to Excel for your users is never an easy task. There are many ways to accomplish this task, each way having its pros and cons.
In an earlier article I covered one of the more common ways: how to take an existing report that has already been generated as an HTML table and stream it as Excel to the client browser. This is a great technique, but leaves it up to Excel to interpret the HTML that you are sending its way. Furthermore, Microsoft seems to be deprecating the markup that was previously so amazingly convenient when formatting HTML streamed to Excel. The article also goes into depth about what markup commands are still useful and what markup has been deprecated. It’s definitely worth reading this article if you are considering streaming HTML as an Excel document using the command: Response.ContentType = “application/ms-excel”
Before I go into further details on using the Interop.Excel namespace to generate Excel files, I’d like to point out that I am providing a fully functional example at the end of this article. If you are primarily interested in the code, then you can skip to the bottom of this article and you can copy and paste the code sample into a Visual Studio project where it should run without any problems.
Using an XSL stylesheet together with an ASP.NET XML control can be a great way to format the output of a database query. For instance a cool implementation is that you can embed an XML control within a ListView control so that you can output a tree-like structure in your ListView rather than the standard tabular structure.
Use RAD with Visual Studio WebForms to Programmatically Query a SQLDataSource Control from Custom Code
Rapid Application Development (RAD) offers huge advantages to developers under time constraints looking to get an application released as quickly as possible. Visual Studio 2010′s WebForms is a great environment to use when looking at possible RAD solutions.
In this article I will cover the issue of using RAD with custom code to speed up writing database connections and queries. This is part of my series of articles investigating ASP.NET and Visual Studio 2010.
When putting together a Web based form space can be at a premium. Select lists are typically auto-adjusted by your browser to be the length of the longest option in the list.
But that just doesn’t work when space is tight and in many of my forms one doesn’t want the select list to automatically size itself to a length beyond what has been allocated for it, especially if the options are dynamically coming from a database.