HI WELCOME TO SIRIS
Showing posts with label ASP.net. Show all posts
Showing posts with label ASP.net. Show all posts

Validate ListBox and DropDown List using Required Field Validator

In Asp.net, sometimes we need to validate ListBox and DropDown List using Required field validator. It is tricky and avoids the use of JavaScript function to validate ListBox and DropDown List. Here I am sharing the code.

Validate DropDown List and ListBox

  1. <form id="form1" runat="server">
  2. <div>
  3. <asp:ListBox ID="lstboxCity" runat="server" Width="200px" SelectionMode="Multiple">
  4. <asp:ListItem Value="1">Delhi</asp:ListItem>
  5. <asp:ListItem Value="2">Noida</asp:ListItem>
  6. <asp:ListItem Value="3">Gurgaon</asp:ListItem>
  7. <asp:ListItem Value="4">Mumbai</asp:ListItem>
  8. <asp:ListItem Value="5">Pune</asp:ListItem>
  9. <asp:ListItem Value="6">Banglore</asp:ListItem>
  10. <asp:ListItem Value="7">Lucknow</asp:ListItem>
  11. </asp:ListBox> <asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="lstboxCity" InitialValue="" runat="server" ErrorMessage="Please select atleast one city"></asp:RequiredFieldValidator>
  12. </div>
  13. <br />
  14. <div>
  15. <asp:DropDownList ID="ddlCity" runat="server" Width="200px">
  16. <asp:ListItem Value="0">--Select--</asp:ListItem>
  17. <asp:ListItem Value="1">Delhi</asp:ListItem>
  18. <asp:ListItem Value="2">Noida</asp:ListItem>
  19. <asp:ListItem Value="3">Gurgaon</asp:ListItem>
  20. <asp:ListItem Value="4">Mumbai</asp:ListItem>
  21. <asp:ListItem Value="5">Pune</asp:ListItem>
  22. <asp:ListItem Value="6">Banglore</asp:ListItem>
  23. <asp:ListItem Value="7">Lucknow</asp:ListItem>
  24. </asp:DropDownList>
  25. <asp:RequiredFieldValidator ID="RequiredFieldValidator2" ControlToValidate="ddlCity" InitialValue="0" runat="server" ErrorMessage="Please select city"></asp:RequiredFieldValidator>
  26. </div>
  27. <br />
  28. <asp:Button ID="btnSubmit" runat="server" Text="Submit" />
  29. </form>



Note

For validating DropDown List, make sure you have "--Select--" option value 0 other wise validation on it will not work.
Summary
In this article I try to explain how to validate DropDownList and ListBox using Required Field Validator. I hope after reading this article you will be able to use this trick in your code. I would like to have feedback from my blog readers. Please post your feedback, question, or comments about this article.

Asp.net Enter Key to submit form

Suppose, you want to press/click submit button on Enter key press or you are trying to post the form on Enter key press. In asp.net, to achieve this functionality we need to set "Defaultbutton" property either in Form or in panel.

Form DefaultButton Property

  1. <form id="form1" runat="server" defaultbutton="btnSubmit">
  2. <div>
  3. <asp:TextBox ID="txtUserID" runat="server"/> <asp:TextBox ID="txtUserpwd" runat="server"/> <asp:Button ID="btnSubmit" runat="server" OnClick="btnSubmit _Click" Text="Submit"/>
  4. </div>
  5. </form>

Panel DefaultButton Property

  1. <asp:Panel ID="Panel1" runat="server" defaultbutton="btnSubmit">
  2. <div>
  3. <asp:TextBox ID="txtUserID" runat="server"/> <asp:TextBox ID="txtUserpwd" runat="server"/> <asp:Button ID="btnSubmit" runat="server" OnClick="btnSubmit _Click" Text="Submit"/>
  4. </div>
  5. </asp:Panel >

Note

  1. We specify the defaultbutton property at the Form level in the form tag when there is only one Submit Button for post back.
  2. We specify the defaultbutton property at the Panel level in the Panel tag when there are multiple Submit Button for post back.
Summary
In this article I try to explain the default submit behavior of form and panel. I hope you will refer this article for your need. I would like to have feedback from my blog readers. Please post your feedback, question, or comments about this article.

A Brief Version History of ASP.NET

ASP.NET Framework is a part of .NET framework. It is used to create dynamic website, web application and web services. It is a server side technology that uses all .NET compatible language such as C#, VB.NET, J# etc. which are compiled to Microsoft Intermediate Language (MSIL). ASP.NET uses server control to develop rapid and interactive application in easy way.
ASP.NET Version History
ASP.NET Version
Introduced with .NET & IDE
Features Detail
4.5.1
4.5.1 and Visual Studio 2013
  1. One ASP.NET
  2. ASP.NET Scaffolding
  3. ASP.NET Identity
4.5
4.5 and Visual Studio 2012
  1. Strongly Typed Data Controls
  2. Model Binding
  3. Unobtrusive Validation
  4. Bundling and Minification
  5. Async Support
  6. Support for asynchronous modules and handlers
  7. Friendly URL
  8. HTML5 Features enhancements
  9. Support for WebSocket protocol
  10. oAuth Support
4.0
4.0 and Visual Studio 2010
  1. Introduced ClientIdMode property for Server Control
  2. Routing
  3. Introduced Meta tags MetaKeyword and MetaDescription
  4. Chart Control
3.5
3.5 and Visual Studio 2008
  1. Integrated ASP.NET AJAX
  2. Support LINQ
  3. New Data controls LINQ DataSource, ListView and DataPager
  4. Dynamic Data
  5. Multi-targeting Framework Support
2.0
2.0 and Visual Studio 2005
  1. New Data controls GridView, FormView and DetailsView
  2. DataSource controls SQLDataSource, Object DataSource, AccessDataSource, XMLDataSource and SiteMapDataSource
  3. Navigation controls
  4. Master pages
  5. Cross Page Postbacks
  6. Validation Groups
  7. Themes
  8. Skins
  9. Login controls
  10. Role Management
  11. Profiles
  12. Membership Service
  13. Localization and Globalization
1.1
1.1 and Visual Studio .NET 2003
  1. ASP.NET Mobile controls
  2. Built-in support for ODBC and databases
  3. Internet Protocol version 6 (IPv6) support
1.0
1.0 and Visual Studio .NET
  1. Support for Object-oriented Web application development
  2. Use of DLL class libraries
What do you think?
I hope you will enjoy the ASP.NET features. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.

Introduction to ASP.NET

According to Microsoft, "ASP.NET is a technology for building powerful, dynamic Web applications and is part of the .NET Framework". In fact, ASP.NET is a programming framework used to develop web applications and web services. Basically, it is the next version of ASP. It provides the easy way to build, deploy & run the web application on any browser.

Benefits of Asp.Net

  1. ASP.NET makes development simple and easy to maintain with event-driven and server side programming model.
  2. ASP.NET source code is executed on the server. The source code is complied first time the page is requested. The server serves the complied version of the page for use next time the page is requested.
  3. ASP.NET provides validations controls.
  4. The html produced by ASP.NET is sent back to the browser. The application code that we write is not sent back to the browser and is not stolen easily.
  5. In Asp.Net business logic(in .cs class file) and presentation logic(in .aspx file) are in separate files.

ASP.NET Page Life Cycle Events

At each stage of the page life cycle, the page raises some events, which could be coded. An event handler is basically a function or subroutine, bound to the event, using declarative attributes like Onclick or handle. Asp.Net 3.5 & 4.0 page life cycle has following events in sequence :
  1. PreInit

    It is is entry point of page life cycle. It checks IsPostBack property to check or recreate dynamic controls. In this we can set master pages dynamically & set and get profile property values.
  2. Init

    It is is raised after all controls of page are initilised and skin properties are set. It is used to read or initialise control properties.
  3. InitComplete

    This indicates that page is completely initialised.
  4. Preload

    This event is called before loading the page in the RAM(memory). If any processing on a control or on page is required we use it.
  5. Load

    This invokes the onload event of the page. In this we create connection to the database, get/set controls values and get/set view state values.

    Source: https://msdn.microsoft.com
  6. loadComplete

    This indicates that page is completely loaded into memory.
  7. Prender

    we use this event to make final changes to the controls or page before rendering it to the browser.
  8. SaveStateComplete

    View state of each control is saved before this event occurs. If we want to change the view state of any control then we use this event. This event can not be used to change the other properties of the controls.
  9. Render/PrenderComplete

    This indicates that page is completely rendered to the browser.Before rendering, view state is saved for the page and all controls. During the rendering stage, the page calls the Render method for each control, providing a text writer that writes its output to the OutputStream object of the page's Response property.
  10. Unload

    The Unload event is raised after the page has been fully rendered, sent to the client, and is ready to be discarded. At this point, page properties such as Response and Request are unloaded and cleanup is performed.We can use this event for closing files & database connection. This event occurs for each control.

Introduction to Ajax

AJAX stands for Asynchronous JavaScript and XML. Ajax is a web based technique and a combination of commonly used techniques like HTML/XHTML, CSS, XML/XSLT, Document Object Model (DOM), JavaScript and the XMLHttpRequest object, to create Rich Internet Applications (RIA).
Ajax allows the user to interact with a web server asynchronously, without using a plug-in. This asynchronous interaction of user with the web server is possible with the help of XMLHttpRequest object found in browsers.

Advantages of AJAX

  1. AJAX is platform independent means it is a cross platform technology that can be used with any browsers; since it is based on XML & JavaScript.
  2. AJAX makes your application more user friendly, faster, and more interactive.
  3. With AJAX, JavaScript script directly communicate with the server with the help of XMLHttpRequest object and can exchange data with the server, without reloading the page.
  4. AJAX uses asynchronous data transfer (HTTP requests) between the browser and the web server, by allowing web pages to request small bits of information from the server instead of entire pages.
  5. Ajax minimize the round trips to the server.

Disadvantage of AJAX

  1. AJAX is based on JavaScript and different browser implement JavaScript different way. Hence your application may behave differently on different browsers. Also, sometimes you need to write browser specific code to implement your application functionality.
  2. If JavaScript is disabled with in the browser, then AJAX will not be able to perform any work.
  3. It is difficult to bookmark a specific state of the web page since page changes dynamically.

What is Asp.Net AJAX?

ASP.NET AJAX is an extension to Asp.Net, developed by Microsoft to make dynamic, client-centric, user-friendly, and interactive web applications. ASP.NET AJAX has its own AJAX Library and AJAX controls to develop rich Web applications that communicate with the server using asynchronous postback.
What do you think?
I hope you will enjoy the tips while programming with Asp.Net AJAX. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.