JavaScript

Show JavaScript Confirmation message while deleting ASP.Net Grid Row

Many a time , we need a confirmation Message on deleting ASP.Net Gird Row . Coz if any User mistakenly clicks the delete button , the data will be gone . So its always the Best to use Confirmation on Deleting .

We will see a demo now how to Show JavaScript/Client Side Confirmation Msg while deleting an ASP.Net Grid Row .

A long time ago ,I had to use more brain processing power to do this requirement ūüôā So I thought to share with all .

Lets see the below image for what I am gonna do :

Show JavaScript Confirmation message while deleting  ASP.Net Grid Row

Show JavaScript Confirmation message while deleting ASP.Net Grid Row

Add this markup inside your ASP.Net Grid .

<asp:TemplateField HeaderText="" ShowHeader="False" HeaderStyle-HorizontalAlign="Left">
                            <ItemTemplate>      
                                      <asp:LinkButton ID="lnkDelete" runat="server" CausesValidation="False" CommandName="Delete"
                                    Text="Delete" OnClientClick="ConfirmDelete(this.id)" ></asp:LinkButton>
                            </ItemTemplate>
                            <HeaderStyle HorizontalAlign="Left" />
                      </asp:TemplateField>

And the JavaScript method implementation of the ConfirmDelete(this.id) is like below –

function ConfirmDelete(RowId)
     {
         var rowNoStr = RowId.split('_');
         rowNo = parseInt(rowNoStr[3]) + 1;              
    
         var confirm_value = document.createElement("INPUT");
         confirm_value.type = "hidden";
         confirm_value.name = "confirm_value";

         var grid = document.getElementById('<%=gvTicketDetails.ClientID%>');

         var delText = "Are you sure to delete - ";
         var ticketNo ="Tk " + grid.rows[rowNo].cells[0].innerText + " : ";
         var ticketTitle = grid.rows[rowNo].cells[1].innerText + " ?";
         var delMsg = delText.concat(ticketNo, ticketTitle);

         var answer = confirm(delMsg);
         if (answer)
         {
             confirm_value.value = "Yes";
         }
         else
         {
             confirm_value.value = "No";
         }
         document.forms[0].appendChild(confirm_value);       
     }

And now using the below code , you can easily get the confirmation value at server side .

protected void gvTicketDetails_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string confirmValue = Request.Form["confirm_value"];
            if (confirmValue.ToUpper() == "YES")
            {
                
            }
        }

You are done ūüôā Cheers

Standard
JavaScript, jQuery, jQuery AJAX

jQuery Ajax in Action with ASP.Net aspx Page

Most of the time , We call our custom WebServices/REST Services/Third Party REST APIs from any jQuery AJAX based HTTP Verb . But we can also call direct ASP.Net Form Page( which is aspx page ) using jQuery AJAX .

Here is how we can call ASPX page from another ASPX page’s AJAX Method.

For this example , I have used two ASPX page –

1.AjaxCheckPage.aspx ::: From this page I am gonna call another ASPX Page
2.jQueryAjaxData.aspx ::: This Page will be called by jQuery AJAX

HTML Markup of my AjaxCheckPage.aspx is like below :

<input type="button" id="ajaxButton" value="Load Data using jQuery Ajax" />   

     <div id="divResult"></div>

Ajax calling code of this Page is —

    <script language="javascript" type="text/javascript">

        $("#ajaxButton").click(function () {

            $.ajax({

                type: "POST",
                url: "jQueryAjaxData.aspx",
                data: "a=2&b=3",
                success: function (data) {
                    $("#divResult").text(data);
                }

            });

        });
    
    </script>

So Now , am doing like – If any Data comes from jQueryAjaxData.aspx , that means after successful , am binding the data to the DIV :

Now lets go another Page jQueryAjaxData.aspx from which the Data is gonna come .

protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Form["a"] != null && Request.Form["b"] != null)
            {
                int a = int.Parse(Request.Form["a"]);
                int b = int.Parse(Request.Form["b"]);
                int sum = a + b;
                
                Response.Write("The Sum is : " + sum);
            }
        }

jQuery Ajax in Action with ASP.Net aspx Page

jQuery Ajax in Action with ASP.Net aspx Page

So you can see , when I click the button then It request a POST to jQueryAjaxData.aspx page and am also passing Form Data as you can see it inside the picture above .

So , this is how , we can call ASPX page from jQuery AJAX POST .

Cheers ūüôā

Standard
Uncategorized

Time Localization using .Net TimeZone API in C#

A time zone is a geographical region in which the same time is used.

You can use the TimeZone class to retrieve information about the current time zone, and to convert times from local time to Coordinated Universal Time (UTC) or vice versa. However, you cannot use the TimeZone class to represent time zones other than the local zone or to handle date and time conversions from one time zone to another. For this purpose, use the TimeZoneInfo class. You can use this class to retrieve information on any time zone defined on the local system, to create custom time zones, and to convert times from one time zone to another.
The TimeZone class supports only a single daylight saving time adjustment rule for the local time zone. As a result, the TimeZone class can accurately report daylight saving time information or convert between UTC and local time only for the period in which the latest adjustment rule is in effect. In contrast, the TimeZoneInfo class supports multiple adjustment rules, which makes it possible to work with historic time zone data.

Lets go to an example .

If you have an Application which will be used in so many Countries and every User comments on a Note in your Site and When Another User logs in , He wants to see the comment Time in His own TimeZone , not in your time Zone . Like If a person from Switzerland comments on a Note at 9.00 AM , after that when I log in I want to see that in my Time Zone Of Bangladesh which should be 1.00 PM . I used same concept few years back in one of My Sites and its awesome ūüôā

Its better to Keep the DateTime Columns as UTC Date and Then convert to User’s own TimeZone By his TimeZoneId using .Net TimeZone API .

Your Site Can have a UI which would be used by The User to Set his Own Time Zone when he gets Registered Into your Site. So That UI can be like below :::

ddlTimeZone

And You can easily populate The Time Zone List DropDown .

Here is Asp.Net markup for that :

<asp:DropDownList ID="ddlTimeZoneId" runat="server">
                </asp:DropDownList>

And Binding DropDown Code will be like :::

private void LoadTimeZoneDropDown()
        {
            ddlTimeZoneId.DataSource = clsTimeZone.GetTimeZones();
            ddlTimeZoneId.DataTextField = "TimeZoneName";
            ddlTimeZoneId.DataValueField = "TimeZoneID";
            ddlTimeZoneId.DataBind();
        }

This clsTimeZone is my custom Class which is using .Net APIs . And I used This below Method to Get the Time Zone List .

public static Collection<structTimeZone> GetTimeZones()
        {
            Collection<structTimeZone> structTimeZoneColl = new Collection<structTimeZone>();

            ReadOnlyCollection<TimeZoneInfo> timeZoneInfos = TimeZoneInfo.GetSystemTimeZones();
            foreach (TimeZoneInfo timeZoneInfo in timeZoneInfos)
            {
                structTimeZoneColl.Add(new structTimeZone { TimeZoneName = timeZoneInfo.DisplayName, TimeZoneID = timeZoneInfo.Id });
            }
            return structTimeZoneColl;
        }

public struct structTimeZone
    {
        public string TimeZoneName { get; set; }
        public string TimeZoneID { get; set; }
    }

From here , User can set his Own Time Zone from the List which .Net Framework or Windows or Microsoft supports.

So your User Table should have Column like this :::

TimeZoneIdColumn

So your Initial Setup is done .

Now When Any User from Switzerland saves Note to Notes Table , then don’t Use Local DateTime rather use UTC Time .

It can be done by using this SQL In your StoredProcedure .

GETUTCDATE()

So your DB Table will hold UTC , so you can easily convert that to any Localized format , to any Time Zone by your TimeZoneId of that User .

So When need to show the Time to UI , just call This method which will return Local time Zone Info . In my Case I did something like this :::

DateTime localTime = clsTimeZone.ConvertToLocalTimeFromUtcTime(utcDate, timeZoneId);

And the implementation of the Method is just simple like below :::

public static DateTime ConvertToLocalTimeFromUtcTime(DateTime utcDate, string timeZoneId)   
        {
            TimeZoneInfo cstZone = TimeZoneInfo.FindSystemTimeZoneById(timeZoneId);
            DateTime cstTime = TimeZoneInfo.ConvertTimeFromUtc(utcDate, cstZone);
            return cstTime;
        }

Just Show that Local Time To The UI .
So you are done ūüôā

Cheers

Standard