Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Repeater.Items only populates in Button click event when I DataBind in that event, but all textbox inputs are empty

I have a repeater that is databound to a 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 repeater 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 items perfectly on the page, but when I click the submit button, this.Repeater.Items is empty (unless I call this.Repeater.DataBind() in that method).

I've tried explicitly data binding Repeater in Page_Load and Page_Init within and outside of a !Page.IsPostBack check, and everytime I either get no Items, or no Text value.

I have gotten an almost identical setup to work in the past, on a page without a master page. I've also noticed that this.Master.EnableViewState is false in the submit_Click method, no matter if I set it to true in Page_Init or Page_Load. this.EnableViewState is true.

like image 843
user467384 Avatar asked May 17 '13 21:05

user467384


2 Answers

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

like image 117
Jon Susiak Avatar answered Oct 08 '22 08:10

Jon Susiak


It was the EnableViewState setting on the master page.

In the Page_Load method of the master page this.EnableViewState was getting set to false. I changed it to Page.EnableViewState and everything worked.

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

do'h

like image 37
user467384 Avatar answered Oct 08 '22 09:10

user467384