我已将数据库中的数据与 Databind 链接,我可以添加新行。我无法在编辑模式下修改数据。代码正在运行,但是当我修改 TextBox 并更新 Item 时,它不会加载更改。对不起,如果我的解释不清楚,我会尝试添加一些图片和代码。
正如您从图片中看到的那样,即使我更改名称(NOME),它也会重写前一个。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.ModelBinding;
using System.Data.Entity.Infrastructure;
using System.Data.Entity;
using System.Threading;
using System.Globalization;
using REAL = System.Single;
using BOOL = System.Boolean;
using System.Web.UI.HtmlControls;
using InserimentoDati.Modello;
using System.Configuration;
namespace InserimentoDati
{
public partial class Cliente : Page
{
public static int VarCliente;
protected void Page_Preload(object sender, EventArgs e)
{
var UrlBase = Request.Url.AbsoluteUri;
string Cliente = UrlBase.Substring(UrlBase.IndexOf('?') + 1);
FormViewState.Value = Convert.ToString(FormViewCliente.CurrentMode);//PASSO AL CLIENT LO STATO DELLA FORMVIEW
try
{
VarCliente = Convert.ToInt32(Cliente);
}
catch
{
VarCliente = 1;
}
ClienteSelezionato.Value = Convert.ToString(VarCliente);
}
protected void Page_Load(object sender, EventArgs e)
{
FormViewCliente.DataBind();
if (!IsPostBack)
{
FormViewCliente.ChangeMode((FormViewMode)DetailsViewMode.Edit);
}
UpdatePanelCliente.Update();
}
public IQueryable<TabellaCliente> SelectTabellaCliente (int? IdTabellaCliente)
{
InserimentoDatiEntities db = new InserimentoDatiEntities();
IdTabellaCliente = VarCliente;
var query = from p in db.TabellaCliente
where p.IdTabellaCliente == IdTabellaCliente
select p;
UpdatePanelCliente.Update();
return query;
}
public void onModeChangingFormview(object sender, FormViewModeEventArgs e)
{
FormViewCliente.ChangeMode(e.NewMode);
FormViewCliente.DataBind();
}
public void onModeChangedFormview(object sender, EventArgs e)
{
UpdatePanelCliente.Update();
}
public void AggiornaCliente(int IdTabellaCliente)
{
using (InserimentoDatiEntities db = new InserimentoDatiEntities())
{
int piio = IdTabellaCliente;
int ClienteInModifica;
ClienteInModifica = VarCliente;
TabellaCliente item = null;
item = db.TabellaCliente.Find(IdTabellaCliente);
//AggiornoManualmente il tipo di set di taglio selezionato
DropDownList ddlPelo = (DropDownList)FormViewCliente.FindControl("DropDownListPelo");
//int TipoPelo = Convert.ToInt32(ddlPelo.SelectedValue);
//item.Pelo = TipoPelo;
TextBox TxNome = (TextBox)FormViewCliente.FindControl("TextBoxNome");
TextBox TxCognome = (TextBox)FormViewCliente.FindControl("TextBoxCognome");
TextBox TxNumero = (TextBox)FormViewCliente.FindControl("TextBoxNumero");
TextBox TxEmail = (TextBox)FormViewCliente.FindControl("TextBoxEmail");
TextBox TxNomeCane = (TextBox)FormViewCliente.FindControl("TextBoxNomeCane");
TextBox TxRazza = (TextBox)FormViewCliente.FindControl("TextBoxRazza");
TextBox TxShampoo = (TextBox)FormViewCliente.FindControl("TextBoxShampoo");
TextBox TxFoto = (TextBox)FormViewCliente.FindControl("TextBoxFile");
TextBox TxPrezzo = (TextBox)FormViewCliente.FindControl("TextBoxPrezzo");
TextBox TxNote = (TextBox)FormViewCliente.FindControl("TextBoxNote");
//if (TxPrezzo.Text == "")
//{
// item.Prezzo = 0;
//}
//else
//{
// item.Prezzo = float.Parse(TxPrezzo.Text);
//}
//item.Pelo = TipoPelo;
//item.Nome = TxNome.Text;
//item.Cognome = TxCognome.Text;
//item.Numero = TxNumero.Text;
//item.Email = TxEmail.Text;
//item.NomeCane = TxNomeCane.Text;
//item.Razza = TxRazza.Text;
//item.Shampo = TxShampoo.Text;
//item.Foto = TxFoto.Text;
//item.Note = TxNote.Text;
try
{
TryUpdateModel(item);
db.SaveChanges();
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('ClienteAggiornato')", true);
}
catch (Exception ex)
{
var appo = ex;
}
}
}
protected void TextBoxNome_TextChanged(object sender, EventArgs e)
{
}
}
}
<asp:UpdatePanel runat="server" ID="UpdatePanelCliente" UpdateMode="Conditional">
<ContentTemplate>
<input id="FormViewState" type="hidden" runat="server" />
<input id="ClienteSelezionato" type="hidden" runat="server" />
<asp:FormView runat="server"
CssClass="FormView"
ItemType = "InserimentoDati.Modello.TabellaCliente"
ID="FormViewCliente"
AutoPostBack = "True"
SelectMethod= "SelectTabellaCliente"
UpdateMethod="AggiornaCliente"
InsertMethod = "SalvaNuovoCliente"
DataKeyNames = "IdTabellaCliente"
OnModeChanging ="onModeChangingFormview"
OnModeChanged ="onModeChangedFormview"
DefaultMode="Edit"
>
<ItemTemplate>
ciao
</ItemTemplate>
<InsertItemTemplate>
ciao
</InsertItemTemplate>
<EditItemTemplate>
<div class="row">
<div class="col-md-3">
<h2>Nome</h2>
<p>
<asp:TextBox ID="TextBoxNome" runat="server" Text="<%# BindItem.Nome%>" Enabled="true" OnTextChanged="TextBoxNome_TextChanged" ReadOnly="false"></asp:TextBox>
</p>
</div>
<div class="col-md-3">
<h2>Cognome</h2>
<p>
<asp:TextBox ID="TextBoxCognome" runat="server" Text="<%# BindItem.Cognome%>"></asp:TextBox>
</p>
</div>
<div class="col-md-3">
<h2>Email</h2>
<p>
<asp:TextBox ID="TextBoxEmail" runat="server" Text="<%# BindItem.Email%>"></asp:TextBox>
</p>
</div>
<div class="col-md-3">
<h2>Numero</h2>
<p>
<asp:TextBox ID="TextBoxNumero" runat="server" Text="<%# BindItem.Numero%>"></asp:TextBox>
</p>
</div>
</div>
<div class="row">
<div class="col-md-3">
<h2>Nome Cane</h2>
<p>
<asp:TextBox ID="TextBoxNomeCane" runat="server" Text="<%# BindItem.NomeCane%>"></asp:TextBox>
</p>
</div>
<div class="col-md-3">
<h2>Razza</h2>
<p>
<asp:TextBox ID="TextBoxRazza" runat="server" Text="<%# BindItem.Razza%>"></asp:TextBox>
</p>
</div>
<div class="col-md-3">
<h2>Pelo</h2>
<asp:DropDownList ID="DropDownListPelo" CssClass="btn btn-primary dropdown" runat="server"
DataSourceID ="SqlDataSource1"
DataTextField="Pelo"
DataValueField="IdTabellaPelo">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:InserimentoDatiConnectionString %>" SelectCommand="SELECT * FROM [TabellaPelo]"></asp:SqlDataSource>
</div>
<div class="col-md-3">
<h2>Shampoo</h2>
<p>
<asp:TextBox ID="TextBoxShampoo" runat="server" Text="<%# BindItem.Shampo%>"></asp:TextBox>
</p>
</div>
</div>
<div class="row">
<div class="col-md-3">
<h2>Foto</h2>
<p>
<asp:TextBox ID="TextBoxFile" runat="server" Text="<%# BindItem.Foto%>"></asp:TextBox>
</p>
</div>
<div class="col-md-3">
<asp:FileUpload ID="FileUpload1" CssClass="ButtonSearchFile" runat="server" > </asp:FileUpload>
</div>
<div class="col-md-3">
<h2>Prezzo</h2>
<p>
<asp:TextBox ID="TextBoxPrezzo" runat="server" Text="<%# BindItem.Prezzo%>"></asp:TextBox>
</p>
</div>
</div>
<div class="row">
<div class="col-md-3">
<h2>Note</h2>
<asp:TextBox ID="TextBoxNote" TextMode="MultiLine" CssClass="note" runat="server" Text="<%# BindItem.Note%>"></asp:TextBox>
</div>
<div class="col-md-9 ButtonSave" >
<div class="PulsanteNew"><asp:LinkButton CommandName="Update" ValidationGroup="mainPropertyGroup" CssClass="btn btn-primary ScambioColore" runat="server" ID="ButtonAggiornaRicetta"><span aria-hidden="true" class="glyphicon glyphicon-floppy-saved"></span></asp:LinkButton></div>
</div>
</div>
<div class="row">
<asp:Image ID="ImageAuthor" runat="server" ImageUrl="<%# BindItem.Foto%>" Height="500px"/>
</div>
</EditItemTemplate>
<EmptyDataTemplate>
There is nothing to see here.
</EmptyDataTemplate>
</asp:FormView>
</ContentTemplate>
</asp:UpdatePanel>