如何更新FormView?

时间:2011-04-21 06:38:12

标签: c# .net asp.net visual-studio formview

我有FormView:

 <asp:FormView ID="fvReport" runat="server" AllowPaging="false" 
        OnModeChanging="fvReport_ModeChanging"  
        OnItemUpdating="fvReport_ItemUpdating" 
        DataKeyNames="id" DataSourceID="ObjectReport">
    <ItemTemplate>
        <asp:LinkButton ID="lbEdit" runat="server" CausesValidation="true" CommandName="Edit" >Edit</asp:LinkButton>
        <table>
           <tr id="order">
               <td style="Width:90px;">order:</td>
               <td><asp:textbox ID="Textbox7" runat="server" Text='<%# Eval("order") %>' Width="600px" TextMode="MultiLine" ReadOnly="True" Rows="3" BorderStyle="NotSet" CssClass="text-details"></asp:textbox></td></tr>
           <tr id="purpose">
               <td style="Width:90px;">purpose:</td>
               <td><asp:textbox ID="Textbox8" runat="server" Text='<%# Eval("purpose") %>' Height="34px" Width="600px" TextMode="MultiLine" ReadOnly="True" CssClass="text-details"></asp:textbox></td></tr>
           <tr id="feature_runs">
               <td style="Width:90px;">features:</td>
               <td><asp:textbox ID="Textbox9" runat="server" Text='<%# Convert.ToString(Eval("features")).Replace( "Esc", "Еsс")%>' Height="52px" Width="600px"  ReadOnly="True" TextMode="MultiLine" Font-Italic="True" ForeColor="Red" Enabled="false" CssClass="text-details"></asp:textbox></td></tr>
           <tr id="settings">
               <td style="Width:90px;">settings:</td>
               <td><asp:textbox ID="Textbox10" runat="server" Text='<%# Convert.ToString(Eval("settings")).Replace( "Esc", "Еsс") %>' Height="44px" Width="600px"  TextMode="MultiLine" ReadOnly="True" CssClass="text-details"></asp:textbox></td></tr>
        </table>
    </ItemTemplate>
    <EditItemTemplate>
        <table>
           <tr id="order">
               <td style="Width:90px;">order:</td>
               <td><asp:textbox ID="Textbox7" runat="server" Text='<%# Eval("order") %>' Width="600px" TextMode="MultiLine" ReadOnly="True" Rows="3" BorderStyle="NotSet" CssClass="text-details"></asp:textbox></td></tr>
           <tr id="purpose">
               <td style="Width:90px;">purpose:</td>
               <td><asp:textbox ID="Textbox8" runat="server" Text='<%# Eval("purpose") %>' Height="34px" Width="600px" TextMode="MultiLine" ReadOnly="True" CssClass="text-details"></asp:textbox></td></tr>
           <tr id="feature_runs">
               <td style="Width:90px;">features:</td>
               <td><asp:textbox ID="Textbox9" runat="server" Text='<%# Convert.ToString(Eval("features")).Replace( "Esc", "Еsс")%>' Height="52px" Width="600px"  ReadOnly="True" TextMode="MultiLine" Font-Italic="True" ForeColor="Red" Enabled="false" CssClass="text-details"></asp:textbox></td></tr>
           <tr id="settings">
               <td style="Width:90px;">settings:</td>
               <td><asp:textbox ID="Textbox10" runat="server" Text='<%# Convert.ToString(Eval("settings")).Replace( "Esc", "Еsс") %>' Height="44px" Width="600px"  TextMode="MultiLine" ReadOnly="True" CssClass="text-details"></asp:textbox></td></tr>
        </table>
        <asp:LinkButton ID="lbUpdate" runat="server" CausesValidation="true" CommandName="Update" >Save</asp:LinkButton>
        <asp:LinkButton ID="lbCancel" runat="server" CausesValidation="false" CommandName="Cancel">Cancel</asp:LinkButton>
    </EditItemTemplate>
    </asp:FormView>

ObjectDataSource:

<asp:ObjectDataSource ID="ObjectReport" runat="server"
       TypeName = "ObjectDataSources.CS.ConnectionToDB"
       SelectMethod = "GetReportById" UpdateMethod="">
   <SelectParameters>
       <asp:Parameter Name="report_id" Type ="Int32" />
   </SelectParameters>
   <UpdateParameters>
       <asp:Parameter Name="report_id" Type ="Int32" />
       <asp:Parameter Name="settings" Type = "String" />
       <asp:Parameter Name="purpose" Type = "String" />
       <asp:Parameter Name="order" Type = "String" />
       <asp:Parameter Name="features" Type = "String" />
   </UpdateParameters>
</asp:ObjectDataSource>

UpdateMetod:

    public int UpdateReportById(int report_id, string settings, string purpose, string order, string features)
        {
            SqlConnection conn = new SqlConnection(_connectionString);
            SqlCommand cmd = new SqlCommand("Update main_report SET settings = @settings, " + 
                "purpose = @purpose, order = @order, " +
                "features = @features WHERE id = @id", conn);
            cmd.Parameters.Add("@id", SqlDbType.Int).Value = report_id;
            cmd.Parameters.Add("@settings", SqlDbType.VarChar).Value = settings;
            cmd.Parameters.Add("@purpose", SqlDbType.VarChar).Value = purpose;
            cmd.Parameters.Add("@order", SqlDbType.VarChar).Value = order;
            cmd.Parameters.Add("@features", SqlDbType.VarChar).Value = features;
            int result = 0;
            try
            {
                conn.Open();
                result = cmd.ExecuteNonQuery();
            }
            catch (SqlException e)
            {
                throw new Exception("UpdateReportById Exception.");
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

fvReport_ItemUpdating:

protected void fvReport_ItemUpdating(Object sender, FormViewUpdateEventArgs e)
{
    switch (fvReport.CurrentMode)
    {
        case FormViewMode.Edit:
            fvReport.AllowPaging = false;
            lbl.Text = "Update!!!";
            fvReport.ChangeMode(FormViewMode.ReadOnly);
            fvReport.DataBind();
            break;
    }
    fvReport.DataBind();
}

但数据未更新(标签已更新:))。我做错了什么?

2 个答案:

答案 0 :(得分:2)

您需要在代码中添加updatedEvent而不是updatingEvent

switch (fvReport.CurrentMode)
{
    case FormViewMode.Edit:
        fvReport.AllowPaging = false;
        lbl.Text = "Update!!!";
        fvReport.ChangeMode(FormViewMode.ReadOnly);
        fvReport.DataBind();
        break;
}
fvReport.DataBind();

编辑:在您发布完整的表单设计后,我注意到在您的编辑个人资料中,您的价值就像... ...

Text='<%# Eval("order") %>'

但是这个功能只提供一种方式绑定。这意味着它将填充数据库中的值以进行控制。

但是,当您尝试更新值时,这不会传回值。

您必须使用提供双向绑定的Bind instead Eval。像..

Text='<%# Bind("order") %>'

在“编辑和插入”模板中添加所有控件。

答案 1 :(得分:1)

您必须设置DataSourceControlID

FormView属性
    <asp:FormView ID="fvReport" runat="server" AllowPaging="false" 
                OnModeChanging="fvReport_ModeChanging"  
                OnItemUpdating="fvReport_ItemUpdating" 
                DataKeyNames="id" 
                DataSourceID="ObjectReport">