tags:

views:

388

answers:

3

Hi everyone -- I have a page with a repeater in it. I'm writing an event handler so that when the user clicks my WebControl button, the event handler for said button iterates through the items in the repeater using FindControl, then uses some of the controls' values. It seems though, that after the page is loaded, the repeater items populate, but when the button is clicked to post this back, as I iterate through the repeater items, I'm seeing that they're all empty. I don't completely understand the sequencing, but I'm assuming it's because my iteration code is trying to access RepeaterItems that haven't been set yet.

The repeater code is in my OnLoad method. Outside of that, I have my event handler trying to iterate through those items after being clicked. This is essentially what I was trying to do:

protected void MyButton_Click(object sender, EventArgs e)
{
    foreach(RepeaterItem item in MyRepeater.Items)
    {
        MyLabel = (Label)item.FindControl("MyLabel");
    }
}

The button is located in the FooterTemplate of the repeater.

<asp:Button runat="server" OnClick="SubmitChecklist_Click" cssclass="BlueSubmit" id="SubmitChecklist" text="Submit" />

Thanks in advance.

Edit: To clarify, the exact error I'm getting is NullReferenceException, when I try to do something, for instance, Response.Write(MyLabel.Text)

Edit: After looking into it more today, this is what I understand to be happening: The repeater is databound on postback. When I then make selections from the generated dropdownlists and hit my button, it posts back again. At this point, the repeater is databound again to it's initial values. So, if I must postback in order to get the users' selections, how can I go about this in the button's eventhandler so that I can get the selected values before that repeater gets databound again?

+2  A: 

THe problem, it sounds like, is that you may be binding the data to your repeater on load, but not first checking to make sure it isnt a post back.

example:

  1. You request the page. On Load Fires. You bind the data to the repeater.
  2. You maniupulate the data in the reapter then click your button
  3. The page refreshes with the postback, firing the onload event. The data is rebound to your repeater and all previous data entered has been nullified.
  4. the onclick event is triggered and your code tries to retrieve values that no longer exist.

Make sure your databinding code in your onLoad event is nested within an postback check

if (!Page.IsPostBack)
{
   Repeater.DataSource = Datatable;
   Repeater.DataBind();
}
Goblyn27
I think this is where the problem may lie. I do have my databinding happening in a if(Page.IsPostBack){}. When the page is not postback, I display a dropdown list. The user selects an item, then hits submit, firing the postback. Inside the postback, I have the repeater databound based on the users selection. The repeater contains input items, so I have an asp:Button at the footertemplate, and in the eventhandler for that button, I want to use the inputted values from the repeater.
lush
Are you checking to make sure that you are not trying to access the Header or Footer when you iterate through your items. Check each item's ItemType property to make sure that it is either a ListItem or AlternateItem and not a HeaderItem or FooterItem (I may have the object names a tad bit off in this example. Confirm independantly.)
Goblyn27
A: 

I've seen the same thing. I don't understand why, but the data doesn't actually get bound until after all events have fired. I ended up making my data source available at the class level and then indexing.

private DataTable myTable;
protected void Page_Load(object sender, EventArgs e)
{
    //populate dataTable
    if (!IsPostBack)
    {
        //databind to repeater
    }
}

protected void Submit_Click(object sender, EventArgs e)
{
    foreach (RepeaterItem item in repeater1.Items)
    {
        DataRow row = myTable.Rows[item.ItemIndex];
    }
}

Ideal? Certainly not but it works.

Nate
I actually just got it working. Instead of relying on IsPostBack inside OnLoad, I just made two seperate event handlers for the two different states the page would be in after selecting a month.
lush
A: 

Instead of relying on the IsPostBack in my OnLoad, I just seperated all of the different states by putting the databinding of the repeater inside of an event handler after the user selects the first option, rather than relying on the IsPostBack of OnLoad. It was a bit convoluted, but I think I'm doing it the right way this time.

lush