26 May 2014

Build Master Detail GridView using jQuery in ASP.Net

In this article I will explain how to build Master / Detail GridView using jQuery in ASP.Net.
The detail GridView can be expanded and collapsed using jQuery when button is clicked in the Master GridView.
Nested GridViews Example i.e. GridView inside GridView with Expand Collapse


Database
I’ll make use of Customers and Orders Table of Microsoft’s Northwind Database which you can easily download using the link provided below
 
HTML Markup
The HTML Markup contains a simple ASP.Net GridView with a child ASP.Net GridView in the ItemTemplate of TemplateField of ASP.Net GridView
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false" CssClass="Grid"
    DataKeyNames="CustomerID" OnRowDataBound="OnRowDataBound">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <img alt = "" style="cursor: pointer" src="images/plus.png" />
                <asp:Panel ID="pnlOrders" runat="server" Style="display: none">
                    <asp:GridView ID="gvOrders" runat="server" AutoGenerateColumns="false" CssClass = "ChildGrid">
                        <Columns>
                            <asp:BoundField ItemStyle-Width="150px" DataField="OrderId" HeaderText="Order Id" />
                            <asp:BoundField ItemStyle-Width="150px" DataField="OrderDate" HeaderText="Date" />
                        </Columns>
                    </asp:GridView>
                </asp:Panel>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField ItemStyle-Width="150px" DataField="ContactName" HeaderText="Contact Name" />
        <asp:BoundField ItemStyle-Width="150px" DataField="City" HeaderText="City" />
    </Columns>
</asp:GridView>
 
 
Binding the Customers records to the Parent GridView
Below is the code to bind the parent ASP.Net GridView with the records of Customers table from the Northwind Database.
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        gvCustomers.DataSource = GetData("select top 10 * from Customers");
        gvCustomers.DataBind();
    }
}
 
private static DataTable GetData(string query)
{
    string strConnString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(strConnString))
    {
        using (SqlCommand cmd = new SqlCommand())
        {
            cmd.CommandText = query;
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.Connection = con;
                sda.SelectCommand = cmd;
                using (DataSet ds = new DataSet())
                {
                    DataTable dt = new DataTable();
                    sda.Fill(dt);
                    return dt;
                }
            }
        }
    }
}
 
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgsHandles Me.Load
    If Not IsPostBack Then
        gvCustomers.DataSource = GetData("select top 10 * from Customers")
        gvCustomers.DataBind()
    End If
End Sub
 
Private Shared Function GetData(query As StringAs DataTable
    Dim strConnString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Using con As New SqlConnection(strConnString)
        Using cmd As New SqlCommand()
            cmd.CommandText = query
            Using sda As New SqlDataAdapter()
                cmd.Connection = con
                sda.SelectCommand = cmd
                Using ds As New DataSet()
                    Dim dt As New DataTable()
                    sda.Fill(dt)
                    Return dt
                End Using
            End Using
        End Using
    End Using
End Function
 
 
Binding the Child GridView with the Orders for each Customer in the Parent GridView
On the RowDataBound event of the Parent GridView I am first searching the Child GridView in the corresponding GridView Row and then populating it with the records from the Orders table of the Northwind Database based on Customer Id stored in the DataKey property.
Note:GetData is a generic function and the same function discussed above is used here.
C#
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        string customerId = gvCustomers.DataKeys[e.Row.RowIndex].Value.ToString();
        GridView gvOrders = e.Row.FindControl("gvOrders"as GridView;
        gvOrders.DataSource = GetData(string.Format("select top 3 * from Orders where CustomerId='{0}'", customerId));
        gvOrders.DataBind();
    }
}
 
VB.Net
Protected Sub OnRowDataBound(sender As Object, e As GridViewRowEventArgs)
    If e.Row.RowType = DataControlRowType.DataRow Then
        Dim customerId As String = gvCustomers.DataKeys(e.Row.RowIndex).Value.ToString()
        Dim gvOrders As GridView = TryCast(e.Row.FindControl("gvOrders"), GridView)
        gvOrders.DataSource = GetData(String.Format("select top 3 * from Orders where CustomerId='{0}'", customerId))
        gvOrders.DataBind()
    End If
End Sub
 
 
Client side Expand Collapse functionality using jQuery and JavaScript
For Expand and Collapse of the Child GridViews I have made use of jQuery
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
    $("[src*=plus]").live("click"function () {
        $(this).closest("tr").after("<tr><td></td><td colspan = '999'>" + $(this).next().html() + "</td></tr>")
        $(this).attr("src""images/minus.png");
    });
    $("[src*=minus]").live("click"function () {
        $(this).attr("src""images/plus.png");
        $(this).closest("tr").next().remove();
    });
</script>

Dynamically change the error message of ASP.Net CustomValidator using JavaScript


In this article I will explain how to dynamically change the error message of ASP.Net Custom Validator using JavaScript.
In order to illustrate the functioning with an example, I have created a simple form where a person has to enter either Phone Number or Cell Number and based on what he chooses the validation message of the ASP.Net CustomValidator is dynamically changed using JavaScript.
 
HTML Markup
In the below HTML Markup I have a RadioButtonList to allow user choose whether he has to enter Phone Number or Cell Number, and there is a TextBox for each of  the respective Phone and Cell Number and a CustomValidator for which I have set the ErrorMessage property as blank string.
<table cellpadding="2" cellspacing="2">
<tr>
    <td>
        <asp:RadioButton ID="rbPhoneNumber" Text="Phone Number" runat="server" GroupName="ContactNumber" />
    </td>
    <td>
        <asp:TextBox ID="txtPhoneNumber" runat="server" />
    </td>
</tr>
<tr>
    <td>
        <asp:RadioButton ID="rbCellNumber" Text="Cell Number" runat="server" GroupName="ContactNumber" />
    </td>
    <td>
        <asp:TextBox ID="txtCellNumber" runat="server" />
    </td>
</tr>
<tr>
    <td colspan="2">
        <asp:CustomValidator runat="server" Display="Dynamic"
            ForeColor="Red" ClientValidationFunction="ContactNumberValidation" ErrorMessage=""></asp:CustomValidator>
    </td>
</tr>
<tr>
    <td>
    </td>
    <td>
        <asp:Button ID="btnSave" runat="server" Text="Save" />
    </td>
</tr>
</table>
 
 
Dynamically changing the error message of ASP.Net Custom Validator using JavaScript
Below is the client side JavaScript validation function which is executed by the CustomValidator.
Note: The CustomValidator is rendered as HTML SPAN and hence I am making use of innerHTML property to set the error message.
Initially I am setting a generic error message which will be raised when user has not chosen the phone number type from the RadioButtonList.
Then based on what he chooses i.e. Phone number or Cell number he will see the respective error message when he leaves the corresponding TextBox empty.
<script type="text/javascript">
function ContactNumberValidation(sender, args) {
    sender.innerHTML = "Please select at least one contact number.";
    if (document.getElementById("<%=rbPhoneNumber.ClientID %>").checked) {
        sender.innerHTML = "Please enter your phone number.";
        args.IsValid = document.getElementById("<%=txtPhoneNumber.ClientID %>").value != ""
    } else if (document.getElementById("<%=rbCellNumber.ClientID %>").checked) {
        sender.innerHTML = "Please enter your cell number.";
        args.IsValid = document.getElementById("<%=txtCellNumber.ClientID %>").value != ""
    } else {
        args.IsValid = false;
    }
}
</script>