Repeater.Items only fills the Button click event when I have a DataBind in this event, but all the inputs of the text field are empty

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.

+6
source share
2 answers

This was the EnableViewState setting on the main page.

The Page_Load method of the main page this.EnableViewState is set to false. I changed it to a .EnableViewState page and it worked.

 this.EnableViewState = Page.EnableViewState; // add to Master page Page_Load method 

do'h

+1
source

As you state, you do not need to call DataBind() in your code behind, so the problem may be with data retrieval. Does your stored procedure use any parameters? Have you tried adding CancelSelectOnNullParameter="false" to your SqlDataSource?

+2
source

Source: https://habr.com/ru/post/945303/


All Articles