c# - 如何更新FormView?

标签 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>

对象数据源:

<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>

更新方法:

    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();
}

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

最佳答案

您需要将 updatedEvent 放入代码中,而不是 updatingEvent

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

编辑:在您发布完整的表单设计后,我注意到在您的编辑配置文件中您已经绑定(bind)了您的值,例如...

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

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

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

您必须使用Bind 而不是Eval,它提供了两种方式的绑定(bind)。就像..

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

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

关于c# - 如何更新FormView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5740452/

相关文章:

c# - .`ToListAsync()`的目的是什么

c# - MSTest:单元测试 - 未找到输入文件 ... vsmdi

c# - 在 Visual Studio 中解决 DisconnectedContext

javascript - 将值从 JavaScript 传递到 C# 后,如何使用它进行一些自动操作?

c# - 在 RhinoMocks 中重置模拟

c# - 寻路算法来查找每个部分是否连接到特定对象

.net - .net 世界中的依赖注入(inject),无需 XML 配置文件

asp.net - 通过 Web 配置将目录添加到 ASP.NET 卷影副本

javascript - 禁用字段但允许将光标放在它上面并允许从中复制

c# - 无法使用 ClientScript.RegisterStartupScript 引发 div