开发者

How to Bind complex data to control in ListView (class User contain instance of class UserType)

I have 2 value class

public class UserType
{
    public int ID;
    public string TypeName;
}
public class User
{
    public int ID;
    public string UserName;
    public UserType Type;
}

1 proccessing class

public class Users
{
    public User[] GetUsers()
    {
            //Retrive and re turn User array
    }
    public int Update(User user, User old_user)
    {
            //Update user
    }
}

1 ListView and 1 ObjectDataSource

<asp:ListView ID="lsvUser" runat="server" DataKeyNames="ID" DataSourceID="odsUser" 
    ItemPlaceholderID="plhItem" onitemupdating="lsvUser_ItemUpdating">
<LayoutTemplate>
    <ul><asp:PlaceHolder ID="plhItem" runat="server"></asp:PlaceHolder></ul>
</LayoutTemplate>
<ItemTemplate>
    <li>
        <asp:Label ID="lblID" runat="server" Text='<%# Eval("ID") %>'></asp:Label>
        <asp:Label ID="lblUserName" runat="server" Text='<%# Eval("UserName") %>'></asp:Label>
        <asp:Label ID="lblTypeName" runat="server" Text='<%# Eval("Type.TypeName") %>'></asp:Label>
        <asp:LinkButton ID="btEdit" runat="server" CssClass="button" CommandName="Edit" Text="Edit"></asp:LinkButton>
    </li>
</ItemTemplate&g开发者_开发技巧t;
<EditItemTemplate>
    <li class="editRow">
        <asp:Textbox ID="txbUserName" runat="server" Text='<%# Bind("UserName") %>'></asp:Textbox >
        <asp:Textbox ID="txbTypeName" runat="server" Text='<%# Bind("Type.TypeName") %>'></asp:Textbox >
    </li>
</EditItemTemplate>
</asp:ListView>
<asp:ObjectDataSource ID="odsUser" runat="server" TypeName="BLL.Users" DataObjectTypeName="BLL.User" SelectMethod="GetUsers" UpdateMethod="Update" ConflictDetection="CompareAllValues" OldValuesParameterFormatString="old_{0}"></asp:ObjectDataSource>

When data is loaded into the ItemTemplate, the ListView interprets Type.TypeName and binds correctly, but when updating the record, I cannot retrieve the old_user.Type from the old values. So old_user.Name and old_user.ID have values but old_user.Type is null.

Help me. I have been searching for about 2 days and cannot find a solution. I can bind data for a new user like this:

protected void lsvUser_ItemUpdating(object sender, ListViewUpdateEventArgs e)
{
    e.NewValues.Add("Type", new BLL.UserType() { TypeName = Convert.ToString(((TextBox)lsvUser.EditItem.FindControl("txbTypeName")).Text) });
}

But I don't know how to retrieve the old values (how to retrive value of lblTypeName). Help me. If there is a solution that doesn't require additional code in the ItemUpdating event, please teach me.

Thank you.


Add Type.TypeName as a DataKey, like this:

<asp:ListView ID="ListView1" runat="server" DataKeyNames="Type.TypeName">

And in your ItemUpdating event handler, retrieve the key value like this:

string typeName = (string)e.Keys["Type.TypeName"];

I'm not positive that "Type.TypeName will work, but some variation of that should work, whether it's Type.TypeName or just TypeName.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜