如何在编辑模式ASP .Net中将日期选择器放在Gridview中

时间:2013-12-09 02:50:48

标签: c# javascript asp.net gridview datepicker

我想在编辑模式下将网格视图中的默认文本字段更改为javascript日期选择器吗?我该如何编码?

下面的代码(Gridview):

<asp:GridView id="grdViewEmpList" runat="server" AutoGenerateColumns="false" PageSize="5" allowpaging="true" Font-Size="11px" OnRowDataBound="grdViewEmpList_RowDataBound"
            GridLines="Both" ShowFooter="false" CssClass="gridview" CellPadding="4" Width="750px" OnRowEditing="grdViewEmpList_RowEditing"
            PagerSettings-Mode="Numeric" AllowSorting="true" autopostback="true" ShowHeaderWhenEmpty="True" OnRowCancelingEdit="grdViewEmpList_RowCancelingEdit"
            EmptyDataText="No record found" OnRowCreated="grdViewEmpList_RowCreated" OnPageIndexChanging="grdViewEmpList_PageIndexChanging" OnRowUpdating="grdViewEmpList_RowUpdating">
        <RowStyle Wrap="true" ForeColor="#666666" BorderColor="#FFFFFF" BorderWidth="0" BackColor="#CCCCFF" CssClass="gridview_alter"/>
        <AlternatingRowStyle Wrap="true" ForeColor="#666666" BorderColor="#CCCCCC" BorderWidth="0" BackColor="#FFFFFF" CssClass="gridview_alter"/>
        <Columns>
            <asp:CommandField ShowEditButton="True" EditText="Edit" UpdateText="Save" ButtonType="Image" ItemStyle-HorizontalAlign="Center" HeaderText="Action" EditImageUrl="~/Images/dotNet/pencil.png" CancelImageUrl="~/Images/dotNet/edit-not-validated-icon.png" UpdateImageUrl="~/Images/dotNet/edit-validated-icon.png"/>
            <asp:BoundField DataField="EMP_ID" readonly="true" ItemStyle-Height="20px" HeaderText="Employee ID" HeaderStyle-CssClass="allWidth100" />
            <asp:BoundField DataField="NAME" readonly="true" HeaderText="Name" HeaderStyle-CssClass="allWidth460" />
            <asp:BoundField DataField="EFF_DATE" HeaderText="Effective Date" HeaderStyle-CssClass="allWidth100" DataFormatString="{0:d}" ItemStyle-HorizontalAlign="Center" ControlStyle-CssClass="txtCommon allWidth80 txtCenter" />
        </Columns>
        <PagerSettings Mode="Numeric" PageButtonCount="5" Position="Bottom" />
        <PagerStyle ForeColor="#FFCC33" HorizontalAlign="left" CssClass="gridview_pager"/>
    </asp:GridView>

HTML格式的日期选择器代码,我想在EFF_DATE列的gridview上面放置:

<input type="text" id="txtEffDate" name="txtEffDate" class="txtCommon allWidth80" value="" readonly="readonly" />
<img alt="Calendar" src="../Images/DateTimePickerImg/cal.gif" style="cursor: pointer;" onclick="javascript: NewCssCal('txtEffDate')" />

3 个答案:

答案 0 :(得分:3)

在这种情况下使用<asp:TemplateField>

数据绑定控件(如GridView)使用BoundField将字段的值显示为文本。因此,在使用TemplateField时,您也可以使用Label作为相同的目的,[即使用模板定义列的布局时]

因此,更改字段的标记: EFF_DATE 来自

 <asp:BoundField DataField="EFF_DATE" HeaderText="Effective Date" 
      HeaderStyle-CssClass="allWidth100" DataFormatString="{0:d}" 
      ItemStyle-HorizontalAlign="Center" 
      ControlStyle-CssClass="txtCommon allWidth80 txtCenter" />

到此:

 <asp:TemplateField>
    <ItemTemplate>
        <asp:Label runat="server" ID="lnl1" 
          Text='<%#DataBinder.Eval(Container.DataItem, "EFF_DATE").ToString()%>'>
        </asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
          <input type="text" id="txtEffDate" name="txtEffDate" 
                 class="txtCommon allWidth80" value="" readonly="readonly" />
          <img alt="Calendar" src="../Images/spain.png" style="cursor: pointer;"
               onclick="javascript: NewCssCal('txtEffDate')" />
    </EditItemTemplate>
  </asp:TemplateField>

答案 1 :(得分:2)

您可以使用TemplateField在editmode中添加日期选择器。将自定义类添加到文本框

var audio = document.getElementById('audio_1');
setTimeout(function() {
  audio.play();
}, 3000);

通过使用gridview ID,您可以将datepicker绑定到文本框。

<asp:TemplateField HeaderText="Effective Date" SortExpression="EFF_DATE" >
    <ItemTemplate>
        <asp:Label ID="lblEffDate" runat="server" Text='<%#Bind("EFF_DATE") %>' />
    </ItemTemplate>
    <EditItemTemplate>
         <asp:TextBox ClientIDMode="Static" ID="txtEffDate" runat="server" Text='<%#Bind("EFF_DATE") %>'  CssClass="Datepicker"/>
    </EditItemTemplate>
</asp:TemplateField>    

答案 2 :(得分:1)

您可以添加日期选择器以使用TemplateField,如下所示。

            <asp:TemplateField HeaderText="Effective Date" 
                SortExpression="EFF_DATE" >
                <ItemTemplate>
                    <asp:Label ID="lblEffDate" runat="server" Text='<%#Bind("EFF_DATE") %>' />
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ClientIDMode="Static" ID="txtEffDate" runat="server" Text='<%#Bind("EFF_DATE") %>' />
                    <img alt="Calendar" src="../Images/DateTimePickerImg/cal.gif" style="cursor: pointer;" onclick="javascript: NewCssCal('txtEffDate')" />
                </EditItemTemplate>
            </asp:TemplateField>