0

Is there a way to add a date picker to a BoundField in a GridView? I have a very simple Gridview that allows a user to insert, edit/update, and delete vendors from a database table. I was able to use a jQuery script I found to add a date picker to a field that is NOT a BoundField, but I'm having trouble getting it on a BoundField since I cannot specify an Id. Here is the function:

<script type="text/javascript" src="https://code.jquery.com/ui/1.13.2/jquery-ui.js"></script>
<script type="text/javascript">
    $(function () {
        $("[id*=TextBox2]").datepicker({
            showOn: 'button',
            buttonImageOnly: true,
            buttonImage: 'images/calendar.png'
        });
    });
    $(function () {
        $("[id*=effDt]").datepicker({
            showOn: 'button',
            buttonImageOnly: true,
            buttonImage: 'images/calendar.png'
        });
    });
</script>

Here is the ASP code where the date picker DOES work:

                   <td style="font-style: italic;">
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator1"
                            ControlToValidate="TextBox2"
                            Display="Static"
                            ErrorMessage="Effective Date Is Required"
                            runat="server"
                            ValidationGroup="addVendor" /></td>

Here is where I also want to use a date picker on "effDt" but I am not sure how:

    <asp:GridView ID="VendorView" runat="server" DataSourceID="VendorSrc" AutoGenerateColumns="false" AllowPaging="true" AllowSorting="true" HorizontalAlign="Center"
                PageSize="10" BorderColor="#921E33" BorderStyle="Solid" HeaderStyle-BackColor="#921E33" HeaderStyle-ForeColor="White" CellPadding="10" CellSpacing="0" AlternatingRowStyle-BorderStyle="Solid" AlternatingRowStyle-BackColor="White" AlternatingRowStyle-ForeColor="#921E33" Font-Size="Small" RowStyle-BackColor="#63666A" RowStyle-BorderStyle="Solid" RowStyle-BorderColor="Gray" RowStyle-ForeColor="White" SelectedRowStyle-BackColor="#F3D03E" SelectedRowStyle-ForeColor="Black" PagerStyle-BackColor="#921E33" PagerStyle-ForeColor="White" PagerSettings-Mode="NextPreviousFirstLast" PagerSettings-NextPageText="Next Page" PagerStyle-HorizontalAlign="Center" PagerStyle-Width="50%" PagerStyle-CssClass="cssPager" EmptyDataText="No Records Found" AutoGenerateSelectButton="false" PagerSettings-PageButtonCount="10" PagerSettings-PreviousPageText="Previous Page" PagerSettings-FirstPageText="First Page" PagerSettings-LastPageText="Last Page" Width="100%" EmptyDataRowStyle-ForeColor="White" DataKeyNames="vendorId" OnRowDataBound="VendorView_OnRowDataBound">
                <Columns>
                    <asp:BoundField DataField="vendorId" HeaderText="ID" InsertVisible="false" ReadOnly="true" SortExpression="ID" />
                    <asp:BoundField DataField="vendorName" HeaderText="Vendor" SortExpression="Vendor" />
                    <asp:BoundField DataField="effDt" HeaderText="Effective Date" SortExpression="Effective Date" />
                    <asp:BoundField DataField="changeDt" HeaderText="Last Updated" ReadOnly="true" SortExpression="Last Updated" />
                    <asp:CommandField ButtonType="Link" ShowEditButton="true" ShowDeleteButton="true" ItemStyle-Width="100" />
                </Columns>
            </asp:GridView>

Do I need to change to ItemTemplate instead? Or is there a simpler way to just adjust the function (or perhaps a better function to use)?

Thanks!

1 Answer 1

1

Well, the asp.net textbox has a rather nice built-in date picker, and I would suggest using as such.

So, simply drop in a standard asp.net textbox, but set the textmode as date.

Hence, say this markup:

        <asp:TextBox ID="TextBox1" runat="server"
           TextMode="Date" >
        </asp:TextBox>

And the result is this:

enter image description here

Note how it also automatic adds a date "icon" beside the date picker.

So, yes, use a TemplateField, and simply drop in a standard text box.

As such, I see little benefit nor reason to adopt the jQuery.UI date picker when a good working date picker is already built in. There might be some jQuery.UI datepicker options you need, but if you looking for a date picker, the standard built in one should suffice for most of your requirements.

So, say we add this text box to the GridView:

        <asp:TemplateField HeaderText="Booking Date">
            <ItemTemplate>
                <asp:TextBox ID="txtBooking" runat="server"
                    Text='<%# Eval("BookingDate", "{0:d}") %>'
                    TextMode="Date" 
                    ></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>

Note how we "format" the date using the 2nd overload of the Eval() function.

Hence, the above results in this:

enter image description here

7
  • Albert, I am checking this from my phone so I will test this out once I get to my computer but I just wanted to say thank you so much! I had no idea there was a property for this, despite all my googling efforts. I’m new to ASP.NET and even got myself a book and this was not mentioned. I am so grateful! Will let you know once I try it out.
    – Amy
    Commented Jan 16 at 11:47
  • Possible the book is very old. They may have well written the book to be compatible with even older versions of. Net. Webforms is not taught much anymore since it's older technology now. Commented Jan 16 at 16:06
  • you're right. just checked the date. 2002 LOL. I guess I'll get myself a better one. I really just got this one because I wanted to more thoroughly understand postbacks, page lifecycle, various event handlers for each control, etc.
    – Amy
    Commented Jan 16 at 16:16
  • The date picker is working great when I insert a new record, but I get "[SqlException (0x80131904): Cannot insert the value NULL into column 'effDt', table 'CustomWebApps.dbo.LibraryVendors'; column does not allow nulls. UPDATE fails." when I try to edit an existing date in a record.
    – Amy
    Commented Jan 16 at 16:49
  • From my SqlDataSource: UpdateCommand="UPDATE LibraryVendors SET vendorName=@vendorName, effDt=@effDt WHERE vendorId=@vendorID". <UpdateParameters> <asp:Parameter Name="vendorName" Type="String" /> <asp:Parameter Name="effDt" Type="String" /> </UpdateParameters>
    – Amy
    Commented Jan 16 at 16:50

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.