asp:Repeater绑定数据源

2018-09-06 11:18

阅读:513

   <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="showRepeater.nection.showRepeater" %>

   <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "
<html xmlns="
<head runat="server">
<title>repeater显示信息</title>
</head>
<body>
<form id="form1" runat="server">
<p>
<asp:Repeater ID="repeater_info" runat="server">
<HeaderTemplate>
<table style="text-align: center">
<tr>
<td nowrap>
名字
</td>
<td nowrap>
性别
</td>
<td nowrap>
年龄
</td>
<td nowrap>
生辰
</td>
<td nowrap>
说明
</td>
<td nowrap>
邮箱
</td>
<td nowrap>
电话
</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:HiddenField ID="HiddenField1" Value='<%# Eval("id")%>' runat="server" />
<%# Eval("names")%>
</td>
<td nowrap>
<%# showSex(Eval("sex").ToString())%>
</td>
<td nowrap>
<%# Eval("age")%>
</td>
<td nowrap>
<%# Eval("birthday")%>
</td>
<td nowrap>
<%# Eval("explain")%>
</td>
<td nowrap>
<%# Eval("email")%>
</td>
<td nowrap>
<%# Eval("telephone")%>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</p>
</form>
</body>
</html>

   using System;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

   namespace bindData.connection
{
public partial class showRepeater : System.Web.UI.Page
{
/// <summary>
/// 连接字符
/// </summary>
string connStr = ConfigurationManager.ConnectionStrings["pubs"].ConnectionString;

   protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindData();
}
}

  
/// <summary>
/// 绑定数据源
/// </summary>
private void bindData()
{
string sql = "select * from company";
SqlConnection sqlConnection = new SqlConnection(connStr);
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sql, connStr);
DataSet ds = new DataSet();
sqlDataAdapter.Fill(ds, "company");
DataView dv = ds.Tables["company"].DefaultView;
repeater_info.DataSource = dv;
repeater_info.DataBind();
}

  
/// <summary>
/// 显示性别:0=女 1=男
/// </summary>
/// <param name="sex"></param>
/// <returns></returns>
protected string showSex(string sex)
{
string sexStr = string.Empty;
if (!string.IsNullOrEmpty(sex.ToString()))
{
if (sex == "0")
{
sexStr = "女";
}
else
{
sexStr = "男";
}
}
return sexStr;
}
}
}


评论


亲,登录后才可以留言!