A2H

This user hasn't shared any biographical information

Homepage: https://aspdotnetcodehelp.wordpress.com

[Solution]Dropdownlist not retaining selected item on Postback

Introduction

In this article we will look into an alternative solution to the problem with dropdownlist where selecteditems are not retained on postback.

Problem Description

When we select an item in dropdownlist and do a postback(button click) then the value selected on dropdownlist will reset to the first item in dropdownlist.

demo

Usually this issue happens when we reload the items in dropdownlist on every postback. To resolve the issue we will load the dropdownlist inside the If(!IsPostBack) block.

   protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DropDownList2.Items.Insert(0, new ListItem("One", ""));
                DropDownList2.Items.Insert(1, new ListItem("Two", ""));
                DropDownList2.Items.Insert(2, new ListItem("Three", ""));
                DropDownList2.Items.Insert(3, new ListItem("Four", ""));
                DropDownList2.Items.Insert(4, new ListItem("Five", ""));
                DropDownList2.Items.Insert(5, new ListItem("Six", ""));
                DropDownList2.Items.Insert(6, new ListItem("Seven", ""));
                DropDownList2.Items.Insert(7, new ListItem("Eight", ""));
                DropDownList2.Items.Insert(8, new ListItem("Nine", ""));
                DropDownList2.Items.Insert(9, new ListItem("Ten", ""));
                DropDownList2.SelectedIndex = 4;
            }
        }

However if you see the code preceding, you will notice that the dropdownlist is populated inside if(!IsPostBack) block. Now what is causing the issue?

Solution:

On further analysis I noticed that while populating the dropdownlist, user is passing an empty string to “value” property of dropdownlist.  Dropdownlist will render on page at runtime without value being populated. This invalid code for dropdownlist caused the issue.

<select name="DropDownList2" id="DropDownList2">
<option value="">One</option>
<option value="">Two</option>
<option value="">Three</option>
<option value="">Four</option>
<option selected="selected" value=""> Five</option>
<option value="">Six</option>
<option value="">Seven</option>
<option value="">Eight</option>
<option value="">Nine</option>
<option value="">Ten</option>
</select>

To resolve the issue we need to provide an entry to “value” property in dropdownlist.

 protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DropDownList2.Items.Insert(0, new ListItem("One", "1"));
                DropDownList2.Items.Insert(1, new ListItem("Two", "2"));
                DropDownList2.Items.Insert(2, new ListItem("Three", "3"));
                DropDownList2.Items.Insert(3, new ListItem("Four", "4"));
                DropDownList2.Items.Insert(4, new ListItem("Five", "5"));
                DropDownList2.Items.Insert(5, new ListItem("Six", "6"));
                DropDownList2.Items.Insert(6, new ListItem("Seven", "7"));
                DropDownList2.Items.Insert(7, new ListItem("Eight", "8"));
                DropDownList2.Items.Insert(8, new ListItem("Nine", "9"));
                DropDownList2.Items.Insert(9, new ListItem("Ten", "10"));
                DropDownList2.SelectedIndex = 4;
            }
        }

Demo

demo1

 

Advertisements

, , , ,

Leave a comment

Bundling and Minification in AspNetCore

Introduction:

In this article we will look into an easy option to do bundling and minification in Asp.Net Core. For those who are new to Bundling and Minification let’s try to understand these concepts.

Bundling

Bundling is the process of combining multiple javascript files to a single file. By this way we will reduce the number of requests needed for loading the files. i.e. if we had javascript code in multiple files then system will generate multiple requests to load all files however since we bundled all files there will be only one requests to load file. This will improve the load time.

Minification

Minification is the process of reducing the size of files (css , javascript). Usually size reduction happens by removing unwanted spaces in file.

For ex, below Javascript code

will change like below after minification.

Bundling and Minification using Gulp

We have variety of options available for Third party tools like Gulp and Grunt can be used to do bundling and minification process. However the problem with this approach is it involves additional steps which are slightly complex.

Using Bundler and Minification Extension

Mads Kristensen has created an extension namely “Bundler and Minifier”, using this we can easily do both bundling and minification in Asp.netCore. You can download the installer for extension from here.

Once you installed the extension, to do bundling and minification all you do is to Right click on the file and then select Bundler & Minifier option.

2017-08-22 14_38_43-Clipboard

If you have a single javascript file then you will see the only Minify File option.

MinfyOption

Once you select the option extension will generate the minified file for us. We can also follow the same process to minify css files as well.

, , , ,

Leave a comment

Implementing Multiselect feature on AutoCompleteExtender

AjaxControlToolkit’s AutoComplete extender is designed to be a single selection control. i.e. When user selects a value the dropdown list will be closed and selected text will be assigned to targeted textbox control.

Recently I got a question on the Asp.net Forums where the user wants to make the AutoCompleteExtender a multi select one. Besides the chosen values should be appended to textbox as comma separated values. This question caught my attention as it was a little tricky.

Here are the details of requirements

  • Keep the dropdown list of options on AutoComplete Extender opened
  • Allow multiple selection on AutoComplete Extender
  • Add the string to target textbox control as comma separated strings

We need to come up with a custom implementation to accomplish this requirement.  When I looked at some threads I found the code to keep the list opened. However, I need to customize it to generate comma separated values.

You can find complete implementation details here

Add a behaviorID to AutocompleteExtender control. We will use this id to close the dropdown list after user selecting values

Now we need to add a button to close the dropdown list after selecting values

Add the following javascript function to your page to close the window

Now we need to customize the inbuilt function “_setText” and “_hideCompletionList” function in order to accomplish our requirement

Complete Code

HTML

C#:

Code to populate the list when user type in characters on textbox

Demo

AutoCompleteExtenderDemo

, , , , ,

Leave a comment

Applying css style on cellClass not working in Angular UIGrid

cellClass is an attribute which allows user to specify different styles to cells in angular UI Grid.

Problem Description:

When user tries to apply custom styling to cellClass the changes won’t get applied to UIGrid.

An important point to be noted is by design uigrid have its own css styles. These css rules comes from uigrid.css or bootstrap.css. So if a user tries to change the default style of uigrid, we need to make sure that the CSS specificity applied properly.

eg:- User is trying to change the background color of uigrid cell. uigrid already have a default cell background color ( grey / white) . So if user need to make change to background color of cell, we need to make sure that CSS Specificity is applied properly.

Solution:

Let’s have a look at CSS Specificity. When you have two or more css rules pointing to same html element, then browser follows some rules to determine which css rule is more specific and that css rule will get applied to html element. This is called CSS Specificity.

  • ID Selector (#Someword)                              : More Specificity
  • Class/Attribute Selector (.Someword)         : Less Specificity than the ID selector.
  • html Selector (all)                                           : Less Specificity than the class selector.

If you notice above list ID selector has more priority, however we can assign priority to css selector by using !important attribute.if a css value is appended with !important, it overrides all other CSS specificity and precedence will be given to the css value with !important.

Hence in order to resolve the issue user need to make sure that user defined style sheet should have more specific css rule than the default rules from uigrid.css / bootstrap.css.

For ex: below user defined css rule will not work

.cellColorChange{
      background-color : 'red' ;
}

Reason is default css from ui-grd.css have more specificity. So this will take priority.

.ui-grid-row:nth-child(odd) .ui-grid-cell{
background-color: #fdfdfd;
}
.ui-grid-row:nth-child(even) .ui-grid-cell{
background-color: #f3f3f3;
}

We need to provide more specificity in user defined CSS. I have used (!important ) along with css property. It works perfectly fine

.cellColorChange{
    background-color : 'red'  !important;
}

 

 

, , , , ,

Leave a comment

Finding Textbox control based on a selected row in Jquery

Introduction

In this article I will demonstrate the steps to find the Textbox control from the selected row in repeater using Jquery.

Implementation

We use a Radiobuttonlist to select each rows. Requirement is to disable and enable the  textbox based on the selected radiobutton value for particular row. If user selected Yes then enable the textbox and if user selected No then enable textbox. You can use the below Jquery code to find the selected repeater row and then disable and enable textbox control based on selected value.

Jquery Code

Complete Code

Demo

gcda2ag

You can find complete source code here

 

Leave a comment

Insert functionality in UIGrid

Introduction

This is a continuation of my previous articles in which I demonstrated the procedure to

In this article I will demonstrate how to implement Add new record functionality in Angular UIGrid.

Prerequisites:

I have used icons from FontAwesome Library, which is a great source for multiple icons. You can download Fontawesome icons from here.

To display message after every operation we used UIBootstrap.

Implementation:

You would already have the Model configured by following my previous article on loading UIGrid with data. We will directly look into the details of Adding new record in UIGrid.

You can see a sample Demo of entire functionality below.

demo

UIGrid Updates

We will add a new button on page which responds to click event and open a modal pop up window. Users can enter the details and on save button click we will save the data to database.

AddCustomer function:

AddCustomer function will take the scope object as input which contains all values as provided by user and then save the data to database. User will also see an alert stating the status of insert operation.

Updated Angular code with Insert new Record functionality

Modal Pop up HTML

WEBAPI AddCustomer Method

You can find complete source code of this demo from github

, , , , , , ,

Leave a comment

How to read config settings in Asp.Net Core

Introduction

In this article I will explain the details of how to read config settings in Asp.Net Core.

Description

In Asp.Net webforms we used web.config file to store the config values like a connection string, appsettings etc.However, in Asp.Net core, the config file doesn’t exist anymore.  Instead, we have a slightly different approach where the configuration API provides a way of configuring an app based on a list of name-value pairs that can be read at runtime from multiple sources. You can read the config values from multiple sources like JSON, XML etc.

In this article we will examine how to implement config files based on JSON formats in Asp.Net Core application.

Add the Required references

To read the values from appsettings.json, we need to add the below references to project

Add appsettings.json file to store config values

We will store the config items as Name:Value pair in this json file.  You can also add hierarchical list in json file where each items will be separated by comma.

Sample value

Create ConfigurationBuilder in Startup.cs class

We use AddJsonFile” method in JsonConfigurationExtensions class to provide the Json File path. To reload the configuration file if the file changes we can set the reloadOnChange property.  You can find details of AddJsonFile method here.

Read the value from appsetttings.json file

Using the GetSection method we can access the values from appsettings.json file

Complete Code

You can find the complete source code for this article here

, , ,

Leave a comment