Mohammadreza
Mohammadreza

Reputation: 3229

save state in postback for a data-bound control

I have a repeater with one delete button and bind it to a list like this:

 page_load()
 {
      list<person> myList = new list<person>()
      myList.add(new person(Id="1",Name="n1"));

    if(!isPostBack) 
    {
      myList.add(new person(Id="2",Name="n2"));
      myRepeater.DataSource = myList;
      myrepeater.DataBind();
    }
      myRepeater.ItemCommand += myHandler;
 }

void AdverticRp_ItemCommand(object source, RepeaterCommandEventArgs e)
 {
    if(e.CommandName == "delete")
    {
       FileUploader fu = myrepeater.FindControl("fu") as FileUploadr;

      // do somthing  ** * but contorls is null refrence ***
    }
 }

the repeater bind successfully and delete button raised correctly but i want get contorls in myrepeater but they are null refrence. i know why. because repeater not binded in postback. what should i do? must save state of repeater in veiwsate? I think im wrong in binding. but what is the correct one? I appreciate for all help.

Upvotes: 0

Views: 317

Answers (2)

Anuj
Anuj

Reputation: 1526

If you disable viewstate, you won't see them unless you databind on every page load. You are getting your values from viewstate

Check link.

Upvotes: 1

Ben Gulapa
Ben Gulapa

Reputation: 1619

Find the control from the repeater's Items, not on the repeater itseld. Try this:

void AdverticRp_ItemCommand(object source, RepeaterCommandEventArgs e)
 {
    if(e.CommandName == "delete")
    {
        RepeaterItem item = e.Item;
        if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
        {
           FileUploader fu = item.FindControl("fu") as FileUploader;

           // do somthing  here
        }
    }
 }

Upvotes: 0

Related Questions