从绑定到Listview的列表中获取值

时间:2011-04-27 12:56:24

标签: asp.net

使用的语言:C#.NET 大家好,   我有一份员工名单如下:

public class Employee
{
   public string EmpID { get; set; }
   public string EmpName { get; set; }
}

List<Employee> emp = new List<Employee>();

现在我将它绑定到列表视图并在列表视图中生成标记的href属性的URL。但是我得到的服务器标签形式不好请帮忙。 asp代码如下:

<asp:ListView ID="lstView" runat="server">
    <ItemTemplate>
        <a runat="server" href="/Employee.aspx?EmpIDId=<%# DataBinder.Eval(Container.DataItem,'EmpID') %>&EmpName=<%# DataBinder.Eval(Container.DataItem,'EmpName') %>"> </a>
    </ItemTemplate>
</asp:ListView>  

1 个答案:

答案 0 :(得分:0)

试试这个

 <a id="A1" runat="server" href='<%#string.Format("Employee.aspx?EmpIDId={0}&EmpName={1}",Eval("EmpID"),Eval("EmpName"))%>'>
                    <%#Eval("EmpName")%>
                </a>

有关完整示例,请查看以下代码

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <script src="Scripts/jquery-1.3.2.min.js" type="text/javascript"></script>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ListView ID="lstView" runat="server">
            <ItemTemplate>
                <a id="A1" runat="server" href='<%#string.Format("Employee.aspx?EmpIDId={0}&EmpName={1}",Eval("EmpID"),Eval("EmpName"))%>'>
                    <%#Eval("EmpName")%>
                </a>
            </ItemTemplate>
        </asp:ListView>
    </div>
    </form>
</body>
</html>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Web.Script.Services;

public partial class Default3 : System.Web.UI.Page
{
    public class Employee
    {
        public string EmpID { get; set; }
        public string EmpName { get; set; }

        public static List<Employee> Employees
        {

            get
            {
                return new List<Employee>()
                {
                    new Employee{EmpID="1",EmpName="X"},
                    new Employee{EmpID="2",EmpName="Y"},
                    new Employee{EmpID="3",EmpName="Z"}
                };
            }
        }




    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            lstView.DataSource = Employee.Employees;
            lstView.DataBind();
        }
    }




}