0

I am struggling to word this correctly as I am not really sure what the issue is but I will give it a try.

I have a list of events in a repeater. I have a checkbox above this list which does a postback and displays expired events.

When it posts back, it has the right information in the repeater, but when I click Edit (ItemCommand) it has the wrong event id (instead it shows the id of the event before postback).

I am not sure if there is some kind of id mapping going on with repeaters which is beyond my knowledge.

Here is some of my code to hopefully make things a bit clearer:

    protected void chkShowExpired_CheckedChanged(object sender, EventArgs e)
    {
        rptEvents.DataSource = Events.GetFiltered(null, null, chkShowExpired.Checked, null, null, null, null, "");
        rptEvents.DataBind();
    }

    protected void rptEvents_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        switch (e.CommandName)
        {
            case "DeleteEvent":
                ...
            break;

            case "EditEvent":
                ...
            break;
        }
    }

1 Answers1

0

Well the check box event going to fire before the on index changed event. So in the check box event, you can't directly use the control to get the row/selected index. You would have to wait until the selected index repeater event fires.

HOwever, I drop buttons, checkboxes and all kinds of controls into that repeator.

What I do, is not even bother with the repeater event model.

just do this in your check box event, or button event, or whatever

protected void cmdUpdate_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;

RepeaterItem rItem = (RepeaterItem)btn.Parent;

TextBox tBox = (Textbox)rItem.FindControl("txtHotelName");

response.Write("hotel name = " + tBox.Text);
}

In ohter words, just pluck out the sender. Now, this becomes plain jane asp.net code and buttions. You don't have to care about using the repeater event mode, and triggering say the on indexed changed event of the repeater.

Also, one more cool trick.

When you drop in a check box, drop downllist, button?

Well, since it is inside of the repeater, then we can't do that all wonderful bring up the property sheet or EVEN more simple is say double click on the control that jumps to code behind.

But, you CAN do this cool trick - just use intel-sense:

eg this:

Just type in the event name in the markup, and when you hit "=" sign, then intel-sense will give you a option to create the event like this:

enter image description here

So, you can now click on create new event - it SEEMS like nothing occured, but if you now go to code behind, you see the buttion event (in this example).

So, by picking up sender, and then sender.parent (which is the repeater item), then we really don't care about having the Repeater event model "index change" event fire - you really don't care since you get/have/see full use of the repeater row item by using the .parent of the sender.

The end result? You kind of just code those controls and their events like any other control outside of a repeater.

And once you have your hands on that repeater item (row), then you quite much home free to do whatever you want.

Albert D. Kallal
  • 42,205
  • 3
  • 34
  • 51