如何刷新asp.net中的2个转发器

时间:2015-04-06 09:50:39

标签: asp.net

我正在用asp构建一个社交网络。我使用转发器控件在每个状态后编码主页面都有帖子状态和注释,但是当我点击“发布”按钮和“注释”按钮时,转发器控件不刷新。我注销并再次登录以显示新状态和新评论。我该如何解决这个问题:

MainPage.aspx

<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
<h2>Hello</h2>
<asp:Label ID="lblUser" runat="server" Text=""></asp:Label>
<asp:Label ID="lblID" runat="server" Text=""></asp:Label>
<hr />
<asp:TextBox ID="txtMessage" runat="server" TextMode="MultiLine"></asp:TextBox>
<asp:Button ID="btnPost" runat="server" Text="Post" OnClick="btnPost_Click" />
<hr />
<asp:Repeater ID="repeater" runat="server" OnItemDataBound="repeater_ItemDataBound">
    <HeaderTemplate>
        <ul>
    </HeaderTemplate>
    <FooterTemplate>
        </ul>
    </FooterTemplate>
    <ItemTemplate>
        <asp:Label ID="labelID" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "Mess_Id") %>'></asp:Label>
        <li><%#DataBinder.Eval(Container.DataItem, "Message")%></li>
        <asp:Repeater ID="repeatComment" runat="server">
            <HeaderTemplate>
                <ul>
            </HeaderTemplate>
            <FooterTemplate>
                </ul>
            </FooterTemplate>
            <ItemTemplate>
                <li>
                    <%#DataBinder.Eval(Container.DataItem, "Comments")%>
                </li>
            </ItemTemplate>
        </asp:Repeater>
        <br />
        <asp:TextBox ID="txtComment" runat="server"></asp:TextBox>
        <asp:Button ID="btnComment" runat="server" Text="Comment" OnClick="btnComment_Click" />
        <br />
    </ItemTemplate>
</asp:Repeater>

MainPage.aspx.cs

private List<Message> list;
private ESupervisionEntities db;
private MessageController message;
int id;
private System.Web.UI.WebControls.Repeater repeatComment;

protected void Page_Load(object sender, EventArgs e)
{
    if (Session["USER_NAME"] != null)
    {
        lblUser.Text = Session["USER_NAME"].ToString();
        lblID.Text = Session["USER_ID"].ToString();
    }
    if (!IsPostBack)
    {
        db = new ESupervisionEntities();
        message = new MessageController();
        id = Int32.Parse(lblID.Text);
        list = message.getAllMyByID(db, id, 2);
        repeater.DataSource = list;
        repeater.DataBind();
    }
}
protected void repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    RepeaterItem item = e.Item;
    if ((item.ItemType == ListItemType.Item) ||
        (item.ItemType == ListItemType.AlternatingItem))
    {
        repeatComment = (Repeater)item.FindControl("repeatComment");
        var myLabel = e.Item.FindControl("labelID") as Label;
        int num = Int32.Parse(myLabel.Text);
        List<MessComment> lst = message.getAllCommentByID(db, id, num);
        repeatComment.DataSource = lst;
        repeatComment.DataBind();
    }
}
protected void btnPost_Click(object sender, EventArgs e)
{
    db = new ESupervisionEntities();
    message = new MessageController();
    id = Int32.Parse(lblID.Text);
    message.createMess(db, id, 2, txtMessage.Text, true);
}

protected void btnComment_Click(object sender, EventArgs e) {
    var btn = (Button)sender;
    var item = (RepeaterItem)btn.NamingContainer;
    Button comment = (Button)item.FindControl("btnComment");
    TextBox txtComment = (TextBox)item.FindControl("txtComment");
    Label msgID = (Label)item.FindControl("labelID");
    db = new ESupervisionEntities();
    message = new MessageController();
    id = Int32.Parse(lblID.Text);
    message.createComment(db, int.Parse(msgID.Text), id, 2, txtComment.Text, true);
}

1 个答案:

答案 0 :(得分:1)

试试这段代码......

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

private void BindData()
{
    db = new ESupervisionEntities();
    message = new MessageController();
    id = Int32.Parse(lblID.Text);
    list = message.getAllMyByID(db, id, 2);
    repeater.DataSource = list;
    repeater.DataBind();
}
protected void btnPost_Click(object sender, EventArgs e)
{

    db = new ESupervisionEntities();
    message = new MessageController();
    id = Int32.Parse(lblID.Text);
    message.createMess(db, id, 2, txtMessage.Text, true);

    BindData();
}
protected void btnComment_Click(object sender, EventArgs e)
{
    var btn = (Button)sender;
    var item = (RepeaterItem)btn.NamingContainer;
    Button comment = (Button)item.FindControl("btnComment");
    TextBox txtComment = (TextBox)item.FindControl("txtComment");
    Label msgID = (Label)item.FindControl("labelID");
    db = new ESupervisionEntities();
    message = new MessageController();
    id = Int32.Parse(lblID.Text);
    message.createComment(db, int.Parse(msgID.Text), id, 2, txtComment.Text, true);

    BindData();
}