How to maintain Ajax calendar values after postback in Gridview Footer

Gani_tpt 2,026 Reputation points
2024-05-29T15:22:30.55+00:00

I am using AJAX control textbox in gridview.

Inside the gridview, Footer Template i am using dropdown control and many textboxes.

after selecting of Ajax calendar control, if i select dropdown, the ajax value getting cleared.

How to maintain the date even postback happens...?

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,364 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,483 questions
{count} votes

Accepted answer
  1. Lan Huang-MSFT 27,796 Reputation points Microsoft Vendor
    2024-05-31T02:49:01.1966667+00:00

    Hi @Gani_tpt,

    Sorry, I only tested FooterTemplate last time and forgot to test EmptyDataTemplate. You can implement the same code in EmptyDataTemplate as FooterTemplate, but you need to delete the outermost UpdatePanel.

    Since the IDs of the dropdownlist and text box in EmptyDataTemplate and FooterTemplate are different, the relevant content of the backend code also needs to be modified. The specific code is as follows:

    <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="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: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>
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            this.BindGrid();
            Control control = null;
            DropDownList DropDownList1 = null;
            if (gvCustomers.FooterRow != null)
            {
                control = gvCustomers.FooterRow;
                DropDownList1 = (control.FindControl("ddlCustomers") as DropDownList);
            }
            else
            {
                control = gvCustomers.Controls[0].Controls[0];
                DropDownList1 = (control.FindControl("ddlCustomer") as DropDownList);
            }
            List<Item> items = new List<Item>();
            items.Add(new Item() { Value = "1", Text = "USA" });
            items.Add(new Item() { Value = "2", Text = "UK" });
            items.Add(new Item() { Value = "3", Text = "AUS" });
            items.Add(new Item() { Value = "4", Text = "OTHERS" });
            DropDownList1.DataSource = items;
            DropDownList1.DataTextField = "Text";
            DropDownList1.DataValueField = "Value";
            DropDownList1.DataBind();
            DropDownList1.Items.Insert(0, new ListItem("--Select --", "0"));
        }
    }
    private void BindGrid()
    {
        string constr = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
        string query = "SELECT Name, Country,FileName FROM Customers";
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand(query, con))
            {
                using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
                {
                    using (DataTable dt = new DataTable())
                    {
                        sda.Fill(dt);
                        con.Open();
                        gvCustomers.DataSource = dt;
                        gvCustomers.DataBind();
                        con.Close();
                    }
                }
            }
        }
    }
    protected void ddlCustomers_SelectedIndexChanged(object sender, EventArgs e)
    {
        Control control = null;
        DropDownList ddlCustomers = null;
        TextBox txtOtherCustomers = null;
        if (gvCustomers.FooterRow != null)
        {
            control = gvCustomers.FooterRow;
            ddlCustomers = (control.FindControl("ddlCustomers") as DropDownList);
            txtOtherCustomers = control.FindControl("txtOtherCustomers") as TextBox;
        }
        else
        {
            control = gvCustomers.Controls[0].Controls[0];
            ddlCustomers = (control.FindControl("ddlCustomer") as DropDownList);
            txtOtherCustomers = control.FindControl("txtOtherCustomer") as TextBox;
        }
        if (ddlCustomers.SelectedItem.Text == "OTHERS")
        {
            txtOtherCustomers.Visible = true;
        }
        else
        {
            txtOtherCustomers.Visible = false;
        }
    }
    public class Item
    {
        public Item() { }
        public string Value { set; get; }
        public string Text { set; get; }
    }
    protected void Add(object sender, EventArgs e)
    {
    }
    

    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.

0 additional answers

Sort by: Most helpful