简体   繁体   中英

Can't access TextBox inside Repeater Itemtemplate

I know this question is asked already for a couple of times but i didn't get the solution..

i have tried this below code:

foreach (RepeaterItem item in repeater1.Items)
        {
            TextBox txtName = (TextBox)item.FindControl("txtComment");
            if (txtName != null)
            {
                lblCheck.Text= txtName.Text;
            }
        }

the above code works but return empty...

this code finds the TextBox txtComment really well i mean when i have debug this code the value i entered in the txtComment TextBox assigned to lblCheck Label but than the lblCheck where i'm showing the TextBox value is disappeared don't know whats happened to that Label...

than i search about this problem than someone said that use EnableViewState="False" in the repeater but when i put this attribute now the whole repeater is disappeared....

This is aspx page

  <asp:Repeater runat="server" ID="repeater1" EnableViewState="false">
     <ItemTemplate>
    <div class="Postcomment" style="float:left;background-         color:yellow;padding:5px;width:100%;margin-top:10px">
                    <div style="float:left;background-   color:AppWorkspace;width:100%">

                        <div style="float:left;width:100%">
                            <asp:Image runat="server" ImageUrl='<%#   "Images\\"+Eval("userPic") %>' Width="24" Height="24" />
                            <asp:TextBox runat="server" ID="txtComment"    placeholder="write a comment..." style="margin-top:2.5px;margin-  left:5px;width:92%"></asp:TextBox>

                        </div>
                        <div style="float:right">

                            <input type="button" value="Post"   id="btnComment" class="btn btn-primary btn-sm" />
                        </div>
                    </div>
                    </div>

</ItemTemplate>

so please help me, i'm facing this problem in my final year project so i must have to solved this...

thanks

You need to check Item Type of the Repeater Item. Try out this:

  foreach (RepeaterItem item in repeater1.Items)
       {             
      if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
         {
               TextBox txtName = (TextBox)item.FindControl("txtComment");
                if (txtName != null)
                {
                    lblCheck.Text= txtName.Text;
                }
          }
       }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM