需要修复: - 除非指定了UpdateMethod,否则ObjectDataSource'odsShiftDetails'不支持更新

时间:2012-11-15 07:53:08

标签: c# asp.net sql-server-2008 ajaxcontroltoolkit

<asp:Panel ID="pnlGrdShift" runat="server" ScrollBars="Auto" Width="900px" Height="520px" CssClass="srcColor">
            <cc1:GridView  ID="gvShift" runat="server" AutoGenerateColumns="False" AllowSorting="True"
               CssClass="grid"
               OnDataBound="gvShift_DataBound" 
               DataSourceID="odsShiftDetails" 
               AllowPaging="True" 
               ShowFooter="false"
               onrowcancelingedit="gvShift_RowCancelingEdit" 
               onrowcommand="gvShift_RowCommand" 
               onrowdeleting="gvShift_RowDeleting" 
               onrowediting="gvShift_RowEditing" 
               onrowupdating="gvShift_RowUpdating" 
               OnSelectedIndexChanged="gvShift_SelectedIndexChanged" 
               OnRowDataBound="gvShift_RowDataBound">        
                <AlternatingRowStyle CssClass="altrowstyle"  />
                <HeaderStyle CssClass="headerstyle" />
                <RowStyle CssClass="rowstyle" Wrap="false"  />
                <EmptyDataRowStyle BackColor="#edf5ff" Height="300px" VerticalAlign="Middle" HorizontalAlign="Center" />
                <EmptyDataTemplate >
                    No Records Found
                </EmptyDataTemplate> 
                <Columns>
                    <asp:TemplateField HeaderText="E Code" SortExpression="userecode" HeaderStyle-Font-Bold="true" HeaderStyle-Font-Names="Calibre" HeaderStyle-ForeColor="White">
                        <ItemTemplate>
                            <asp:Label ID="lblUserECode" runat="server" Text='<%#Eval("userecode") %>' CssClass="GridContent" />                    
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:Label ID="lblEditUserECode" runat="server" Text='<%#Eval("userecode") %>' style="width:50px;" CssClass="GridContent" />                    
                        </EditItemTemplate>
                    </asp:TemplateField>                       

                    <asp:TemplateField HeaderText="User Name" SortExpression="username" HeaderStyle-Font-Bold="true" HeaderStyle-Font-Names="Calibre" HeaderStyle-ForeColor="White">
                        <ItemTemplate>
                            <asp:Label ID="lblUserName" runat="server" Text='<%#Eval("username") %>' CssClass="GridContent" />                    
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:Label ID="lblEditUserName" runat="server" Text='<%#Eval("username") %>' style="width:100px;" CssClass="GridContent"/>                    
                        </EditItemTemplate>
                    </asp:TemplateField>


                    <asp:TemplateField HeaderText="Shift Start Time" SortExpression="ShiftStartTime" HeaderStyle-Font-Bold="true" HeaderStyle-Font-Names="Calibre" HeaderStyle-ForeColor="White">
                         <ItemTemplate>
                            <asp:Label ID="lblShiftStartTime" runat="server" Text='<%#Eval("ShiftStartTime") %>'  CssClass="GridContent"/>                    
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="ddlShiftStartTime" runat="server" Text='<%#Eval("ShiftStartTime") %>' style="width:65px;" CssClass="GridContent" />
                            <asp:RegularExpressionValidator ID="RegularExpValidatorddlShiftStartTime" runat="server" ControlToValidate="ddlShiftStartTime" ValidationExpression="^([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$" ErrorMessage="*" Font-Bold="true" ForeColor="Red" ToolTip="Must be in HH:MM" />                            

                        </EditItemTemplate>                            
                    </asp:TemplateField>  

                    <asp:TemplateField HeaderText="Shift End Time" SortExpression="ShiftEndTime" HeaderStyle-Font-Bold="true" HeaderStyle-Font-Names="Calibre" HeaderStyle-ForeColor="White">
                        <ItemTemplate>
                            <asp:Label ID="lblShiftEndTime" runat="server" Text='<%#Eval("ShiftEndTime") %>' CssClass="GridContent" />                    
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="ddlShiftEndTime" runat="server" Text='<%#Eval("ShiftEndTime") %>' style="width:65px;" CssClass="GridContent"  />
                            <asp:RegularExpressionValidator ID="RegularExpValidatorddlShiftEndTime" runat="server" ControlToValidate="ddlShiftEndTime" ValidationExpression="^([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$" ErrorMessage="*" Font-Bold="true" ForeColor="Red" ToolTip="Must be in HH:MM" />                            

                        </EditItemTemplate>                            
                    </asp:TemplateField>
                        <%--<asp:BoundField DataField="ShiftEndTIme" HeaderText="Shift End Time" SortExpression="ShiftEndTIme"/>--%>
                    <asp:TemplateField HeaderText="Saturday Shift Start Time" SortExpression="WeekendShiftStartTime" HeaderStyle-Font-Bold="true" HeaderStyle-Font-Names="Calibre" HeaderStyle-ForeColor="White">
                        <ItemTemplate>
                            <asp:Label ID="lblWeekendShiftStartTime" runat="server" Text='<%#Eval("WeekendShiftStartTime") %>' CssClass="GridContent" />                    
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="ddlWeekendShiftStartTime" runat="server" Text='<%#Eval("WeekendShiftStartTime") %>' style="width:65px;" CssClass="GridContent" />  
                            <asp:RegularExpressionValidator ID="RegularExpValidatorddlWeekendShiftStartTime" runat="server" ControlToValidate="ddlWeekendShiftStartTime" ValidationExpression="^([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$" ErrorMessage="*" Font-Bold="true" ForeColor="Red" ToolTip="Must be in HH:MM" />                          

                        </EditItemTemplate>
                    </asp:TemplateField>

                    <asp:TemplateField HeaderText="Saturday Shift End Time" SortExpression="weekendshiftendtime" HeaderStyle-Font-Bold="true" HeaderStyle-Font-Names="Calibre" HeaderStyle-ForeColor="White">
                    <ItemTemplate>
                            <asp:Label ID="lblWeekendShiftEndTime" runat="server" Text='<%#Eval("weekendshiftendtime") %>' CssClass="GridContent"/>                    
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="ddlWeekendShiftEndTime" runat="server" Text='<%#Eval("weekendshiftendtime") %>' style="width:65px;" CssClass="GridContent" /> 
                            <asp:RegularExpressionValidator ID="RegularExpValidatorddlWeekendShiftEndTime" runat="server" ControlToValidate="ddlWeekendShiftEndTime" ValidationExpression="^([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$" ErrorMessage="*" Font-Bold="true" ForeColor="Red" ToolTip="Must be in HH:MM" />                           

                        </EditItemTemplate>                           
                    </asp:TemplateField>

                    <asp:TemplateField  ShowHeader="False" > 
                        <EditItemTemplate> 
                          <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update" Text="Update" ForeColor="White"></asp:LinkButton> 
                          <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" ForeColor="White"></asp:LinkButton> 
                        </EditItemTemplate> 
                        <ItemTemplate> 
                          <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit" ForeColor="White"></asp:LinkButton> 
                        </ItemTemplate> 
                </asp:TemplateField>                    
           </Columns>
                <PagerTemplate >
                     <table width="100%" >
                        <tr>
                            <td style="text-align:  left">
                                Page Size: 
                                    <asp:DropDownList ID="ddPageSize" runat="server" EnableViewState="true" OnSelectedIndexChanged="ddPageSize_SelectedIndexChanged" AutoPostBack="true" style="width:50px;">
                                    <asp:ListItem Text="10" ></asp:ListItem>
                                    <asp:ListItem Text="20" ></asp:ListItem>
                                    <asp:ListItem Text="30" ></asp:ListItem>
                                    <asp:ListItem Text="40" ></asp:ListItem>
                                    <asp:ListItem Text="50" ></asp:ListItem>
                                </asp:DropDownList>
                            </td>
                            <td style="text-align: right">
                                <asp:Label ID="lblPageCount" runat="server"></asp:Label>
                            </td>
                        </tr>
                    </table>
                </PagerTemplate>
                </cc1:GridView>      
         </asp:Panel></td></tr>
                </table>
                                                       <div style="margin-top:5px" class="PagerGrid">
                <asp:DataPager  ID="pager" runat="server" PagedControlID="gvShift">
                    <Fields>                                            
                        <asp:NextPreviousPagerField FirstPageText="&lt;&lt;" LastPageText="&gt;&gt;" 
                            NextPageText="&gt;"  PreviousPageText="&lt;" ShowFirstPageButton="True"
                            ShowNextPageButton="False" ButtonCssClass="datapager"  />
                        <asp:NumericPagerField ButtonCount="10"  NumericButtonCssClass="datapager" CurrentPageLabelCssClass="datapager"   />
                        <asp:NextPreviousPagerField LastPageText="&gt;&gt;" NextPageText="&gt;" 
                            ShowLastPageButton="True" ShowPreviousPageButton="False" ButtonCssClass="datapager" />
                    </Fields>
                </asp:DataPager>
            </div>
            <br />
            <asp:ObjectDataSource ID="odsShiftDetails" runat="server" 
                SelectMethod="GetShiftInfoSortedPage" TypeName="EQ.DAL.ShiftInfoDB"
                EnablePaging="True" SelectCountMethod="GetShiftInfoCount" 
                SortParameterName="sortExpression">
                <SelectParameters>
                    <asp:ControlParameter  ControlID="hfSearchCriteria"  Name="searchCriteria" Direction="Input"  />
                </SelectParameters>                                            
            </asp:ObjectDataSource>

行更新的代码背后是: -

protected void gvShift_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        try
        {
            TextBox txtShiftStartTime = (TextBox)gvShift.Rows[e.RowIndex].FindControl("ddlShiftStartTime");
            TextBox txtShiftEndTime = (TextBox)gvShift.Rows[e.RowIndex].FindControl("ddlShiftEndTime");
            TextBox txtWeekendShiftStartTime = (TextBox)gvShift.Rows[e.RowIndex].FindControl("ddlWeekendShiftStartTime");
            TextBox txtWeekendShiftEndTime = (TextBox)gvShift.Rows[e.RowIndex].FindControl("ddlWeekendShiftEndTime");
            Label lblUserecode = (Label)gvShift.Rows[e.RowIndex].FindControl("lblEditUserECode");

            string userecode = lblUserecode.Text.ToString();
            string _ShiftStart = txtShiftStartTime.Text.ToString();
            string _ShiftEnd = txtShiftEndTime.Text.ToString();
            string _WeekendShiftStart = txtWeekendShiftStartTime.Text.ToString();
            string _WeekendShiftend = txtWeekendShiftEndTime.Text.ToString();

            EmployeeQuotientCL.Entities.ConfigurationVariables _configVariables = new ConfigurationVariables();
            string databaseConnectionString = _configVariables.ConnectionString;
            SqlConnection sqlConnection = null;
            if (((databaseConnectionString + string.Empty) != string.Empty))
            {
                DBConnect dbConnect = new DBConnect(_configVariables.ConnectionString);
                sqlConnection = dbConnect.SQLConnection;
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = sqlConnection;
                cmd.CommandText = "UPDATE UserInfo SET ShiftStartTime ='" + _ShiftStart + "',ShiftEndTime='" + _ShiftEnd.ToString() + "',Weekendshiftstarttime='" + _WeekendShiftStart.ToString() + "',Weekendshiftendtime='" + _WeekendShiftend.ToString() + "'   WHERE UserECode=" + userecode.ToString();
                sqlConnection.Open();
                cmd.ExecuteNonQuery();
                gvShift.DataSource = null;
                gvShift.DataBind();
                sqlConnection.Close();
            }
        }
        catch (Exception ex)
        {
        }
    }

在网格中更新行时,我收到以下错误消息。

  

Microsoft JScript运行时错误:Sys.WebForms.PageRequestManagerServerErrorException:&gt; ObjectDataSource'odsShiftDetails'不支持更新,除非指定了UpdateMethod。

指导我如何解决此错误。提前谢谢。

1 个答案:

答案 0 :(得分:0)

您需要为gridview添加更新方法。请看本教程:Gridview ObjectdataSource