Loupi
Loupi

Reputation: 1112

Automatically post current date in gridview Editmode ASP.NET C#

Is there a way were I could set the current date in one of the field while in edit mode in gridview? What I want to do is when a user click on a boolean property (true or false check box field) in gridview it will automatically set the current date in the 'datereturned' field.

Here's a sample of my code in gridview.

<asp:GridView ID="GridView2" runat="server" AllowPaging="True" 
            AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="lenid,bookid, booktitle, EmployeeID" 
            DataSourceID="currentborrowersDataSource">
            <Columns>
                <asp:CommandField HeaderStyle-Width="140px" ButtonType="Button" ShowDeleteButton="False" ShowEditButton="True" />

                <asp:BoundField DataField="lenid" HeaderText="lenid" InsertVisible="False" 
                    ReadOnly="True" Visible="false" SortExpression="lenid" />

                <asp:BoundField DataField="bookid" HeaderText="bookid" 
                    ReadOnly="True" Visible="false" SortExpression="bookid" />

                <asp:BoundField DataField="booktitle" HeaderText="Book Title" 
                    ReadOnly="true" SortExpression="booktitle" />

                <asp:BoundField DataField="EmployeeID" HeaderText="Employee ID" 
                    ReadOnly="true" SortExpression="EmployeeID" />

                <asp:BoundField DataField="department" HeaderText="Department" 
                    ReadOnly="true" SortExpression="department" />

                <asp:BoundField DataField="dateborrowed" HeaderText="Date borrowed" 
                    SortExpression="dateborrowed" />

                <asp:BoundField DataField="datereturned" HeaderText="Date returned" 
                    SortExpression="datereturned" />

                <asp:CheckBoxField DataField="returned" HeaderText="Returned" SortExpression="flag" />

            </Columns>
        </asp:GridView>

Any help would be much appreciated. Thanks in advance!!

Upvotes: 0

Views: 4422

Answers (2)

Muhammad Akhtar
Muhammad Akhtar

Reputation: 52241

You need to convert it to template control and put the TextBox control in it like:..

<asp:TemplateField>
      <asp:TextBox runat="server" ID="textbox1" Text='<%# DateTime.Now.ToString() %>'>
            </asp:TextBox>
    </asp:TemplateField>

Upvotes: 1

Naveed Butt
Naveed Butt

Reputation: 2901

You would have to handle the preRender event of GridView and loop over the rows and add the date in the textbox manually for each row...

Upvotes: 0

Related Questions