C# Asp.Net中面向初学者的嵌套转发器
我在asp.net中使用repeater在网页中显示数据,现在我想在树中显示数据,如“主类别->子类别”等 我有两个表注释和重播注释。我能够成功显示评论,但无法显示对这些评论的回复 Asp.Net代码C# Asp.Net中面向初学者的嵌套转发器,c#,asp.net,C#,Asp.net,我在asp.net中使用repeater在网页中显示数据,现在我想在树中显示数据,如“主类别->子类别”等 我有两个表注释和重播注释。我能够成功显示评论,但无法显示对这些评论的回复 Asp.Net代码 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Detail.aspx.cs" Inherits="Detail" %> <!DOCTYPE html> <html xmlns="http://www
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Detail.aspx.cs" Inherits="Detail" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style>
body
{
padding:0;
margin:0;
background-color:whitesmoke;
}
.main
{
border:1px solid black;
background-color:silver;
width:40%;
height:auto;
background-repeat:repeat-y;
padding:0;
margin:0;
}
.question
{
text-align:left;
}
.question .ques
{
padding-left:20px;
}
.question .ans{
padding-left:20px;
}
.comment{
}
panel
{
margin-bottom:20px;
}
table
{
width:50%;
}
td
{
border-bottom:1px solid white;
margin-bottom:10px;
}
.rep
{
top:-20px;
}
</style>
</head>
<body>
<form id="form1" runat="server" style="position:relative; text-align:center;">
<div class="main">
<div class="question">
<asp:Label ID="Question" ForeColor="Red" runat="server" Text="Question"></asp:Label> <asp:Label CssClass="ques" ID="Ques" runat="server" Height="20px" Width="400px"></asp:Label><br />
<asp:Label ID="Answer" ForeColor="Green" runat="server" Text="Answer"></asp:Label> <asp:Label ID="Ans" CssClass="ans" runat="server" Text="" Height="60px" Width="400px"></asp:Label>
</div>
<div class="comment">
<table>
<asp:Panel ID="Panel1" runat="server" Height="140px" Width="378px">
<asp:Repeater ID="repUserComments" runat="server" OnItemCommand="repUserComments_ItemCommand" OnItemDataBound="repUserComments_ItemDataBound">
<ItemTemplate>
<tr>
<td>
<p>
<%#Eval("Username") %>
Commented on
<%#Eval("Date_Created") %>
</p>
<div><%#Eval("Comment") %></div>
<p>
<asp:Button ID="replay" runat="server" CssClass="rep" OnClick="replay_Click" Text="Replay"/>
</p>
</td>
</tr>
<tr>
<td>
<asp:Label ID="repuser" Visible="false" runat="server">Username</asp:Label> <br />
<asp:TextBox ID="uname" runat="server" Visible="false"></asp:TextBox><br />
<asp:Label ID="repcoment" Visible="false" runat="server">Replay</asp:Label> <br />
<asp:TextBox ID="comreplay" runat="server" Visible="false"></asp:TextBox><br />
<asp:Button ID="savereplay" runat="server" Text="Replay Post" Visible="false" OnClick="savereplay_Click"/>
</td>
</tr>
<asp:Repeater ID="replay_comments" runat="server" OnItemDataBound="replay_comments_ItemDataBound">
<ItemTemplate>
<p>
<%#Eval("username") %>
Commented on
<%#Eval("date") %>
</p>
<div><%#Eval("replay") %></div>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
</asp:Panel>
</table>
</div>
<div style="text-align:left; margin-left:40px;">
<asp:Label ID="User" runat="server" Text="User"></asp:Label> <br />
<asp:TextBox ID="username" runat="server" style="margin-bottom: 14px"></asp:TextBox>
<p>
<asp:Label ID="coment" runat="server" Text="Comment"></asp:Label> <br />
<asp:TextBox ID="Comment" runat="server" Height="85px" Width="147px" MaxLength="500" Rows="10"></asp:TextBox>
</p>
<p>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Post Comment" />
</p>
</div>
</div>
</form>
</body>
</html>
更新
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Detail.aspx.cs" Inherits="Detail" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style>
body
{
padding:0;
margin:0;
background-color:whitesmoke;
}
.main
{
border:1px solid black;
background-color:silver;
width:40%;
height:auto;
background-repeat:repeat-y;
padding:0;
margin:0;
}
.question
{
text-align:left;
}
.question .ques
{
padding-left:20px;
}
.question .ans{
padding-left:20px;
}
.comment{
}
panel
{
margin-bottom:20px;
}
table
{
width:50%;
}
td
{
border-bottom:1px solid white;
margin-bottom:10px;
}
.rep
{
top:-20px;
}
</style>
</head>
<body>
<form id="form1" runat="server" style="position:relative; text-align:center;">
<div class="main">
<div class="question">
<asp:Label ID="Question" ForeColor="Red" runat="server" Text="Question"></asp:Label> <asp:Label CssClass="ques" ID="Ques" runat="server" Height="20px" Width="400px"></asp:Label><br />
<asp:Label ID="Answer" ForeColor="Green" runat="server" Text="Answer"></asp:Label> <asp:Label ID="Ans" CssClass="ans" runat="server" Text="" Height="60px" Width="400px"></asp:Label>
</div>
<div class="comment">
<table>
<asp:Panel ID="Panel1" runat="server" Height="140px" Width="378px">
<asp:Repeater ID="repUserComments" runat="server" OnItemCommand="repUserComments_ItemCommand" OnItemDataBound="repUserComments_ItemDataBound">
<ItemTemplate>
<tr>
<td>
<p>
<%#Eval("Username") %>
Commented on
<%#Eval("Date_Created") %>
</p>
<div><%#Eval("Comment") %></div>
<p>
<asp:Button ID="replay" runat="server" CssClass="rep" OnClick="replay_Click" Text="Replay"/>
</p>
</td>
</tr>
<tr>
<td>
<asp:Label ID="repuser" Visible="false" runat="server">Username</asp:Label> <br />
<asp:TextBox ID="uname" runat="server" Visible="false"></asp:TextBox><br />
<asp:Label ID="repcoment" Visible="false" runat="server">Replay</asp:Label> <br />
<asp:TextBox ID="comreplay" runat="server" Visible="false"></asp:TextBox><br />
<asp:Button ID="savereplay" runat="server" Text="Replay Post" Visible="false" OnClick="savereplay_Click"/>
</td>
</tr>
<asp:Repeater ID="replay_comments" runat="server" OnItemDataBound="replay_comments_ItemDataBound">
<ItemTemplate>
<p>
<%#Eval("username") %>
Commented on
<%#Eval("date") %>
</p>
<div><%#Eval("replay") %></div>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
</asp:Panel>
</table>
</div>
<div style="text-align:left; margin-left:40px;">
<asp:Label ID="User" runat="server" Text="User"></asp:Label> <br />
<asp:TextBox ID="username" runat="server" style="margin-bottom: 14px"></asp:TextBox>
<p>
<asp:Label ID="coment" runat="server" Text="Comment"></asp:Label> <br />
<asp:TextBox ID="Comment" runat="server" Height="85px" Width="147px" MaxLength="500" Rows="10"></asp:TextBox>
</p>
<p>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Post Comment" />
</p>
</div>
</div>
</form>
</body>
</html>
我可以使用嵌套转发器,但我知道问题是,如果我在注释1上重播,那么重播也会显示在注释2、注释3等下,但我希望它只显示在指定的注释(注释1)下
我的更新代码是
using System;
using System.Collections.Generic;
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;
public partial class Detail : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("data source=MSZ900; integrated security=true; initial catalog=Test; MultipleActiveResultSets=true");
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack) //----- Make sure you always bind your databound controls after this.
{
con.Open();
string st = Request.QueryString["id"];
SqlCommand cmd = new SqlCommand("select * from faq where Qid='" + st + "'", con);
SqlCommand cmdd = new SqlCommand("select * from comment where Qid='" + st + "'", con);
cmdd.ExecuteNonQuery();
cmd.ExecuteNonQuery();
SqlDataReader drr;
SqlDataReader dr;
dr = cmd.ExecuteReader();
if (dr.Read())
{
Ques.Text = dr["Question"].ToString();
Ans.Text = dr["Answer"].ToString();
}
dr.Close();
drr = cmdd.ExecuteReader();
//---- bind comments
if (drr.HasRows)
{
repUserComments.DataSource=drr;
repUserComments.DataBind();
}
drr.Close();
con.Close();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
con.Open();
string st = Request.QueryString["id"];
DateTime dt = DateTime.Now;
string qry = "insert into comment(Username,Comment,Date_Created,Qid)values('" + username.Text + "','" + Comment.Text + "','" + dt + "','" + st + "')";
SqlCommand cmd = new SqlCommand(qry, con);
cmd.ExecuteNonQuery();
username.Text = string.Empty;
Comment.Text = string.Empty;
Response.Redirect("detail.aspx?id=" + st);
con.Close();
}
protected void repUserComments_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if(e.CommandName=="preplay")
{
if (e.Item.ItemType == ListItemType.Item)
{
con.Open();
TextBox username = (TextBox)e.Item.FindControl("cusername");
TextBox replay = (TextBox)e.Item.FindControl("commentreplay");
string st = Request.QueryString["id"];
DateTime dt = DateTime.Now;
string qry = "insert into comment_replay(username,replay,date)values('" + username.Text + "','" + replay.Text + "','" + dt + "')";
SqlCommand cmd = new SqlCommand(qry, con);
cmd.ExecuteNonQuery();
username.Text = string.Empty;
replay.Text = string.Empty;
con.Close();
}
}
}
protected void replay_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
RepeaterItem rpt = (RepeaterItem)btn.NamingContainer;
Label repu = (Label)rpt.FindControl("repuser");
TextBox user =(TextBox)rpt.FindControl("uname");
Label repcomment = (Label)rpt.FindControl("repcoment");
TextBox cmnt = (TextBox)rpt.FindControl("comreplay");
Button save = (Button)rpt.FindControl("savereplay");
user.Visible = true;
cmnt.Visible = true;
save.Visible = true;
repu.Visible = true;
repcomment.Visible = true;
}
protected void savereplay_Click(object sender, EventArgs e)
{
con.Open();
string st = Request.QueryString["id"];
Button sv = (Button)sender;
RepeaterItem rptt = (RepeaterItem)sv.NamingContainer;
TextBox user = (TextBox)rptt.FindControl("uname");
TextBox cmnt = (TextBox)rptt.FindControl("comreplay");
DateTime dt = DateTime.Now;
string qry = "insert into comment_replay(username,replay,date,Commentid)values('" + user.Text + "','" + cmnt.Text + "','" + dt + "','"+st+"')";
SqlCommand cmd = new SqlCommand(qry, con);
cmd.ExecuteNonQuery();
con.Close();
}
protected void replay_comments_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
}
protected void repUserComments_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)
{
Repeater rept = e.Item.FindControl("replay_comments") as Repeater;
string id = Request.QueryString["id"].ToString();
//string id = DataBinder.Eval(e.Item.DataItem, "Commentid").ToString();
SqlCommand cmd = new SqlCommand("select username,date,replay from comment_replay where Commentid='" +id+ "'", con);
DataTable dt = new DataTable();
SqlDataAdapter dap = new SqlDataAdapter(cmd);
dap.Fill(dt);
rept.DataSource = dt;
rept.DataBind();
}
}
}
单击“Replay”按钮时,
Replay\u click()
只是显示隐藏的元素
当您单击“Replay Post”时,savereplay\u click()
将表单提交给自己,并且由于Request.QueryString[“id”]
是在url上传递的问题id,因此您总是使用错误的id保存Replay
当repUserComments\u ItemDataBound
时,您再次调用Request.QUeryString[“id”]
,这也是url中的问题id。这就是为什么你所有的评论都有相同的回放
您需要更好地构造代码,并在相应的get/post方法上传递正确的id,否则您将始终在数据库中保存错误的信息
编辑
正如在评论中所说,要包含您使用的隐藏字段(不知道您的数据库模式,所以请根据需要评估以更正字段):
您是否尝试过使用以下方法?请阅读我的更新…在您的savereplay中,单击函数确实请求。QueryString[“id”]返回注释的id或用户的id?这是注释id。检查先生,我还尝试了“string id=DataBinder.Eval(e.Item.DataItem,“Commentid”)。ToString();”但结果是一样的..@webydesigny-在中继器上,您可以始终有一个隐藏的输入,并绑定其上的尊敬id。这样,您就可以简单地查找控件并获取值。我相信您在
savereplay\u Click()
中特别需要这个,因为这个人正在为Replay保存数据。
<asp:HiddenField ID="CmntId" runat="server" value='<%#Eval("CommentId") %>' />
<asp:Label ID="repuser" Visible="false" runat="server">Username</asp:Label> <br />
<asp:TextBox ID="uname" runat="server" Visible="false"></asp:TextBox><br />
HiddenField hiddenField = (HiddenField)rptt.FindControl("CmntId");
string st = hiddenField.Value;