如何使用asp.net中的转发器动态绑定数据c# [英] How to bind data dynamically using repeater in asp.net c#
问题描述
我可以绑定所有行。但只有最后一行显示在转发器中。
可以在服务器端和客户端提供任何帮助。
以下是我的代码:
I am able to bind all rows. But only the last row gets displayed in repeater.
Can any help in server side and client side.
The following is my code:
List<string> adds = new List<string>();
SqlDataAdapter da1 = new SqlDataAdapter("select..)", con);
SqlCommandBuilder cb1 = new SqlCommandBuilder(da1);
DataSet ds1 = new DataSet("member");
da1.Fill(ds1, "member");
RepDetails.DataSource = ds1.Tables[0];
RepDetails.DataBind();
RepDetails.Visible = true;
服务器端:
server side:
<td>
    Member Id:   <asp:Label ID="lblUser" runat="server" Text='<%#Eval("mem_id")%>' ForeColor="Black"/>
    Phone: <asp:Label ID="lblDate" runat="server" Font-Bold="False" Text='<%#Eval("phone") %>' ForeColor="Black" />
    Name :<asp:textbox ID="Textbox3" runat="server" Text='<%#Eval("mem_name") %>' ForeColor="Black" />
</td>
推荐答案
<asp:Repeater ID="RepDetails" runat="server">
<HeaderTemplate>
<table style=" border:1px solid #df5015; width:500px" cellpadding="0">
<tr style="background-color:#df5015; color:White">
<td colspan="2">
<b>Comments</b>
</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr style="background-color:#EBEFF0">
<td>
<table style="background-color:#EBEFF0;border-top:1px dotted #df5015; width:500px" >
<tr>
<td>
Subject:
<asp:Label ID="lblSubject" runat="server" Text='<%#Eval("Subject") %>' Font-Bold="true"/>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblComment" runat="server" Text='<%#Eval("Comment") %>'/>
</td>
</tr>
<tr>
<td>
<table style="background-color:#EBEFF0;border-top:1px dotted #df5015;border-bottom:1px solid #df5015; width:500px" >
<tr>
<td>Post By: <asp:Label ID="lblUser" runat="server" Font-Bold="true" Text='<%#Eval("UserName") %>'/></td>
<td>Created Date:<asp:Label ID="lblDate" runat="server" Font-Bold="true" Text='<%#Eval("PostedDate") %>'/></td>
</tr>
</table>
</td>
</tr>
<tr>
<td colspan="2"> </td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
private SqlConnection con = new SqlConnection(Data Source = SureshDasari; Initial Catalog = MySampleDB; Integrated Security = true);
protected void Page_Load(object sender,EventArgs e)
{
if(!IsPostBack)
{
BindRepeaterData();
}
}
//此按钮点击事件用于插入注释详细信息并将数据绑定到转发器控件
protected void btnSubmit_Click(object sender,EventAr gs e)
{
con.Open();
SqlCommand cmd = new SqlCommand(insert into Repeater_Table(UserName,Subject,Comment) ,PostedDate)值(@ userName,@ subject,@ comment,@ postedDate),con);
cmd.Parameters.AddWithValue(@ userName,txtName.Text);
cmd.Parameters.AddWithValue(@ subject,txtSubject.Text);
cmd.Parameters.AddWithValue(@ comment,txtComment.Text);
cmd.Parameters.AddWithValue(@ postedDate,DateTime.Now);
cmd.ExecuteNonQuery();
con.Close();
txtName.Text = string.Empty;
txtSubject.Text = string.Empty;
txtComment.Text = string.Empty;
BindRepeaterData();
}
//将数据绑定到转发器控件
protected void BindRepeaterData()
{
con.Open();
SqlCommand cmd = new SqlCommand(select * from Repeater_Table Order By PostedDate desc,con);
DataSet ds = new DataSet();
SqlDataAdap ter da = new SqlDataAdapter(cmd);
da.Fill(ds);
RepDetails.DataSource = ds;
RepDetails.DataBind( );
con.Close();
}
private SqlConnection con = new SqlConnection("Data Source=SureshDasari;Initial Catalog=MySampleDB;Integrated Security=true");
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
BindRepeaterData();
}
}
// This button click event is used to insert comment details and bind data to repeater control
protected void btnSubmit_Click(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = new SqlCommand("insert into Repeater_Table (UserName,Subject,Comment,PostedDate) values(@userName,@subject,@comment,@postedDate)", con);
cmd.Parameters.AddWithValue("@userName", txtName.Text);
cmd.Parameters.AddWithValue("@subject", txtSubject.Text);
cmd.Parameters.AddWithValue("@comment", txtComment.Text);
cmd.Parameters.AddWithValue("@postedDate", DateTime.Now);
cmd.ExecuteNonQuery();
con.Close();
txtName.Text = string.Empty;
txtSubject.Text = string.Empty;
txtComment.Text = string.Empty;
BindRepeaterData();
}
//Bind Data to Repeater Control
protected void BindRepeaterData()
{
con.Open();
SqlCommand cmd = new SqlCommand("select * from Repeater_Table Order By PostedDate desc", con);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
RepDetails.DataSource = ds;
RepDetails.DataBind();
con.Close();
}
试试这个: - http://www.aspdotnet-suresh.com/2012/01/repeater-control-example-in-aspnet .html [ ^ ]
Try this one :- http://www.aspdotnet-suresh.com/2012/01/repeater-control-example-in-aspnet.html[^]
看看这篇文章 http://www.etechpulse.com/2014/04/aspnet-repeater-control-c-example.html [ ^ ]
这篇关于如何使用asp.net中的转发器动态绑定数据c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!