Accessing Textboxes in Repeater Control

独自空忆成欢 提交于 2019-11-28 23:04:34

Have you tried something like following on the button click:-

foreach (RepeaterItem item in Repeater1.Items)
{
      TextBox txtName= (TextBox)item.FindControl("txtName");
      if(txtName!=null)
      {
      //do something with txtName.Text
      }
      Image img= (Image)item.FindControl("Img");
      if(img!=null)
      {
      //do something with img
      }
}

/* Where txtName and Img are the Ids of the textbox and the image controls respectively in the repeater.*/

Hope this helps.

.aspx

        <asp:Repeater ID="rpt" runat="server" EnableViewState="False">
        <ItemTemplate>
                <asp:TextBox ID="txtQty" runat="server" /> 
        </ItemTemplate>
        </asp:Repeater>

.cs

        foreach (RepeaterItem rptItem in rpt.Items)
        {
            TextBox txtQty = (TextBox)rptItem.FindControl("txtQty");
            if (txtQty != null) { Response.Write(txtQty.Text); }          
        }

Be sure to add EnableViewState="False" to your repeater, otherwise you will get empty string. (That wasted my time, dont waste yours :) )

On postback, you can iterate over the collection of RepeaterItems in repeater.Items. You could then retrieve each TextBox with code such as

TextBox tbDemo = (TextBox)rptr.Items[index].FindControl("textBox");
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!