locked
Why is the change in an html input textbox not being detected in asp.net? RRS feed

  • Question

  • User-618475340 posted

    I have a repeater, one columm of which contains a textbox on which is attached a JQuery datepicker along with an update button. So a user uses the datepicker to change the date, clicks update and it writes to the database.The original date and the record id are stored in hidden fields.

    <asp:Repeater ID="Repeater1" runat="server">
    <ItemTemplate>
    <tr>
    <td>
    <asp:HiddenField ID="hidThisID" Value='<%# Eval("orderID") %>' runat="server" />
    <asp:HiddenField ID="hidPrevDueDate" Value='<%# Eval("Duebeforedate", "{0:dd/MM/yyyy}") %>' runat="server" />
    <input type="text" class="form-control fc-datepicker duedateinput" style="width: 150px" value='<%# Eval("Duebeforedate", "{0:dd/MM/yyyy}") %>'  runat="server" id="fccd" readonly />
    <asp:LinkButton ID="btnDateUpdate" OnClick="btnDateUpdate_Click" CssClass="btn btn-primary btn-sm" runat="server" ToolTip="Approved"> Update </asp:LinkButton>
    </td>
    </tr>
    </ItemTemplate>
    </asp:Repeater>

    Which calls this function - 

    protected void btnDateUpdate_Click(object sender, EventArgs e)
        {
            foreach (RepeaterItem item in Repeater1.Items)
            {
                HtmlInputText htmlDueDate = (HtmlInputText)item.FindControl("fccd");
                HiddenField hidID = (HiddenField)item.FindControl("hidThisID");
                HiddenField hidOldDate = (HiddenField)item.FindControl("hidPrevDueDate");
                DateTime prevDueDate = Convert.ToDateTime(hidOldDate.Value.ToString());
                DateTime newDueDate = Convert.ToDateTime(htmlDueDate.Value.ToString());
                string ID = hidID.Value;
                if (prevDueDate != newDueDate)
                {
                    string query = "update [tblOrders] set Duebeforedate=CONVERT(datetime,'" + newDueDate.ToString() + "', 103) where  [orderID] =  '" + ID + "'";
                    
                    //database stuff here
                }
            }
        }

    However, the newDueDate variable still holds the original due date. Consequently the old and new dates match and so the database doesn't get updated. How do I get it to store the new value?

    Saturday, March 27, 2021 9:02 PM

Answers

  • User-1716253493 posted

    Ensure, the repeater is not rebind every postback

    if (!IsPostBack)
    {
         //bind the repeater
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, March 28, 2021 5:33 AM

All replies

  • User-1545767719 posted

    Please use Bind method instead of Eval method.

    Saturday, March 27, 2021 10:52 PM
  • User-618475340 posted

    I did change it to 

    <input type="text" 
    class="form-control fc-datepicker duedateinput" 
    style="width: 150px"
    value='<%# Bind("Duebeforedate", "{0:dd/MM/yyyy}") %>'
    runat="server" 
    id="fccd"
    readonly />

    But the same occurs, newDueDate holds the original value as in prevDueDate

    Saturday, March 27, 2021 11:59 PM
  • User-1545767719 posted

    <input type="text" 
    class="form-control fc-datepicker duedateinput" 
    style="width: 150px"
    value='<%# Bind("Duebeforedate", "{0:dd/MM/yyyy}") %>'
    runat="server" 
    id="fccd"
    readonly />

    Remove readonly and try again.

    Sunday, March 28, 2021 1:02 AM
  • User-618475340 posted

    Makes no difference. Besides it needs to be readonly so they have to use the datepicker.

    Sunday, March 28, 2021 1:14 AM
  • User-1545767719 posted

    I cannot reproduce your problem with the following sample similar to yours. I guess that something not shown in your code causes the issue.

    .aspx.cs

    using System;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    
    namespace WebApplication2
    {
        public partial class RepeaterDatePicker : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
    
            protected void Button1_Click(object sender, EventArgs e)
            {
                foreach (RepeaterItem item in Repeater1.Items)
                {
                    HtmlInputText htmlDueDate = (HtmlInputText)item.FindControl("fccd");
                    DateTime newDueDate = Convert.ToDateTime(htmlDueDate.Value.ToString());
                }
            }
        }
    }

    .aspx

    <%@ Page Language="C#" AutoEventWireup="true" 
        CodeBehind="RepeaterDatePicker.aspx.cs" 
        Inherits="WebApplication2.RepeaterDatePicker" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <title></title>
        <script src="Scripts/jquery-3.4.1.js"></script>
        <script src="Scripts/jquery-ui-1.12.1.js"></script>
        <link href="Content/themes/base/jquery-ui.css" rel="stylesheet" />
        <script type="text/javascript">
            //<![CDATA[
            $(function () { 
                $('table input:text[id*=fccd]')
                .datepicker(); 
            });
    //]]>
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:SqlDataSource ID="SqlDataSource1" 
                    runat="server" 
                    ConnectionString="<%$ ConnectionStrings:NORTHWINDConnectionString %>" 
                    SelectCommand="SELECT TOP 1 [OrderID], [CustomerID], [OrderDate] FROM [Orders]">
                </asp:SqlDataSource>
                <asp:Repeater ID="Repeater1" 
                    runat="server" 
                    DataSourceID="SqlDataSource1">
                    <HeaderTemplate>
                        <table>
                            <tr>
                                <th>OrderID</th>
                                <th>CustomerID</td>
                                <th>OrderDate</th>
                            </tr>
                    </HeaderTemplate>
                    
                    <ItemTemplate>
                        <tr>
                            <td><%# Eval("OrderID")%></td>
                            <td><%# Eval("CustomerID")%></td>
                            <td>
                                <input type="text"
                                    style="width: 150px"
                                    value='<%# Bind("OrderDate", "{0:dd/MM/yyyy}") %>'
                                    runat="server"
                                    id="fccd"
                                    readonly />
                            </td>
                        </tr>
                    </ItemTemplate>
    
                    <FooterTemplate>
                        </table>
                    </FooterTemplate>
                </asp:Repeater>
            </div>
            <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
        </form>
    </body>
    </html>



     

    Sunday, March 28, 2021 2:35 AM
  • User-1716253493 posted

    Ensure, the repeater is not rebind every postback

    if (!IsPostBack)
    {
         //bind the repeater
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, March 28, 2021 5:33 AM
  • User-618475340 posted

    Brilliant, that's fixed it.

    Sunday, March 28, 2021 4:48 PM