有没有其他方法可以动态添加新行并将内容提交到gridview以外的数据库?

时间:2014-07-03 20:47:17

标签: c# javascript jquery asp.net gridview

我目前正在使用以下代码动态地向Gridview添加新行并成功提交将内容提交到数据库。

    <script type="text/javascript">

        $(function () {

            $("[id$=txtdate]").datepicker({

                showOn: 'button',

                buttonImageOnly: true,

                buttonImage: 'images/20/calendar200.gif'

            });

        });

    </script>
    <script type='text/javascript'>//<![CDATA[
     $(window).load(function () {
         //Attach click event to button
         $('#btnSave').on('click', function () {
             //Get Values from dropdownlist
             var sHour = $('#startHour').val();
             var sMinutes = $('#startMinutes').val();
             var sAmPm = $('#startAmPm').val();

             var eHour = $('#endHour').val();
             var eMinutes = $('#endMinutes').val();
             var eAmPm = $('#endAmPm').val();

             //create date format from dropdownlist selected values 
             var theDate = $('#txtdate').val()
             var timeStart = new Date(theDate + ' ' + sHour + ':' + sMinutes + ' ' + sAmPm).getHours();
             var timeEnd = new Date(theDate + ' ' + eHour + ':' + eMinutes + ' ' + eAmPm).getHours();

             //Calulate the time difference
             var hourDiff = timeEnd - timeStart;

             //Check if hour difference is less than 4 hours and show the message accordingly
             if (hourDiff < 4) {
                 alert("A mininum of 4 hours is required!");
             }
         });
     });//]]>  

</script>

Then markup
        <asp:gridview ID="Gridview1" runat="server" OnRowDeleting="Gridview1_RowDeleting" ShowFooter="true" AutoGenerateColumns="false">
            <Columns>
            <asp:BoundField DataField="RowNumber" HeaderText="Row Number" />
                    <asp:TemplateField HeaderText="Event Date(s)">
                        <ItemTemplate>
                            <asp:TextBox ID="txtdate" runat="server" class = "Calender"></asp:TextBox>
                            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtdate"
                                ErrorMessage="*" SetFocusOnError="True"></asp:RequiredFieldValidator>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Start Time">
                        <ItemTemplate>
                                  <asp:DropDownList id="startHour" runat="server">
                                    <asp:ListItem Value="07">07</asp:ListItem>
                                    <asp:ListItem Value="08">08</asp:ListItem>
                                    <asp:ListItem Value="09">09</asp:ListItem>
                                    <asp:ListItem Value="10">10</asp:ListItem>
                                    <asp:ListItem Value="11">11</asp:ListItem>
                                    <asp:ListItem Selected="True" Value="12">12</asp:ListItem>
                                    <asp:ListItem Value="01">01</asp:ListItem>
                                    <asp:ListItem Value="02">02</asp:ListItem>
                                    <asp:ListItem Value="03">03</asp:ListItem>
                                    <asp:ListItem Value="04">04</asp:ListItem>
                                    <asp:ListItem Value="05">05</asp:ListItem>
                                    <asp:ListItem Value="06">06</asp:ListItem>
                                  </asp:DropDownList>
                            <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="startHour"
                                ErrorMessage="*" SetFocusOnError="True"></asp:RequiredFieldValidator>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Minutes">
                        <ItemTemplate>
                                  <asp:DropDownList id="startMinutes" runat="server">
                                    <asp:ListItem Value="00">00</asp:ListItem>
                                    <asp:ListItem Value="15">15</asp:ListItem>
                                    <asp:ListItem Value="30">30</asp:ListItem>
                                    <asp:ListItem Value="45">45</asp:ListItem>
                                 </asp:DropDownList>
                            <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="startMinutes"
                                ErrorMessage="*" SetFocusOnError="True"></asp:RequiredFieldValidator>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="AM/PM">
                        <ItemTemplate>
                                 <asp:DropDownList id="startAmPm" runat="server">
                                    <asp:ListItem Value="AM">AM</asp:ListItem>
                                    <asp:ListItem Value="PM">PM</asp:ListItem>
                                 </asp:DropDownList>
                            <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="startAmPm"
                                ErrorMessage="*"></asp:RequiredFieldValidator>&nbsp;
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="End Time">
                        <ItemTemplate>
                                  <asp:DropDownList id="endHour" runat="server">
                                    <asp:ListItem Value="07">07</asp:ListItem>
                                    <asp:ListItem Value="08">08</asp:ListItem>
                                    <asp:ListItem Value="09">09</asp:ListItem>
                                    <asp:ListItem Value="10">10</asp:ListItem>
                                    <asp:ListItem Value="11">11</asp:ListItem>
                                    <asp:ListItem Selected="True" Value="12">12</asp:ListItem>
                                    <asp:ListItem Value="01">01</asp:ListItem>
                                    <asp:ListItem Value="02">02</asp:ListItem>
                                    <asp:ListItem Value="03">03</asp:ListItem>
                                    <asp:ListItem Value="04">04</asp:ListItem>
                                    <asp:ListItem Value="05">05</asp:ListItem>
                                    <asp:ListItem Value="06">06</asp:ListItem>
                                  </asp:DropDownList>
                            <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ControlToValidate="endHour"
                                ErrorMessage="*" SetFocusOnError="True"></asp:RequiredFieldValidator>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Minutes">
                        <ItemTemplate>
                                  <asp:DropDownList id="endMinutes" runat="server" CSClass="fptextbox12">
                                    <asp:ListItem Value="00">00</asp:ListItem>
                                    <asp:ListItem Value="15">15</asp:ListItem>
                                    <asp:ListItem Value="30">30</asp:ListItem>
                                    <asp:ListItem Value="45">45</asp:ListItem>
                                 </asp:DropDownList>
                             <asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" ControlToValidate="startMinutes"
                                ErrorMessage="*" InitialValue="Select"></asp:RequiredFieldValidator>
                        </ItemTemplate>
                         </asp:TemplateField>
                        <asp:TemplateField HeaderText="AM/PM">
                        <ItemTemplate>
                                 <asp:DropDownList id="endAmPm" runat="server">
                                    <asp:ListItem Value="AM">AM</asp:ListItem>
                                    <asp:ListItem Value="PM">PM</asp:ListItem>
                                 </asp:DropDownList>
                            <asp:RequiredFieldValidator ID="RequiredFieldValidator8" runat="server" ControlToValidate="endAmPm"
                                ErrorMessage="*" SetFocusOnError="True"></asp:RequiredFieldValidator>
                        </ItemTemplate>

                        <FooterStyle HorizontalAlign="Right" />
                        <FooterTemplate>
                            <asp:Button ID="ButtonAdd" runat="server" Text="Add Another Event" OnClick="ButtonAdd_Click" />
                        </FooterTemplate>
                      </asp:TemplateField>
                    <asp:CommandField ShowDeleteButton="True" />
                </Columns>
                <FooterStyle BackColor="#AC54AA" Font-Bold="True" ForeColor="white" />
                <RowStyle BackColor="#EFF3FB" />
                <EditRowStyle BackColor="#2461BF" />
                <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                <HeaderStyle BackColor="#AC54AA" Font-Bold="True" ForeColor="white" />
                <AlternatingRowStyle BackColor="White" />
             </asp:gridview>
            <br />
         </div>
        <asp:Button ID="btnSave" runat="server" Text="Send Request" OnClick="btnSave_Click" alt="Send Request" /><br />
        <asp:Label ID="lblResult" style ="font-weight:bold; font-size:large" runat="server"></asp:Label>

 <asp:Button ID="btnSave" runat="server" Text="Send Reservation Request" OnClick="btnSave_Click" alt="Send Request" /><br />

这在向gridview添加新行并将整个数据提交到数据库方面工作正常。

我遇到的问题是我的验证不再有效。

例如,我们要求进行预订的用户必须至少选择4小时。

如果用户在开始时间和结束时间之间选择的时间少于4小时,则会出现以下警告:

需要最少4小时!

这很有效(非常感谢我在这里收到了很大的帮助),直到我切换到Gridview。

不存在任何错误,但警报不再有效。

是否有另一种方法可以在asp.net表单(而不是gridview)中动态添加行,并且仍能成功提交到数据库?

或者更好的是,我需要改变JQuery处理gridview控件以允许验证工作的方式?

我花了整整一天的时间试图弄明白这一点无济于事。

0 个答案:

没有答案