Two Important Notes On RegisterStartupScript

In this Blog, we will explore some interesting stuff related to the RegisterStartupScript Method.

Showing one Alert Box

string script = "alert('Hello World !!!')";

ClientScript.RegisterStartupScript(this.GetType(), "script1", script, true);

If you write this code in code behind, it will show one Alert Box on the page. Let’s see how the script is added to the page dynamically. Below is the FireBug Script View.

Example 1 Rendered Script for Alert with RegisterStartupScript

Example 1 Rendered Script for Alert with RegisterStartupScript

Showing two Alert Boxes

Now, let’s write some more codes to run another script.

string script = "alert('Hello World !!!')";
string script1 = "alert('Hello World Again !!!')";

ClientScript.RegisterStartupScript(this.GetType(), "script", script, true);
ClientScript.RegisterStartupScript(this.GetType(), "script", script1, true);

Will this work !!! Let’s try in browser and see. Only, one Alert showing on browser instead of two.

Alert Box on Browser

Alert Box on Browser

This is how it looks in script Tab of FireBug…

Example 2 Rendered Script for Alert with RegisterStartupScript

Example 2 Rendered Script for Alert with RegisterStartupScript

So, the question here is where is the next Alert? Why it did not work? Why it did not get rendered on browser?

What happened to the second Alert Box?

So, after this, I dug more into the code, after getting a coffee and try to see carefully what I have written. The second parameter of the Method is actually a key.

A startup script is uniquely identified by its key and its type. Scripts with the same key and type are considered duplicates. Only one script with a given type and key pair can be registered with the page. Attempting to register a script that is already registered does not create a duplicate of the script.

Call the IsStartupScriptRegistered method to determine whether a startup script with a given key and type pair is already registered and avoid unnecessarily attempting to add the script.

The very first line clears everything. There is also one method to check whether the script is already registered or not.

We should have Unique Keys for every Script we register

So, without further delay, I quickly changed the key. So, the code will look like…

string script = "alert('Hello World !!!')";
string script1 = "alert('Hello World Again !!!')";

ClientScript.RegisterStartupScript(this.GetType(), "script", script, true);
ClientScript.RegisterStartupScript(this.GetType(), "script1", script1, true);

Now, question is, will it work? Yes/No !!! Let’s test.

Still Not Working !!!

Oops !!! Nothing worked. Rendered script is as follows.

Example 3 Rendered Script for Alert with RegisterStartupScript

Example 3 Rendered Script for Alert with RegisterStartupScript

Can you see, what is the issue? If not, then the following image of Console will clarify all our doubts.

Example 3 Console Error

Example 3 Console Error

Head bang, we are missing Semicolons !!!

So, we are actually missing a semicolon (;) after the first line of code. In JavaScript, semicolons are optional, provided the code lines are separated by new line character. But here, RegisterStartupScript adds the scripts in one line, which bugs the page eventually. Let’s modify our code again to include semicolons after the Alert statements.

string script = "alert('Hello World !!!');";
string script1 = "alert('Hello World Again !!!');";

ClientScript.RegisterStartupScript(this.GetType(), "script", script, true);
ClientScript.RegisterStartupScript(this.GetType(), "script1", script1, true);

Now, it perfectly works, showing two Alert Boxes one after the other.

Conclusion

We explored the following points.

  • Key in the RegisterStartupScript method should be unique.
  • Each statement of JavaScript should have a semicolon at last, so that it will treat the next JavaScript statement as code.

I hope you enjoyed reading the Blog. Feel free to comment on the Blog. If you liked, please share among your friends.

DZone Most Valuable Blogger

MVB Logo

MVB Logo

Yes, you heard that right. I am now a MVB (Most valuable Blogger) of DZone. Yesterday I got the mail from Ryan Spain (Content Curator, DZone) saying…

DZone MVB Mail

DZone MVB Mail

What is the MVB Program?

Here is what DZone describes about this program:

DZone’s Most Valuable Blogger program brings together a group of highly talented bloggers, authors, and technologists actively writing about topics of interest to the developer community. These people are recognized in the industry for their contributions and deep technical knowledge on subjects ranging from software design and architecture to programming on a range of platforms including Java, .NET, Ruby and others.

Refer – About Most Valuable Blogger (MVB) Program to explore more.
My DZone profile – http://dotnet.dzone.com/users/taditdash looks really cool.

DZone MVB Profile

DZone MVB Profile

It all started…

…Way back in January, when Code Project awarded me as a Most Valuable Professional. I was contributing mostly on Forums and Social Sites. After I got the award, I started this Blog. Blogging gave me the platform to explain my day to day problems with my friends and followers.

Then, during my birthday month (April 2014), Microsoft recognized all my had work and awarded me as a Most Valuable Professional. Thanks to Mr. Biplab Paul, my MVP Lead and my fellow MVPs.

I was surprized when my CEO Mr. Chinmoy Panda wrote about me on Mindfire Solutions Blog. Let me quote couple of lines here…

His story inspires me. Coming from the small town of Nayagarh, Tadit joined Mindfire at Bhubaneswar 3 years back. After proving his worth at work, his voluntary energy led to responsibility for “extra non-work stuff”, and subsequent awards, at Mindfire. He moved ahead to receive a CodeProject MVP award few months back, and now he has received the Microsoft MVP recognition. That’s not all.

He is not only Mindfire’s first Microsoft MVP, but also the first Microsoft MVP from Bhubaneswar, a city with 10,000 software engineers! Wonderful.

Be awesome. Be Tadit.

The Blog really meant a lot me. Mindfire Solutions always encouraged my activities and motivated me to do more and more. I did not look back after that. I am also getting good response from the readers. So, I am enjoying.

Again, I would like to dedicate this to my…

Grand Father (Ganeswar Tripathy). He is always with me and guiding me towards every step I take.

What’s Next?

It’s another tag that has been added to my profile. By becoming a DZone MVB, I gave DZone permission to publish some of my blog articles on their site. This will give me chance to reach out to maximum people.

I have started speaking on Events. If you are doing any Event or know someone who is organizing, do connect them with me. I would love to speak about my Experience or about the Technology I am working on.

Following are my Social Profiles to connect with me.
Facebook – https://www.facebook.com/taditdash
Twitter – http://www.twitter.com/taditdash
LinkedIn – https://www.linkedin.com/in/taditdash
Google+ – https://plus.google.com/u/0/+TaditDash/

Also, if you have not followed my Blog, just register your mail id in the box at right side saying “Follow Blog Via Email“. All my new Blogs will be directly delivered to your inbox.

Thank you everyone for following my blog. Please don’t forget to share your feedback about my articles. This will help me to write much better contents in future.

Cascading DropDownList on GridView

Cascading DropDownLists on GridView

Cascading DropDownLists on GridView


In this Blog, we will learn how to get the current Row of GridView inside the SelectedIndexChanged Event of a DropDownList present on the GridView itself and then find the other DropDownList and bind that.

Background

The requirement came up in one of my projects to Bind one DropDownList present on a Column based on the selection of DropDownList present on another Column. So, it was like the Cascading DropDownLists, where both the DropDownLists are present on the same GridView.

So, steps to get the solution would be like…

  1. Attach the SelectedIndexChanged Event for the first DropDownList on GridView
  2. Inside the Event, first get the GridView Row, from which the first DropDownList is selected
  3. Then from the current GridView Row, find the second DropDownList and bind it with a DataTable or something

The step which is bold is very important. Let’s go step by step.

GridView Markup

In the Markup, you can see that I have two DropDownLists Declared inside the GridView. One is for Country and another one for City. Just for demo purpose, I have hard coded the Items for Country. You can dynamically populate that using code.

Note: According to Step – 1, I have attached the OnSelectedIndexChanged Event to the Country DropDownList.
Goal: Our goal is to populate the City DropDownList for the particular row, when Country is selected in that row.

<asp:GridView ID="gvWithCascadingDropDownList" runat="server" AutoGenerateColumns="false">
	<Columns>
		<asp:BoundField DataField="RowNumber" />
		<asp:TemplateField HeaderText="Country">
			<ItemTemplate>
				<asp:DropDownList 
                        ID="ddlCountry" runat="server" 
                        AutoPostBack="true" 
                        OnSelectedIndexChanged="ddlCountry_SelectedIndexChanged">
					<asp:ListItem Text="-1">Select Country</asp:ListItem>
					<asp:ListItem Text="India">India</asp:ListItem>
					<asp:ListItem Text="Nepal">Nepal</asp:ListItem>
					<asp:ListItem Text="Bangladesh">Bangladesh</asp:ListItem>
					<asp:ListItem Text="Sri lanka">Sri lanka</asp:ListItem>
				</asp:DropDownList>
			</ItemTemplate>
		</asp:TemplateField>
		<asp:TemplateField HeaderText="City">
			<ItemTemplate>
				<asp:DropDownList ID="ddlCity" runat="server">
				</asp:DropDownList>
			</ItemTemplate>
		</asp:TemplateField>
	</Columns>
</asp:GridView>

What’s Next?

Let’s write code inside the Country Change Event and populate the City DropDownList.
So, according to the Step – 2

Inside the Event, first get the GridView Row, from which the first DropDownList is selected

For the above, we have to get the current Country DropDownList first, which we can easily get by.

// Get the Country DropDownList first.
DropDownList ddlCountry = (DropDownList)sender;

Now the important Step – 3.

Then from the current GridView Row, find the second DropDownList and bind it with a DataTable or something

In order to get the Containing Row for the DropDownList, we have to use the Control.NamingContainer Property.

The naming container for a given control is the parent control above it in the hierarchy that implements the INamingContainer interface. A server control that implements this interface creates a unique namespace for the ID property values of its child server controls. You can use the NamingContainer property of a naming container’s child control to get a reference to its parent container.

So, after getting the current GridView Row, now it is just a matter of finding the City DropDownList on that particular row and bind that with appropriate Data.

Below is the full code. I have used one Service to get the Cities Data. To use the Service, add Service Reference to your Project by right clicking on the Project in Visual Studio.

Service Reference Dialog

Service Reference Dialog


Service URL is – http://www.webservicex.net/globalweather.asmx?WSDL

protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
{
	// Get the Country DropDownList first.
	DropDownList ddlCountry = (DropDownList)sender;

	// Get the current GridView Row, from which the DropDownList is selected.
	GridViewRow currentRow = (GridViewRow)ddlCountry.NamingContainer;

	// Now let's find the City DropDownList on the same GridView Row.
	DropDownList ddlCity = (DropDownList)currentRow.FindControl("ddlCity");

	if (ddlCountry != null && ddlCountry.SelectedIndex > 0 && ddlCity != null)
	{
		string selectedCountry = ddlCountry.SelectedValue;

		// Get the Cities from the Service.
		string xmlCities = GetCitiesByCountry(selectedCountry);

		// Let's parse the XML into DataTable.
		XmlTextReader citiesReader = new XmlTextReader(new System.IO.StringReader(xmlCities));
		DataSet dsCities = new DataSet();
		dsCities.ReadXml(citiesReader);

		// Bind the City DropDownList.
		if (dsCities != null && dsCities.Tables.Count > 0 && dsCities.Tables[0].Rows.Count > 0)
		{
			ddlCity.DataSource = dsCities.Tables[0];
			ddlCity.DataTextField = "City";
			ddlCity.DataValueField = "City";
			ddlCity.DataBind();
		}
	}
	else if (ddlCity != null)
	{
		ddlCity.Items.Clear();
	}
}

private string GetCitiesByCountry(string selectedCountry)
{
	GlobalWeatherServiceReference.GlobalWeatherSoapClient client = new GlobalWeatherSoapClient();
	return client.GetCitiesByCountry(selectedCountry);
}

Hope You Enjoyed Reading

If you have any queries, please comment below. I will come back to you. Feel free to Like and Share the Blog in Social Sites.

Thanks for reading. 🙂

Top Features of ASP.NET vNext

Of course, as the name suggests, it is the upcoming version of ASP.NET.

At a Glance…

The next version of ASP.NET has been redesigned from the ground up. It has been designed to provide you with a lean and composable .NET stack for building modern cloud-based apps. The Applications will be faster as well as modular and consume less memory. .NET vNext is the next major release of .NET Framework. .NET vNext will have a cloud optimized mode, which will have a smaller clone of the Framework as compared to the full .NET Framework. ASP.NET vNext will build on .NET vNext.

Overview of ASP.NET vNext

Overview of ASP.NET vNext


Let’s explore some of the features of ASP.NET vNext.

No more System.Web !!!

Could you imagine ASP.NET without System.Web.dll !!! Yes, it’s happening now in the new Version. This dll makes the .NET applications slower and also consumes higher memory. Therefore, there’s no Web Forms in vNext, but Web Form development will continue to be supported in Visual Studio. MVC, Web API and Web Pages have been merged into one framework, called MVC 6. This will follow common programming approach between all these three i.e. a single programming model for Web sites and services. ASP.NET vNext includes new cloud-optimized versions of MVC, Web API, Web Pages, SignalR, and Entity Framework.

First Class Citizen is NuGet

Microsoft’s new strategy is…

“mobile-first, cloud-first”

As a result, the vNext Apps can use a cloud-optimized runtime and subset of the .NET Framework. You will be astonished by the fact, this subset of the framework is about 11 megabytes in size compared to 200 megabytes for the full framework. This is because the .NET Framework is composed of NuGet Packages.
Can you guess what I am going to write next? Yes / no !!! Okay, let me tell you. As the Cloud Optimized Framework is just a composition of NuGet packages, so control is with you. You can select and add the NuGet package, that is necessary for your Application. No need to have the whole Framework added to the App, while you are not even using 10% of it. No unnecessary memory, disk space, loading time, etc. For instance, if your App implements Dependency Injection, then include it, otherwise not.

New Development Experience

Time Elapsed Shown on the Go

Now, in Visual Studio, you can see how much time a particular method or code line takes to execute.

Time Elapsed in Visual Studio

Time Elapsed in Visual Studio

Save and Refresh, no Build

Change code files, don’t build again. Go to browser and refresh. Will it work? Answer is Yes, with vNext. This is a very interesting and time saving feature. As a developer, we know the pain of building the App again and again for any simple change. Now, in vNext, we don’t have to. This is the power of new Open Source Roslyn compiler, which compiles code dynamically. You can even use Notepad or any Text Editor to directly edit the files and just save to see the change. That means we can now build ASP.NET Web Apps without Visual Studio. But Visual Studio provides the best development experience. See the video below to see how this works.

Even, you can now edit code on the go using Visual Studio Online (“Monaco”).

Update Code in vNext

Update Code in vNext

Open Source, Cross Platform

As I mentioned that, we can build projects without Visual Studio, this implies that .NET vNext is open source and cross platform. It can run on Mono, on Mac and Linux. Everything is available at .NET Foundation and Github. You can Follow and Contribute.

vNext Cross Platform

vNext Cross Platform

Dependency Injection

Dependency Injection is built into vNext. All of the vNext components like MVC, Web API, SignalR, Entity Framework and Identity will use the same Dependency Injection.

Side by Side Deployment

Framework is with you, so forget System Admin !!!

For upgrading your App to the latest Framework, you always request your System Administrator or Network guy to install the new Framework, so that you can consume that. In the next generation of .NET world, no need to do that. Framework itself comes as NuGet Package and gets included to the project you create. Microsoft would deliver updates for the package and your App can directly get the latest Framework changes. That also means, you can have multiples Apps on a Server, running different .NET Frameworks. Isn’t it interesting !!!

Application is insulated from Server’s Framework Changes

Application on the Production Server would not have any deployment issues further, as it won’t be affected by Server’s Framework patch level. That is because, the Application itself has the NuGet Package of certain Framework level attached with it. Even if the Framework on Server changes, it cannot affect the Applications.

But where to start?

There are many more life changing things coming in this new version. Below are the resources you can refer to explore more.

Join Hands with the Team

If you have any questions, suggestions, then you can directly work with the team. Below are the platforms you can use to use.

Uncaught TypeError While Attaching Client Side Event to Telerik RadEditor

In this Blog, we will explore the error I faced, while I was attaching Events to Telerik RadEditor and how I resolved that.

Walkthrough

Declare the Event in Markup

We need to declare RadEditor OnClientLoad Event for the RadEditor.

This event is fired after the content is set in the content area, after the OnClientInit event and just before the editor is ready for use.

<telerik:RadEditor
   runat="server"
   ID="RadEditor1"
   OnClientLoad="OnClientLoad">
</telerik:RadEditor>

Define the Event

We are going to attach onkeydown, onchange and onpaste Events to the RadEditor. So, whenever we type or paste something in the RadEditor, detectChanges() will be fired.

function OnClientLoad(editor)
{
    editor.attachEvent('onkeydown', function(){ detectChanges(); });
    editor.attachEvent('onchange', function(){ detectChanges(); });
    editor.attachEvent('onpaste', function(){ detectChanges(); });
}

function detectChanges()
{
    alert("You are doing something in RadEditor !!!");
}

Let’s Test

Let’s type or paste something in the RadEditor now.

OOPS !!!! Something bad happened. The below is what I see in the Chrome Developer Tool Browser Console.

RadEditor Error

RadEditor Error

What to do now?

Now, the error message says “Uncaught TypeError : undefined is not a function OnClientLoad“. That means, the way we are attaching the Events, is wrong.
So, I looked back to where I started and suddenly I saw that we should attach the Event with attachEventHandler instead of attachEvent. Moreover, attachEvent is no longer supported as MSDN indicated.

Therefore, the updated working code will be…

function OnClientLoad(editor)
{
    editor.attachEventHandler('onkeydown', function(){ detectChanges(); });
    editor.attachEventHandler('onchange', function(){ detectChanges(); });
    editor.attachEventHandler('onpaste', function(){ detectChanges(); });
}

function detectChanges()
{
    alert("You are doing something in RadEditor !!!");
}

Final correct Output

Here is the alert box, which opens when we type something in the RadEditor.

RadEditor onkeydown Event

RadEditor onkeydown Event

End Notes

Hope you liked the Blog. Please share among your circle. Stay updated by following the Blog with Email ID or WordPress Account (“Follow Blog via Email” Widget is on the right side panel). Go to “About” Page to know more about me and ways to connect with me. You can simply write to me via the “Contact” form. Thanks for reading.

Enable or Disable All Tabs Except the Selected Tab inside the Telerik RadTabStrip

We will explore the JavaScript code, by which we can easily Enable or Disable all the Tabs inside a Telerik RadTabStrip except the SelectedTab. In my previous Blog, we explored how to Enable or Disable all the Tabs. Now let’s see how we can just do this for all the Tabs except the current Tab.

How this is helpful?

This kind of requirement comes, when you have some business logic in a Tab (without saving the data), other Tabs can’t be accessible. Take one example, like, when you are registering something step by step. So, in first Tab, you will ask the User to enter some details and when he/she enters and hits register, you Enable all other Tabs, else just Disable.

How to?

If the Tab Text does not match with the SelectedTab‘s Text, then Disable it. Simple !!!

function DisableOtherTabs(){
    // Get the Tab Strip.
    var tabStrip = $find("<%= yourTabStripID.ClientID %>");

    // Get all the Tabs.
    var tabs = tabStrip.get_tabs();

    // Get Selected Tab.
    var selectedTab = tabStrip.get_selectedTab();

    // Now loop through all the Tabs and Disable one by one.
    for(var i = 0; i < tabStrip.get_allTabs().length; i++){
        // If the Tab Text does not match with the Selected Tab's Text, then Disable it. Simple !!!
        if(tabs.getTab(i).get_text().toUpperCase() != selectedTab.get_text().toUpperCase()){
            tabs.getTab(i).disable();
        }
    }
}

Thanks !!!

For taking time and reading the Blog. If you find it useful, then share within your circle, by pressing the Social Icons.

Enable Disable all Tabs inside Telerik RadTabStrip

We will explore the JavaScript code, by which we can easily Enable or Disable all the Tabs inside a Telerik RadTabStrip.

Read the comments inside the code blocks to know how the code works.

Enable All Tabs

function EnableAllTabs(){
    // Get the Tab Strip.
    var tabStrip = $find('<%= yourTabStripID.ClientID >');
    
    // Get all the Tabs.
    var tabs = tabStrip.get_tabs();

    // Now loop through all the Tabs and Enable one by one.
    for(var i = 0; i < tabStrip.get_allTabs().length; i++){
        tabs.getTab(i).enable();
    }
}

Disable All Tabs

function DisableAllTabs(){
    // Get the Tab Strip.
    var tabStrip = $find('<%= yourTabStripID.ClientID >');
    
    // Get all the Tabs.
    var tabs = tabStrip.get_tabs();

    // Now loop through all the Tabs and Disable one by one.
    for(var i = 0; i < tabStrip.get_allTabs().length; i++){
        tabs.getTab(i).disable();
    }
}

ASP.NET GridView Client Side Validation

Email Validation Failed Alert

Email Validation Failed Alert


We will explore the technique in jQuery to validate the ASP.NET GridView.

Setting up the GridView First

On aspx Page

We will show following fields in GridView.

  • Name – TextBox
  • Age – TextBox
  • Email – TextBox
  • Date of Birth – TextBox (with jQuery DatePicker)
  • Gender – RadioButtonList

For each field, we will have one TemplateField with

  1. ItemTemplate (shows when Grid is loaded) – Inside this, a Label to show the value.
  2. EditItemTemplate (shows when Grid is in Edit Mode) – Inside this, actual TextBox or RadioButtonList.
<asp:GridView ID="gvTestValidations" runat="server" AutoGenerateColumns="false" OnRowEditing="gvTestValidations_RowEditing"
	OnRowUpdating="gvTestValidations_RowUpdating" OnRowCancelingEdit="gvTestValidations_RowCancelingEdit">
	<Columns>
		<asp:TemplateField HeaderText="Name">
			<ItemTemplate>
				<asp:Label ID="lblName" runat="server" Text='<%#Bind("Name") %>'></asp:Label>
			</ItemTemplate>
			<EditItemTemplate>
				<asp:TextBox ID="txtName" runat="server" Text='<%#Bind("Name") %>' />
			</EditItemTemplate>
		</asp:TemplateField>
		<asp:TemplateField HeaderText="Age">
			<ItemTemplate>
				<asp:Label ID="lblAge" runat="server" Text='<%#Bind("Age") %>'></asp:Label>
			</ItemTemplate>
			<EditItemTemplate>
				<asp:TextBox ID="txtAge" runat="server" Text='<%#Bind("Age") %>' />
			</EditItemTemplate>
		</asp:TemplateField>
		<asp:TemplateField HeaderText="Email">
			<ItemTemplate>
				<asp:Label ID="lblEmail" runat="server" Text='<%#Bind("Email") %>'></asp:Label>
			</ItemTemplate>
			<EditItemTemplate>
				<asp:TextBox ID="txtEmail" runat="server" Text='<%#Bind("Email") %>' />
			</EditItemTemplate>
		</asp:TemplateField>
		<asp:TemplateField HeaderText="Date Of Birth">
			<ItemTemplate>
				<asp:Label ID="lblDOB" runat="server" Text='<%#Bind("DOB") %>'></asp:Label>
			</ItemTemplate>
			<EditItemTemplate>
				<asp:TextBox ID="txtDOB" runat="server" Text='<%#Bind("DOB") %>' />
			</EditItemTemplate>
		</asp:TemplateField>
		<asp:TemplateField HeaderText="Gender">
			<ItemTemplate>
				<asp:Label ID="lblGender" runat="server" Text='<%#Bind("Gender") %>'></asp:Label>
			</ItemTemplate>
			<EditItemTemplate>
				<asp:RadioButtonList ID="rblGender" runat="server" SelectedValue='<%#Bind("Gender") %>'>
					<asp:ListItem Value="Male">Male</asp:ListItem>
					<asp:ListItem Value="Female">Female</asp:ListItem>
				</asp:RadioButtonList>
			</EditItemTemplate>
		</asp:TemplateField>
		<asp:CommandField ShowEditButton="true" HeaderText="Edit" />
	</Columns>
</asp:GridView>

On aspx.cs Page

As I am not using Database, so to bind the GridView, we can have one DataTable property stored in ViewState. We will edit, update to the ViewState Table and bind that to the GridView. Inside the Page Load, we will declare hard-coded rows and add them to DataTable. Then bind that to the Grid.

// This ViewState DataTable Property holds the GridView Data.
DataTable dtGridData
{
    get
    {
        return (DataTable)ViewState["dtGridData"];
    }
    set
    {
        ViewState["dtGridData"] = value;
    }
}

protected void Page_Load(object sender, EventArgs e)
{
	if (!IsPostBack)
	{
		// Add Column for the DataTable.
		DataTable dt = new DataTable();
		dt.Columns.Add(new DataColumn("Name"));
		dt.Columns.Add(new DataColumn("Age"));
		dt.Columns.Add(new DataColumn("Email"));
		dt.Columns.Add(new DataColumn("DOB"));
		dt.Columns.Add(new DataColumn("Gender"));

		// Add Rows to DataTable.
		DataRow dr = dt.NewRow();
		dr["Name"] = "Deepak Mahapatra";
		dr["Age"] = 24;
		dr["Email"] = "abc@anything.com";
		dr["DOB"] = "09/28/1990";
		dr["Gender"] = "Male";

		dt.Rows.Add(dr);

		dr = dt.NewRow();
		dr["Name"] = "Dillip Das";
		dr["Age"] = 24;
		dr["Email"] = "def@anything.com";
		dr["DOB"] = "10/02/1990";
		dr["Gender"] = "Male";

		dt.Rows.Add(dr);

		dr = dt.NewRow();
		dr["Name"] = "Tadit Dash";
		dr["Age"] = 24;
		dr["Email"] = "ghi@anything.com";
		dr["DOB"] = "07/05/1990";
		dr["Gender"] = "Male";

		dt.Rows.Add(dr);

		// Assign the DataTable to dtGridData, so that it is stored in ViewState.
		// After that, bind the GridView.
		dtGridData = dt;
		BindGrid();
	}
}

private void BindGrid()
{
	gvTestValidations.DataSource = ViewState["dtGridData"];
	gvTestValidations.DataBind();
}

What’s on Browser?

Initial Load

GridView on Browser

GridView on Browser

On Row Edit Button Click

First Row in Edit Mode

First Row in Edit Mode

Now, the Actual Show Begins !!!

We will do following validation…

  • For Age Field – Only Allow Numbers
  • For Email Field – Email Validation

You can do more validations, you just need to learn how to do it.

So, How to?

Logic is that, inside the document Ready Event, we will try to find all the Age TextBoxes and Email TextBoxes inside the GridView and handle the keydown and change events respectively.

For Age TextBox

The highlighted code lines are important.

$("#gvTestValidations tr input[id*='txtAge']").each(function () {
	$(this).keydown(function (event) {
        // Just to find all non-decimal characters and replace them with blank.
		$(this).val($(this).val().replace(/[^\d].+/, ""));

		var charCode = (event.which) ? event.which : event.keyCode;
		if (charCode > 31 && (charCode < 48 || charCode > 57)) {
			event.preventDefault();

			isValidated = false;
		}
		else { isValidated = true; }
	});
});

The first important line is…

$("#gvTestValidations tr input[id*='txtAge']")

This is actually searching for all the TextBoxes inside the Grid with the help of Attribute Contains Selector [name*=”value”].
Then the following code attaches the keydown Event with all those matched Age TextBoxes.

$(this).keydown(function (event) {
    // Validation code here...
});

For Email TextBox

The same logic is used for Email TextBox. The only difference is that we are handling the change Event here instead of keydown Event.

$("#gvTestValidations tr input[id*='txtEmail']").each(function () {
	$(this).change(function (event) {
		if (!validateEmail($(this).val())) {
			alert("Email not in correct format.");
			$(this).addClass('errorClass');
			event.preventDefault();

			isValidated = false;
		}
		else {
			$(this).removeClass('errorClass');

			isValidated = true;
		}
	});
});

NOTE: The CSS class “errorClass” is used to make Email TextBox Border Red-Coloured, when validation fails.

One more interesting thing…

We are also restricting User to Update one record, if the row is not validated. Suppose, User has typed one wrong Email Id and then directly hits the Update Button, so it won’t allow the User to Update and show one message. For that, we are using the variable isValidated, which you have already seen inside the code.
We are just checking the isValidated value inside the Update Button click and taking action according to that. Again the same logic, which is searching all the links and finds the Button, which is having Text as “Update” and fires its click Event.

$("#gvTestValidations a").each(function () {
	if ($(this).text() == "Update") {
		$(this).click(function (event) {
			if (!isValidated) {
				alert("There are issues with input fields. Please correct and then try.");
				return false;
			}
			else { return true; }
		});
	}
});
Update Button not working if Validation Fails

Update Button not working if Validation Fails

Thanks !!!

For taking time and reading the Blog. Download the Project and play with it. If you find it useful, then share within your circle, by pressing the Social Icons.

Why GridView RowUpdating Event is not giving the updated values?

Debugger Inside GridView RowUpdating Event

Debugger Inside GridView RowUpdating Event


This is a most common question in Forums. We will find the actual cause, for which the RowUpdating Event behaves abnormal.

Walk Through

  1. Add a GridView in aspx page and define all its required properties and Events.
    <asp:GridView ID="gvTestGrid" runat="server" AutoGenerateColumns="false" 
                  OnRowCancelingEdit="gvTestGrid_RowCancelingEdit"
                  OnRowEditing="gvTestGrid_RowEditing" 
                  OnRowUpdating="gvTestGrid_RowUpdating">
        <Columns>
            <asp:BoundField DataField="Column1" HeaderText="Some Column"/>
            <asp:CommandField ShowEditButton="true" />
        </Columns>
    </asp:GridView>
    
  2. Bind the GridView from back end. For our example, we will bind one DataTable.
    private void BindGrid()
    {
        // Add Column for the DataTable.
        DataTable dt = new DataTable();
        dt.Columns.Add(new DataColumn("Column1"));
    
        // Add Rows to DataTable.
        for (int i = 1; i < 5; i++)
        {
    	    DataRow dr = dt.NewRow();
    	    dr["Column1"] = "Old Value" + i;
    	    dt.Rows.Add(dr);
        }
    
        gvTestGrid.DataSource = dt;
        gvTestGrid.DataBind();
    }
  3. Define the GridView Events for Edit and Update operation.
    protected void gvTestGrid_RowEditing(object sender, GridViewEditEventArgs e)
    {
        //Set the edit index.
        gvTestGrid.EditIndex = e.NewEditIndex;
    
        //Bind data to the GridView control.
        BindGrid();
    }
    
    protected void gvTestGrid_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        //Get the new Values.
        GridViewRow row = gvTestGrid.Rows[e.RowIndex];
        string column1Value = ((TextBox)row.Cells[0].Controls[0]).Text;
    
        // Code to update the DataSource.
        //.....
    
        //Reset the edit index.
        gvTestGrid.EditIndex = -1;
    
        //Bind data to the GridView control.
        BindGrid();
    }
    
    protected void gvTestGrid_RowCancelingEdit(object sender, 
                                               GridViewCancelEditEventArgs e)
    {
        //Reset the edit index.
        gvTestGrid.EditIndex = -1;
    
        //Bind data to the GridView control.
        BindGrid();
    }
    
  4. GridView on Browser would look like…

    GridView on Browser

    GridView on Browser

    Now, Let’s Test

    To test the Edit and Update features, let’s click on Edit Button in any Row. We can see that GridView adds TextBoxes in all the cells of that Row immediately. It also shows you Update and Cancel Buttons where it was previously showing Edit Button.
    Let’s do some editing in the cells and click Update Button.

    GridView Cell Showing Updated Value

    GridView Cell Showing Updated Value


    It hits the breakpoint, we have set inside the RowUpdating Event. If we move our mouse on to the variables, which hold the cell values, we can see the old value instead of new updated value (Refer the debugging Screen Shot at the Top). So, we are stuck now, this is the bug.

    What to do now?

    Don’t panic. We need to find out what exactly in code is helping RowUpdating Event to give old values. So, the most wild guess is that, something is causing the Grid to bind again before RowUpdating Event, as we initially did during load.

    If you remember, in Step-2, we did bind the Grid, which is actually called on the Page Load like…

    protected void Page_Load(object sender, EventArgs e)
    {
        BindGrid();
    }
    

    According to the Event Life Cycle, when any Control Event is fired (here Grid RowUpdating), it first goes to Page Load. As it is coming to Page Load, so it calls the Bind method again and re-binds the Grid. Now, it goes to RowUpdating Event, but the updated values are lost because Grid is already reset in Page Load.

    So, How to Resolve?

    IsPostBack() is the answer. We have to check if the Page is posted back or not. If not, then we will bind the Grid.

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGrid();
        }
    }
    

    Go and Share this with your friends

    This issue sometimes kills time. So, don’t wait, just share with everybody you know, who can understand this. Also provide your valuable feedback here. Thanks for reading.

ASP.NET Repeater with jQuery Slider

Image Slider with Repeater

Image Slider with Repeater


In this Blog, we will learn how to integrate one jQuery Slider Plugin with the ASP.NET Repeater Control.

Background

There was a question on implementation of slider with ASP.NET DataList Control and the guy who posted that was facing issues. The issue was actually due to the DataList control, which was rendering as HTML tables breaking the default jQuery Slider functionality. After researching a bit, I found that, it is very easy to implement this with the ASP.NET Repeater control. Let’s go step by step and learn.

Step by Step

Step-1 : Download jQuery Slider Plugin

You can take any jQuery Slider, but you need to see how exactly it allows contents inside it. For this example, we will be using the Elastislide. Hit the “Download Source” button on the page to download the zip file containing the Plugin files. We will try to implement the demo given here (Example 1).

Let’s extract the zip file. You will have something like this inside the Extracted folder.

Extracted jQuery Slider Files

Extracted jQuery Slider Files


So, here we can find all related JavaScript, CSS and images used for the Demos. The demo, which we will implement with Repeater is shown inside the index.html (highlighted in image). You can directly run and see in browser.

Step-2 : Analysis of Demo HTML Page

JavaScript and CSS Files Included

In Head section…

<link rel="stylesheet" type="text/css" href="css/demo.css" />
<link rel="stylesheet" type="text/css" href="css/elastislide.css" />
<link rel="stylesheet" type="text/css" href="css/custom.css" />
<script src="js/modernizr.custom.17475.js"></script>

In Body Section…

<script type="text/javascript" 
        src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js">
</script>
<script type="text/javascript" src="js/jquerypp.custom.js"></script>
<script type="text/javascript" src="js/jquery.elastislide.js"></script>

So, we will also include these files in our aspx page.

HTML Markup for Image Slider

If you open and see the index.html page, you will find a HTML structure which loads all the images. (I have removed the not needed headers and other HTML.)

<div class="container demo-1">
	<div class="main">
		<!-- Elastislide Carousel -->
		<ul id="carousel" class="elastislide-list">
			<li><a href="#"><img src="images/small/1.jpg" alt="image01" /></a></li>
			<li><a href="#"><img src="images/small/2.jpg" alt="image02" /></a></li>
			<li><a href="#"><img src="images/small/3.jpg" alt="image03" /></a></li>
			<li><a href="#"><img src="images/small/4.jpg" alt="image04" /></a></li>
			<li><a href="#"><img src="images/small/5.jpg" alt="image05" /></a></li>
			<li><a href="#"><img src="images/small/6.jpg" alt="image06" /></a></li>
			<li><a href="#"><img src="images/small/7.jpg" alt="image07" /></a></li>
			<li><a href="#"><img src="images/small/8.jpg" alt="image08" /></a></li>
			<li><a href="#"><img src="images/small/9.jpg" alt="image09" /></a></li>
			<li><a href="#"><img src="images/small/10.jpg" alt="image10" /></a></li>
			<li><a href="#"><img src="images/small/11.jpg" alt="image11" /></a></li>
			<li><a href="#"><img src="images/small/12.jpg" alt="image12" /></a></li>
			<li><a href="#"><img src="images/small/13.jpg" alt="image13" /></a></li>
			<li><a href="#"><img src="images/small/14.jpg" alt="image14" /></a></li>
			<li><a href="#"><img src="images/small/15.jpg" alt="image15" /></a></li>
			<li><a href="#"><img src="images/small/16.jpg" alt="image16" /></a></li>
			<li><a href="#"><img src="images/small/17.jpg" alt="image17" /></a></li>
			<li><a href="#"><img src="images/small/18.jpg" alt="image18" /></a></li>
			<li><a href="#"><img src="images/small/19.jpg" alt="image19" /></a></li>
			<li><a href="#"><img src="images/small/20.jpg" alt="image20" /></a></li>
		</ul>
		<!-- End Elastislide Carousel -->
	</div>
</div>

This structure is most important, because the classes added to the HTML controls are used inside the CSS and JavaScript files.
So, we just need to replace the “list items (li)” inside the “Unordered Lists” with Repeater Items, which eventually would look like the same structure and render all the image URLs inside List Items.

Step-3 : Create a Web Application and Add one aspx Page

After adding the aspx page to the Project, now we need to copy the js, css files and images from the “Downloaded Plugin” folders. So, Solution Explorer would look like…

Solution Explorer View

Solution Explorer View


NOTE : We have maintained the exact folder structure as in the Downloaded Plugin. Only “small” folder inside “images” folder is copied with another the “nav.png” image used for “previous next” buttons.

Step-4 : Let’s Design the aspx Page

Basically, we will copy the HTML present in index.html page and update it by adding one Repeater.
Now, the div would look like…

<div class="container demo-1">
	<div class="main">
		<ul id="carousel" class="elastislide-list">
			<asp:Repeater ID="mylist" runat="server">
				<ItemTemplate>
					<li><a href="#">
						<asp:Image ID="Image1" runat="server" ImageUrl='<%# "images/small/" + Eval("image")%>' />
					</a></li>
				</ItemTemplate>
			</asp:Repeater>
		</ul>
	</div>
</div>

:: Here ImageUrl Property is a combination of folders path and image name. Image Name is dynamically bound from the Datasource (explained in the next step), EVAL is used for this purpose only.
Next we will add the JavaScript and CSS files in the Page just like it appears in the index.html Page.
Now, most important thing is to add the following script inside the body.

<script type="text/javascript">
    $('#carousel').elastislide();
</script>

This code actually invokes the Slider to work. carousel is the id of the ul (Unordered list) element in HTML. This elastislide(); method is present in the Plugin js.

Step-5 : Bind the Repeater from Code Behind

protected void Page_Load(object sender, EventArgs e)
{
	if (!Page.IsPostBack)
	{
		BindRepeater();
	}
}

/// <summary>
/// This method binds the Image Slider.
/// </summary>
private void BindRepeater()
{
	DataTable dt = new DataTable();
	dt.Columns.Add("image");

	for (int i = 1; i <= 20; i++)
	{
		dt.Rows.Add(i + ".jpg");
	}

	mylist.DataSource = dt;
	mylist.DataBind();
}

This code is very simple. It binds the Repeater from a DataTable, which contains rows as image names. Image names are 1.jpg, 2.jpg till 19.jpg. You can put your image binding logic here to bind the Repeater.

Feedback Equips Me !!!

Please put your thoughts on the Project by commenting below the Blog. Like and Share, if you liked it.