I have a repeater that binds to an SqlDataSource
<asp:Repeater runat="server" ID="Repeater" DataSourceID="SqlDataSource"> <ItemTemplate> <asp:HiddenField runat="server" Value='<%# Eval("Code") %>' ID="Code" /> <asp:TextBox ID="NumberNeeded" runat="server" Text='<%# Eval("Needed") %>' /><br /> </ItemTemplate> </asp:Repeater> <asp:Button runat="server" ID="submit" Text="submit" OnClick="submit_Click" /> <asp:SqlDataSource ID="SqlDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="rtvFiltered" SelectCommandType="StoredProcedure"> <SelectParameters> </SelectParameters> </asp:SqlDataSource>
I want to iterate over the relay and get the value in NumberNeeded in submit_Click
protected void submit_Click(object sender, EventArgs e) { this.Repeater.DataBind(); //comment this guy and this.Repeater.Items has no items foreach (RepeaterItem item in this.Repeater.Items) { String code = ((HiddenField)item.FindControl("JournalCode")).Value; // below fails because "NumberNeeded" control doesn't have the Text input by the user. int numNeeded = Int32.Parse(((TextBox)item.FindControl("NumberNeeded")).Text); // Doing other stuff with numNeeded } }
The repeater displays its elements perfectly on the page, but when I click the submit button, this.Repeater.Items is empty (unless I call this.Repeater.DataBind () in this method).
I tried to explicitly bind the data to the Repeater in Page_Load and Page_Init inside and outside the validation! .IsPostBack, and every time I either get no items or no text value.
I had an almost identical setup for working in the past, on a page without a main page. I also noticed that this.Master.EnableViewState is false in the submit_Click method, whether it is set to true in Page_Init or Page_Load. this.EnableViewState is true.
source share