I have 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();
}
开发者_运维技巧but the data is not updated (label is updated :) ). What am I doing wrong?
You need to put updatedEvent
in your code instead of updatingEvent
switch (fvReport.CurrentMode)
{
case FormViewMode.Edit:
fvReport.AllowPaging = false;
lbl.Text = "Update!!!";
fvReport.ChangeMode(FormViewMode.ReadOnly);
fvReport.DataBind();
break;
}
fvReport.DataBind();
Edit: After you posted your complete form design I have noticed that in your edit profile you have bound your value like...
Text='<%# Eval("order") %>'
But this function provides only one way binding. It means it will populate the value from your DB to control.
However this will not pass the values back when you are trying to update your values.
You have to use Bind instead Eval
, that provide two way binding. like..
Text='<%# Bind("order") %>'
Add in all your controls in the Edit and Insert template.
You have to set the DataSourceControlID
property of your FormView
<asp:FormView ID="fvReport" runat="server" AllowPaging="false"
OnModeChanging="fvReport_ModeChanging"
OnItemUpdating="fvReport_ItemUpdating"
DataKeyNames="id"
DataSourceID="ObjectReport">
精彩评论