How to make readonly in Gridview textbox calendar controls in Footer Template

BeUnique 2,112 Reputation points
2024-05-31T12:18:46.3566667+00:00

I am developing web based application.

i used gridview and it contains Footer Template for input entry.

almost i done all the functionality.

but, for the validation especially for footer template i need help to solve this.

  1. how to make readonly gridview footer template textbox calendar control
  2. How to validation numeric and decimal on keypress in another textbox for the same.

footer template is new for me and i am also trying the better solution.

if you find any, pls. share the javascript validation (onkeypress) and readonly for date control.

below is my sample design code:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
        <asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false" ShowFooter="true">
            <Columns>
                <asp:TemplateField ItemStyle-HorizontalAlign="Center">
                    <FooterTemplate>
                        <asp:UpdatePanel runat="server" ID="Up_LeaveDetails" UpdateMode="Always">
                            <ContentTemplate>
                                <asp:DropDownList ID="ddlCustomers" Width="100px" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlCustomers_SelectedIndexChanged">
                                </asp:DropDownList>
                                <asp:TextBox ID="txtOtherCustomers" Text="" runat="server" Visible="false"></asp:TextBox>
                            </ContentTemplate>
                            <Triggers>
                                <asp:AsyncPostBackTrigger ControlID="ddlCustomers" EventName="SelectedIndexChanged" />
                            </Triggers>
                        </asp:UpdatePanel>
                    </FooterTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Name">
                    <ItemTemplate>
                        <%# Eval("Name") %>
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
                    </FooterTemplate>
                </asp:TemplateField>

                <asp:TemplateField HeaderText="Country">
                    <ItemTemplate>
                        <%# Eval("Country") %>
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtCountry" runat="server"></asp:TextBox>
                    </FooterTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="DOB">
                    <ItemTemplate>
                    </ItemTemplate>
                    <FooterTemplate>
                        <div>
                            <asp:TextBox ID="txtDOB" runat="server" Text=""></asp:TextBox>
                            <ajaxToolkit:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="txtDOB"
                                Format="MM/dd/yyyy" PopupButtonID="btnDate1"
                                PopupPosition="BottomRight" CssClass="cal_Theme1"></ajaxToolkit:CalendarExtender>
                            <asp:ImageButton ID="btnDate1" ImageUrl="~/images/imgCalendar.png"
                                runat="server" />
                        </div>
                    </FooterTemplate>
                </asp:TemplateField>
 <asp:TemplateField HeaderText="PayAmount">
                    <ItemTemplate>
                        <%# Eval("PayAmount") %>
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtPayAmount" runat="server"></asp:TextBox>
                    </FooterTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Upload">
                    <ItemTemplate>
                        <%# Eval("FileName") %>
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:FileUpload ID="fuUpload" runat="server" />
                    </FooterTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <FooterTemplate>
                        <asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="Add" CommandName="Footer" />
                    </FooterTemplate>
                </asp:TemplateField>
            </Columns>
            <EmptyDataTemplate>
                <tr>
                    <th scope="col">Customer Name</th>
                    <th scope="col">Name</th>
                    <th scope="col">Country</th>
                    <th scope="col">DOB</th>
                    <th scope="col">Upload</th>
                    <th scope="col"></th>
                </tr>
                <tr>
                    <td>
                        <asp:UpdatePanel runat="server" ID="UpdatePanel1" UpdateMode="Always">
                            <ContentTemplate>
                                <asp:DropDownList ID="ddlCustomer" Width="100px" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlCustomers_SelectedIndexChanged">
                                </asp:DropDownList>
                                <asp:TextBox ID="txtOtherCustomer" Text="" runat="server" Visible="false"></asp:TextBox>
                            </ContentTemplate>
                            <Triggers>
                                <asp:AsyncPostBackTrigger ControlID="ddlCustomer" EventName="SelectedIndexChanged" />
                            </Triggers>
                        </asp:UpdatePanel>
                    </td>
                    <td>
                        <asp:TextBox ID="txtName" runat="server" /></td>
                    <td>
                        <asp:TextBox ID="txtCountry" runat="server" />
                    </td>
                    <td>
                        <asp:TextBox ID="txtDOB" CssClass="txtInput" runat="server" Text=""></asp:TextBox>
                        <ajaxToolkit:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="txtDOB"
                            Format="MM/dd/yyyy" PopupButtonID="btnDate1"
                            PopupPosition="BottomRight" CssClass="cal_Theme1"></ajaxToolkit:CalendarExtender>
                        <asp:ImageButton ID="btnDate1" ImageUrl="~/images/imgCalendar.png"
                            runat="server" />
                    </td>
                    <td>
                        <asp:TextBox ID="txtPayAmount" runat="server"></asp:TextBox>
	            <td>
                    </FooterTemplate>
                </asp:TemplateField>
                    <td>
                        <asp:FileUpload ID="fuUpload" runat="server" />
                    </td>
                </tr>
            </EmptyDataTemplate>
        </asp:GridView>
        <table>
            <tr>
                <td>
                    <asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="Add" />
                </td>
            </tr>
        </table>
    </form>
</body>
</html>

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,500 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,994 questions
0 comments No comments
{count} votes

Accepted answer
  1. Lan Huang-MSFT 29,666 Reputation points Microsoft Vendor
    2024-06-03T06:39:38.1533333+00:00

    Hi @Gani_tpt,

    how to make readonly gridview footer template textbox calendar control

    set readonly on Page_Load.

    protected void Page_Load(object sender, EventArgs e)    
    {    
    if (!IsPostBack)    
    {    
     TextBox txtDOB = control.FindControl("txtDOB") as TextBox;
     txtDOB.Attributes.Add("readonly", "readonly");    
    }    
    }  
    

    How to validation numeric and decimal on keypress in another textbox for the same.> But, i am using gridview footer template textbox and i am not sure how to call the above validation script in footer template textbox.

    Change the code to this:

      <script type="text/javascript">
          function checkNumberAndDecimal(val) {           
              var allowedString = /^\d+(\.\d{0,2})?$/; // Allow 2 decimal place with numeric value
              if (allowedString.test(val) == false) {
                  alert("Allow 2 decimal place with numeric value...! " + val);
                
                  $("[id*=gvCustomers]").find("[id*=txtPayAmount]").value = "";
                  $("[id*=gvCustomers]").find("[id*=txtPayAmount]").focus();
                  return false;
              }
          }
     </script>
    

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. AgaveJoe 28,456 Reputation points
    2024-05-31T14:34:53.8866667+00:00

    Your markup up is invalid. The markup editor underlines the error and if not fixed the Web Page will not compile. Please share working code when asking for assistance on the forums.

    How to make readonly gridview footer template textbox calendar control

    The TextBox has a ReadOnly attribute, the calendar extender has an enabled property (inherited from ExtenderControlBase) and the Image control has a Visible property (inherited from Control) .

    You've provided no information as to how read only mode is entered. I can only guess how your logic is intended to function. The best I can do is show how to programmatically set server control properties.

    How to validation numeric and decimal on keypress in another textbox for the same.

    The TextBox control has a TextMode attribute which renders an HTML 5 inputs. Set the attribute to "Number". The user can only enter numeric values including the decimal character.

    Demo code

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GridViewFooter.aspx.cs" Inherits="WebFormsDemo.Gridviews.GridViewFooter" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
            <asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false" ShowFooter="true" OnDataBound="gvCustomers_DataBound">
                <Columns>
                    <asp:TemplateField ItemStyle-HorizontalAlign="Center">
                        <FooterTemplate>
                            <asp:UpdatePanel runat="server" ID="Up_LeaveDetails" UpdateMode="Always">
                                <ContentTemplate>
                                    <asp:DropDownList ID="ddlCustomers" Width="100px" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlCustomers_SelectedIndexChanged">
                                    </asp:DropDownList>
                                    <asp:TextBox ID="txtOtherCustomers" Text="" runat="server" Visible="false"></asp:TextBox>
                                </ContentTemplate>
                                <Triggers>
                                    <asp:AsyncPostBackTrigger ControlID="ddlCustomers" EventName="SelectedIndexChanged" />
                                </Triggers>
                            </asp:UpdatePanel>
                        </FooterTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Name">
                        <ItemTemplate>
                            <%# Eval("Name") %>
                        </ItemTemplate>
                        <FooterTemplate>
                            <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
                        </FooterTemplate>
                    </asp:TemplateField>
    
                    <asp:TemplateField HeaderText="Country">
                        <ItemTemplate>
                            <%# Eval("Country") %>
                        </ItemTemplate>
                        <FooterTemplate>
                            <asp:TextBox ID="txtCountry" runat="server"></asp:TextBox>
                        </FooterTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="DOB">
                        <ItemTemplate>
                        </ItemTemplate>
                        <FooterTemplate>
                            <div>
                                <asp:TextBox ID="txtDOB" runat="server" Text=""></asp:TextBox>
                                
                                    <ajaxToolkit:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="txtDOB"
                                        Format="MM/dd/yyyy" PopupButtonID="btnDate1"
                                        PopupPosition="BottomRight" CssClass="cal_Theme1" ></ajaxToolkit:CalendarExtender>
                                    <asp:ImageButton ID="btnDate1" ImageUrl="~/images/imgCalendar.png" Width="16" Height="16"
                                        runat="server" />
                                
    
                            </div>
                        </FooterTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="PayAmount">
                        <ItemTemplate>
                            <%# Eval("PayAmount") %>
                        </ItemTemplate>
                        <FooterTemplate>
                            <asp:TextBox ID="txtPayAmount" runat="server" TextMode="Number"></asp:TextBox>
                        </FooterTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Upload">
                        <ItemTemplate>
                            <%# Eval("FileName") %>
                        </ItemTemplate>
                        <FooterTemplate>
                            <asp:FileUpload ID="fuUpload" runat="server" />
                        </FooterTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField>
                        <FooterTemplate>
                            <asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="Add" CommandName="Footer" />
                        </FooterTemplate>
                    </asp:TemplateField>
                </Columns>
                <EmptyDataTemplate>
                    <tr>
                        <th scope="col">Customer Name</th>
                        <th scope="col">Name</th>
                        <th scope="col">Country</th>
                        <th scope="col">DOB</th>
                        <th scope="col">Upload</th>
                        <th scope="col"></th>
                    </tr>
                    <tr>
                        <td>
                            <asp:UpdatePanel runat="server" ID="UpdatePanel1" UpdateMode="Always">
                                <ContentTemplate>
                                    <asp:DropDownList ID="ddlCustomer" Width="100px" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlCustomer_SelectedIndexChanged">
                                    </asp:DropDownList>
                                    <asp:TextBox ID="txtOtherCustomer" Text="" runat="server" Visible="false"></asp:TextBox>
                                </ContentTemplate>
                                <Triggers>
                                    <asp:AsyncPostBackTrigger ControlID="ddlCustomer" EventName="SelectedIndexChanged" />
                                </Triggers>
                            </asp:UpdatePanel>
                        </td>
                        <td>
                            <asp:TextBox ID="txtName" runat="server" /></td>
                        <td>
                            <asp:TextBox ID="txtCountry" runat="server" />
                        </td>
                        <td>
                            <asp:TextBox ID="txtDOB" CssClass="txtInput" runat="server" Text=""></asp:TextBox>
                            <ajaxToolkit:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="txtDOB"
                                Format="MM/dd/yyyy" PopupButtonID="btnDate1"
                                PopupPosition="BottomRight" CssClass="cal_Theme1"></ajaxToolkit:CalendarExtender>
                            <asp:ImageButton ID="btnDate1" ImageUrl="~/images/imgCalendar.png"
                                runat="server" />
                        </td>
                        <td>
                            <asp:TextBox ID="txtPayAmount" runat="server"></asp:TextBox>
                            <td></FooterTemplate>
                    <%--</asp:TemplateField>--%>
                                <td>
                                    <asp:FileUpload ID="fuUpload" runat="server" />
                                </td>
                    </tr>
                </EmptyDataTemplate>
            </asp:GridView>
            <table>
                <tr>
                    <td>
                        <asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="Add" />
                    </td>
                </tr>
            </table>
        </form>
        <script>
    
        </script>
    </body>
    </html>
    
    

    Code behind

    using AjaxControlToolkit;
    using System;
    using System.Collections.Generic;
    using System.Globalization;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace WebFormsDemo.Gridviews
    {
        public partial class GridViewFooter : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack) {
                    gvCustomers.DataSource = PopulateDataSource();
                    gvCustomers.DataBind();
                }
            }
    
    
            protected List<ViewModel> PopulateDataSource()
            {
               return new List<ViewModel> {
                        new ViewModel() { CustomerName = "Les Paul", Country="USA", DOB=DateTime.Now.ToShortDateString(), Name="Les", PayAmount=123.45m, FileName="File1"},
                        new ViewModel() { CustomerName = "Leo Fender", Country="USA", DOB=DateTime.Now.ToShortDateString(), Name="Leo", PayAmount=678.90m, FileName="File1"}
                };
            }
    
            protected void ddlCustomer_SelectedIndexChanged(object sender, EventArgs e)
            {
    
            }
    
            protected void ddlCustomers_SelectedIndexChanged(object sender, EventArgs e)
            {
    
            }
    
            protected void Add(object sender, EventArgs e)
            {
    
            }
    
            protected void gvCustomers_DataBound(object sender, EventArgs e)
            {
                GridView grid = (GridView)sender;
                GridViewRow footerRow = grid.FooterRow;
    
                //Set the DOB control to readonly
                TextBox amount = (TextBox)footerRow.FindControl("txtDOB");
                amount.ReadOnly = true;
    
                //Disable the calendar 
                CalendarExtender calendar = (CalendarExtender)footerRow.FindControl("CalendarExtender1");
                calendar.Enabled = false;
    
                Image calendarImage = (Image)footerRow.FindControl("btnDate1");
                calendarImage.Visible = false;
    
            }
        }
    
        public class ViewModel
        {
            public string CustomerName { get; set; }
            public string Name { get; set; }
            public string Country { get; set; }
            public string DOB { get; set; }
            public decimal PayAmount { get; set; }
            public string FileName { get; set; }
        }
    }
    
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.