Search code examples
asp.netwebformstextboxrepeaterviewstate

TextBox inside repeater disappeared when button postback


Hello developers i really need solution for this problem because its my final year social networking site project the problem is

I have a Repeater, inside Repeater i have one textbox and one button and i have a label outside the repeater where i'm showing textbox value but when i click the button and it postback to server than the label disappears where i wanna show textbox value

this is what i'm trying:

ASPX Page

<%@ 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>

This is the Label outside the repeater control

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

Code Behind:

this is the page load event where i'm binding the repeater

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();
        }
    }

Button 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;
                }
            }
        }
    }

here i debugged the button code which works well i mean the value i entered in the textbox assigned to the label/lblMsg but on Front end the label just disappears

i search for this problem but not get the solution ....some people says that use Page Init....Page OnInit .... ViewStateMode....EnableViewState....but not working for me

please help its my final year project problem


Solution

  • You can try doing it like this:

    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;
    }
    

    In your original code, the Label text is set in a loop to the content of each TextBox of the repeater until being set to the final TextBox. As a result, the Label always displays the text of the last repeater item, no matter which button is clicked. If the last TextBox is empty, the Label "disappears".