Asp:带有CommandArgument的GridView按钮

时间:2017-04-11 23:39:23

标签: c# asp.net gridview webforms

我正试图点击我的按钮获取一个id。我的按钮位于GridView

<asp:GridView runat="server" AutoGenerateColumns="false"
                            ID="UsersGridView" AllowPaging="true"
                            CssClass="table table-hover table-striped"
                            UseAccessibleHeader="true" GridLines="None">
                    <Columns>
                        <asp:BoundField DataField="User.FullName" HeaderText="User" />
                        <asp:BoundField DataField="Book.Title" HeaderText="Book" />
                        <asp:BoundField DataField="BooksReserved.DateOut" HeaderText="Return Date" 
                            FooterStyle-BackColor="#ff0000" DataFormatString="{0:d}" />
                        <asp:TemplateField ShowHeader="false">
                            <ItemTemplate>
                                <asp:LinkButton ID="userProfile" runat="server" OnCommand="userProfile_Command"
                                    CommandArgument='<% Eval("User.Id") %>'
                                    Text="Open User Profile" CssClass="btn btn-success" />
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>

在方法背后的方法Page_Load中,我从3个表中加载了gridview的数据:

protected void Page_Load(object sender, EventArgs e)
{
    // Users Grid
    var query = from u in db.Users
                join br in db.BooksReserveds on u.Id equals br.UserId
                join b in db.Books on br.BookId equals b.Id
                where br.DateOut < DateTime.Today
                orderby br.DateOut
                select new { User = u, BooksReserved = br, Book = b };

    UsersGridView.DataSource = query.ToList();
    UsersGridView.DataBind(); }

点击按钮,我尝试获取ID:

protected void userProfile_Command(object sender, CommandEventArgs e)
{
    try
    {
        int id = Int32.Parse(e.CommandArgument.ToString());
        var query = from u in db.Users
                    where u.Id == id
                    select u;
    } catch(Exception any)
    {
        Console.WriteLine(any.ToString());
    }
}

我遇到了异常:“输入字符串的格式不正确。”

有谁知道为什么?

由于

2 个答案:

答案 0 :(得分:4)

你忘了#。语法应为<%# %>

<asp:LinkButton ID="userProfile" runat="server" OnCommand="userProfile_Command"
   CommandArgument='<%# Eval("User.Id") %>'
   Text="Open User Profile" CssClass="btn btn-success" />

答案 1 :(得分:1)

在您的Page_Load事件处理程序中,您正在select new进入query变量,使该变量成为匿名对象:

// Users Grid
//by using var here and select new this object is anonymous.
var query = from u in db.Users
            join br in db.BooksReserveds on u.Id equals br.UserId
            join b in db.Books on br.BookId equals b.Id
            where br.DateOut < DateTime.Today
            orderby br.DateOut
            select new { User = u, BooksReserved = br, Book = b };

UsersGridView.DataSource = query.ToList();
UsersGridView.DataBind();

您需要一个可以在Gridview的ItemTemplate中强制转换的已定义对象。像这样:

public class UserGridViewModel
{
    public User User { get; set; }
    public BooksReserved BooksReserved { get; set; } //sounds like it could be a collection, but I do not know.
    public Book Book { get; set; }
}

然后,返回到您的Page_Load UserGrid选择查询:

// Users Grid -- Notice the select new UserGridViewModel()
var query = from u in db.Users
        join br in db.BooksReserveds on u.Id equals br.UserId
        join b in db.Books on br.BookId equals b.Id
        where br.DateOut < DateTime.Today
        orderby br.DateOut
        select new UserGridViewModel() { User = u, BooksReserved = br, Book = b };

然后在您的Gridviw中,ItemTemplate本质上是一个命名容器,对于该容器,您需要获取DataItem,它将是一个UserGridViewModel对象(您必须将dataItem强制转换为模型)。请参阅此命令参数的片段(另请注意此服务器打开tage <%#):

CommandArgument='<%# ((UserGridViewModel)Container.DataItem).User.Id %>'

您可以在ItemTemplate中以类似的方式访问UserGridViewModel中的任何其他变量。