按钮回发时中继器内的文本框消失

TextBox inside repeater disappeared when button postback

你好开发者我真的需要这个问题的解决方案因为它是我最后一年的社交网站项目问题是

我有一个 Repeater,在 Repeater 里面我有一个文本框和一个按钮 我在中继器外面有一个标签,我在其中显示文本框值但是 当我单击按钮并将其回发到服务器时,标签会在我想显示文本框值的地方消失

这就是我正在尝试的:

ASPX 页面

<%@ Page Language="C#"   AutoEventWireup="true"  CodeBehind="index.aspx.cs" Inherits="SocialSite.index"  MasterPageFile="~/Master.Master"%>



<asp:Repeater runat="server" ID="repeater1">
     <ItemTemplate>

    <asp:TextBox runat="server" ID="txtComment" placeholder="write a comment..."></asp:TextBox>

    <asp:Button runat="server" ID="btnComment" Text="Post" CssClass="btn btn-primary btn-sm" OnClick="btnComment_Click"/>

  </ItemTemplate>
  </asp:Repeater>

这是转发器控件外的Label

<asp:Label runat="server" ID="lblMsg" Text="Not working" ForeColor="Red">    </asp:Label>

代码隐藏:

这是我绑定转发器的页面加载事件

private void Page_Load(object sender, System.EventArgs e)
    {
        if (!IsPostBack)
        {
            DataTable dt = Helper.ExecutePlainQuery("select * from post inner join userregistration on post.uid=userregistration.uid inner join profile on userregistration.uid=profile.uid order by postid DESC");
            repeater1.DataSource = dt;
            repeater1.DataBind();
        }
    }

按钮点击:

protected void btnComment_Click(object sender, EventArgs e)
    {
        foreach (RepeaterItem item in repeater1.Items)
        {
            if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
            {
                TextBox txtName = (TextBox)item.FindControl("txtComment");
                if (txtName != null)
                {
                    lblMsg.Text = txtName.Text;
                }
            }
        }
    }

这里我调试了运行良好的按钮代码我的意思是我在分配给 label/lblMsg 的文本框中输入的值但是在前端标签只是消失了

我搜索了这个问题,但没有找到解决方案....有人说使用 Page Init....Page OnInit ....ViewStateMode....EnableViewState....但不适用于我

请帮忙解决我最后一年的项目问题

点击按钮后重新绑定中继器。您可以制作一个单独的方法来绑定中继器并从 Page_LoadbtnComment_Click:

调用它
protected void btnComment_Click(object sender, EventArgs e)
    {
        foreach (RepeaterItem item in repeater1.Items)
        {
            if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
            {
                TextBox txtName = (TextBox)item.FindControl("txtComment");
                if (txtName != null)
                {
                    lblMsg.Text = txtName.Text;
                }
            }
        }

DataTable dt = Helper.ExecutePlainQuery("select * from post inner join userregistration on post.uid=userregistration.uid inner join profile on userregistration.uid=profile.uid order by postid DESC");
            repeater1.DataSource = dt;
            repeater1.DataBind();
    }

您可以尝试这样做:

protected void btnComment_Click(object sender, EventArgs e)
{
    Button btnComment = sender as Button;
    RepeaterItem item = btnComment.NamingContainer as RepeaterItem;
    TextBox txtComment = item.FindControl("txtComment") as TextBox;
    lblMsg.Text = txtComment.Text;
}

在您的原始代码中,Label 文本在循环中设置为转发器的每个 TextBox 的内容,直到被设置为最终的 TextBox。因此,无论单击哪个按钮,Label 始终显示最后一个转发器项目的文本。如果最后一个 TextBox 为空,则 Label "disappears".