更新Gridview将文本框解析为DateTime

时间:2014-05-17 15:53:34

标签: c# asp.net datetime gridview updating

我在editTemplate中使用ajax日历扩展器来获取gridview。更新时,我收到一条消息,表明数据库无法更新,因为String未被识别为有效的DateTime。我认为它混合了一个月,因为它适用于本月12日以下的日期。如何将文本解析为日期时间以使更新起作用?

 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
                    DataKeyNames="dogId,treatmentId" DataSourceID="VaxDataSource" CssClass="table" >
                    <Columns>
                        <asp:CommandField ShowEditButton="True" EditText="edit" UpdateText="update" />

                        <asp:TemplateField HeaderText="reminder" SortExpression="reminderDate">
                            <EditItemTemplate>
                                <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("reminderDate") %>'></asp:TextBox>
                                <asp:CalendarExtender ID="TextBox1_CalendarExtender" runat="server" 
                                    TargetControlID="TextBox1">
                                </asp:CalendarExtender>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="Label1" runat="server" Text='<%# Bind("reminderDate") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:BoundField DataField="treatmentDate" HeaderText="treatment" 
                            SortExpression="treatmentDate" readOnly="false"/>
                        <asp:TemplateField HeaderText="treatment" SortExpression="recievedDate">
                            <EditItemTemplate>
                                <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("recievedDate") %>'></asp:TextBox>
                                <asp:CalendarExtender ID="TextBox2_CalendarExtender" runat="server" 
                                    TargetControlID="TextBox2">
                                </asp:CalendarExtender>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="Label2" runat="server" Text='<%# Bind("recievedDate") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:BoundField DataField="description" HeaderText="treatment" 
                            SortExpression="description" readOnly="true"/>
                    </Columns>
                </asp:GridView>
                <asp:SqlDataSource ID="VaxDataSource" runat="server" 
                    ConnectionString="<%$ ConnectionStrings:dogDBConnectionString %>" 
                    SelectCommand="SELECT Treatment2Dog.dogId, Treatment2Dog.treatmentId, Treatment2Dog.reminderDate, Treatment2Dog.treatmentDate, Treatment2Dog.recievedDate, Medical_Treatment.description FROM Medical_Treatment INNER JOIN Treatment2Dog ON Medical_Treatment.id = Treatment2Dog.treatmentId WHERE (Treatment2Dog.dogId = @dogID)" 
                    UpdateCommand="UPDATE Treatment2Dog SET reminderDate = @reminderDate, recievedDate = @recievedDate WHERE (dogId = @dogID) AND (treatmentId = @treatmentId)">
                    <SelectParameters>
                        <asp:QueryStringParameter Name="dogID" QueryStringField="dogid" />
                    </SelectParameters>
                    <UpdateParameters>
                        <asp:Parameter Name="reminderDate" Type="DateTime" />
                        <asp:Parameter Name="recievedDate" Type="DateTime" />
                        <asp:Parameter Name="dogID" />
                        <asp:Parameter Name="treatmentId" />
                    </UpdateParameters>
                </asp:SqlDataSource>

2 个答案:

答案 0 :(得分:0)

如果您使用的是AjaxEntender,那么您也必然会使用ScriptManager。确保脚本管理器设置了以下两个参数:

EnableScriptGlobalization="true"EnableScriptLocalization="true"'所以看起来像这样:

<ajaxToolkit:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server" EnableScriptGlobalization="true"
    EnableScriptLocalization="true">
</ajaxToolkit:ToolkitScriptManager>

如果您需要进一步的帮助,请发布整个标记块,以便我们查看周围的控件。

答案 1 :(得分:0)

尝试在gridview上添加RowUpdating事件的处理程序:

OnRowUpdating="GridView1_RowUpdating"

在处理程序功能中,您可以将treatmentDatereminder字段的值设置为正确的格式。

查看此信息以获取更多信息:http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowupdating(v=vs.110).aspx