jquery datepicker日期在gridview编辑/更新中没有绑定到asp.net文本框(带有母版页)

时间:2010-06-21 15:10:21

标签: c# asp.net jquery gridview datepicker

在下面的代码中,字符串 sFoodFormReceived1 始终获取DateTime.Now的值。我不知道如何使datepicker UI上点击的日期绑定到gridview gvReservationsWithForms中的文本框。

protected void gvReservationsWithForms_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    GridViewRow row = gvReservationsWithForms.Rows[e.RowIndex];
    string sFoodFormReceived1 = ((TextBox)(row.Cells[9].Controls[1])).Text;

    DataTable dt = dsReservationsToBind.Tables[0];


    dt.Rows[gvReservationsWithForms.EditIndex]["FoodFormReceived"] = new DateTime(System.DateTime.Parse(sFoodFormReceived1));
    DataRow dr = dt.Rows[row.DataItemIndex];

    var Reservation = new reservation_RoomReservationTableAdapter();

    Reservation.Update(dr);

    gvReservationsWithForms.EditIndex = -1; 
    gvReservationsWithForms.DataBind();        

}


<asp:Content ID="Content4" ContentPlaceHolderID="Main" Runat="Server">
<script type="text/javascript"> 
     $(document).ready(function () 
       {$("input[id$='tbFoodFormReceivedEditItemTemplate']").datepicker();})
</script>  
   <asp:GridView ID="gvReservationsWithForms" runat="server"     
    AutoGenerateColumns="False" 
    DataKeyNames="RoomReservationID" AutoGenerateSelectButton="True" 
    allowsorting ="True" AutoGenerateEditButton="True" BackColor="White" 
    BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" 
    GridLines="Vertical" onsorting="gvReservationsWithForms_Sorting1" 
    onrowcancelingedit="gvReservationsWithForms_RowCancelingEdit" 
    onrowediting="gvReservationsWithForms_RowEditing" 
    onrowupdating="gvReservationsWithForms_RowUpdating" 
    onselectedindexchanged="gvReservationsWithForms_SelectedIndexChanged1" 
    onrowupdated="gvReservationsWithForms_RowUpdated">
    <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
<columns>
      <asp:BoundField DataField="RoomReservationID" HeaderText="Confirmation ID" SortExpression="RoomReservationID" Visible="false" />
      <asp:BoundField DataField="Room" HeaderText="Room" SortExpression="Room" ReadOnly="true"/>    
       <asp:BoundField DataField="BeginDate" HeaderText="Event Begin Date" SortExpression="BeginDate" ReadOnly="true"/>  
      <asp:BoundField DataField="EndDate" HeaderText="Event End Date" SortExpression="EndDate" ReadOnly="true"/> 
      <asp:BoundField DataField="BeginTime" HeaderText="Event Begin Time" SortExpression="BeginTime" ReadOnly="true"/> 
      <asp:BoundField DataField="EndTime" HeaderText="Event End Time" SortExpression="EndTime" ReadOnly="true"/> 
      <asp:BoundField DataField="Department" HeaderText="Department/Organization" SortExpression="Department" ReadOnly="true"/>      
      <asp:BoundField DataField="Activity" HeaderText="Activity"  SortExpression="Activity" ReadOnly="true"/>    
       <asp:TemplateField HeaderText="Food Form Received" 
          SortExpression="FoodFormReceived">
           <EditItemTemplate>
               <asp:TextBox ID="tbFoodFormReceivedEditItemTemplate" runat="server"  CssClass="datepickers"
                   Text='<%# DateTime.Now%>'>  </asp:TextBox>
           </EditItemTemplate>
           <ItemTemplate>
               <asp:Label ID="Label1" runat="server" Text='<%# Bind("FoodFormReceived") %>'></asp:Label>
           </ItemTemplate>
      </asp:TemplateField>
      </columns>
    <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
    <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
    <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
    <AlternatingRowStyle BackColor="#DCDCDC" />
</asp:GridView>
</asp:Content>

1 个答案:

答案 0 :(得分:1)

您可能希望在代码中将文本框设置为datetime.now,而不是在RowEditing上。这样,绑定不会干扰。可能是......

另外,如果你这样做,你可能会获得更好的表现:

$("#<%= gvReservationsWithForms.ClientID %>")
   .find("input[id$='tbFoodFormReceivedEditItemTemplate']").datepicker();

相反,您将输入控件仅限制为gridview,而不是整个页面。