编辑GridView(ASP.NET)中没有键的对象(Value对象)



我有我的主对象PersonAdrresses值对象的集合,如下所示:

public class Person
{
public virtual int Id {get; set;}
public virtual string Name {get; set;}
private IList<Adrress > _adrresses= new List<Adrress>();
public virtual IEnumerable Adrress Adrresses {...}
public virtual void AddAddress(Address a){...}
public virtual void RemoveAddress(Address a){...}
public virtual void ChangeAddress(OldAddress oa, OldAddress oa)
{
int index = _adrresses.IndexOf(oa);
if (index != -1)
_adrresses[index] = oa;
}
}
public class Adrress 
{
public virtual string Street {get; set;}
public virtual string Number {get; set;}
public virtual string City {get; set;}
... other 7 fields
}

在我的asp页面中,我有一个Gridview,用户可以在其中添加、删除或更改地址。

问题是:如何从网格中的一个用户选择中获得正确的地址?我必须从网格中读取每个字段,并使用它们来构造我必须搜索的地址吗?

我使用CommandArgument传递Container。DataItemIndex,所以不能用它来发送整个对象到代码

编辑:这是网格的摘录:

<asp:GridView ID="gvElencoFaseAttivita" runat="server" 
AllowPaging="false" AutoGenerateColumns="False" 
Width="100%" CssClass="datiGridView tabletwo" ShowHeader="False" 
OnRowCommand="gvElencoFaseAttivita_RowCommand" 
OnRowUpdating="gvElencoFaseAttivita_RowUpdating" 
OnRowDeleted="gvElencoFaseAttivita_RowDeleted" 
OnRowEditing="gvElencoFaseAttivita_RowEditing" 
OnRowCancelingEdit="gvElencoFaseAttivita_RowCancelingEdit">
<Columns>
<asp:TemplateField HeaderText="Sel">
<ItemTemplate>
<asp:LinkButton ID="lbtnEditFaseAttivita" runat="server" CausesValidation="False" CommandName="Edit" CommandArgument='<%# Container.DataItemIndex %>' ToolTip="Modifica la fase" CssClass="imgModificaPiccola" />
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="lbtnAggiornaFaseAttivita" runat="server" ValidationGroup="UpdateFaseAttivitaGrid" CommandName="Update" CssClass="imgSalvaPiccola" ToolTip="Salva le modifiche" CommandArgument='<%# Container.DataItemIndex %>' />
</EditItemTemplate>
<ItemStyle CssClass="gestioneFaseAttivitaTd2" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Street" SortExpression="Street">
<ItemTemplate>
<asp:Label ID="lblStreet" runat="server" Text='<%# ((Adrress)Container.DataItem).Street %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEditStreet" runat="server" Text='<%# ((Adrress)Container.DataItem).Street %>'></asp:TextBox>
</EditItemTemplate>
<ItemStyle CssClass="gestioneFaseAttivitaTd3" />
</asp:TemplateField>
</Columns>
<SelectedRowStyle CssClass="selezioneGridView" />
<AlternatingRowStyle CssClass="odd" />
</asp:GridView>

这就是dataBind

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
loadGridAddress();
}
protected void loadGridAddress()
{
using (iuow = kernel.Get<IUnitOfWork>())
{
gvElencoFaseAttivita.DataSource = person.Addresses;
gvElencoFaseAttivita.DataBind();
}
}
protected void gvElencoFaseAttivita_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
var a = e.OldValues[""];  //e.OldValues  here is empty
}

您可以定义一个方法来提供对象的字符串表示,以及一个接受相同表示的构造函数。由于您在评论中提到JSON,它可能是:

public class Address 
{
public virtual string Street {get; set;}
public virtual string Number {get; set;}
public virtual string City {get; set;}
...
public string ToJSON()
{ 
// Encode the object as as JSON string
...
return strJSON;
}
public Address(string json)
{
// Decode JSON string and set object properties
...
}
}

您可以将JSON字符串保存在一个隐藏字段中:

<EditItemTemplate>
<asp:HiddenField ID="hfAddress" runat="server" Value='<%# ((Address)Container.DataItem).ToJSON() %>' />
<asp:TextBox ID="txtEditStreet" runat="server" Text='<%# ((Address)Container.DataItem).Street %>'/>
...
</EditItemTemplate>

并在RowUpdating事件处理程序中检索它以重新创建原始对象:

protected void gvElencoFaseAttivita_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = gvElencoFaseAttivita.Rows[e.RowIndex];
HiddenField hfAddress = row.FindControl("hfAddress") as HiddenField;
TextBox txtEditStreet = row.FindControl("txtEditStreet") as TextBox;
Address oldAddress = new Address(hfAddress.Value);
string newStreet = txtEditStreet.Text;
...
}

相关内容

  • 没有找到相关文章

最新更新