开发者

Binding an object to a control

开发者 https://www.devze.com 2023-03-11 06:05 出处:网络
You\'ll have to forgive me if this makes no sense, as I\'m still very new to ASP.NET. I have an arbitrarily long List of items (let\'s say it\'s a list of type Person) which I\'ve figured out how to

You'll have to forgive me if this makes no sense, as I'm still very new to ASP.NET.

I have an arbitrarily long List of items (let's say it's a list of type Person) which I've figured out how to bind to a repeater control and display the item's properties (eg. the name of the person).

What I'm interested to know is how I can kind of "combine" sections and know where data originates from back on the server-side (that probably makes no sense; I'll try again). From the code below, when the user hits a LinkButton alongside a Person's name, the HelloHandler will run. But how can can I know whose name it was that the link is logically associated with on the page? How can I "get" their name?

aspx

<asp:Repeater ID="Queue" runat="server">
 <ItemTemplate>
  <div>
   <%# Eval("Name") %><br />
   <asp:LinkButton runat="server" Text="Say Hello" OnClick="HelloHandler"></asp:LinkButton>
  </div>
 </ItemTemplate>
</asp:Repeater>

cs

public List<Person> People = new List<Person>();

protected void Page_Load(object sender, EventArgs e){

 People.Add(new Person("Albert"));
 People.Add(new Person("Bradley"));
 People.Add(new Person("Chad"));
 //etc...

 Queue.DataSource = People;
 Queue.DataBind();

}

public void HelloHandler(object sender, EventArgs e){
 //?
}

Edi开发者_运维问答t

For clarification, I want to be able to get back to the appropriate Person object in the List from the event handler.


In this kind of situation, you would use the LinkButton.CommandArgument property. You can set the data for each LinkButton with some data and then pull that data out when the button event handler is called.


I think I know what you after, The code below will allow you delete a item from a repeater from the click of a link button.

EDIT : In response to the comment I have changed the code to show how to pass an ID back to the command event.

Change your top line to

<asp:Repeater ID="Queue" runat="server" ItemCommand="rpt_ItemCommand">

and the following line to your link button

<asp:LinkButton id="lbDelete" runat="server" Text="[Delete]" CommandName="delete"></asp:LinkButton>

Then add the following in your cs

To get an ID of the repeater item, you the CommandArgument, set the ID during data bind.

protected void rep_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
      if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
      {
           var lbDelete = (LinkButton)e.Item.FindControl("lbDelete");
           lbDelete.CommandArgument = retailItem.Id.ToString();
    }
}

protected void rpt_ItemCommand(object source, RepeaterCommandEventArgs e) 
{
   if (e.CommandName == "delete")
        {
            LinkButton btn = e.CommandSource as LinkButton;

            if (btn != null)
            {
                var itemID = (string)e.CommandArgument;

                //Do Stuff
            }
        }
}

This will mean you now have this event fire in the code behind when you click the button in the repeater..


Based on what TheAlbear posted, I came up with this in the event handler.

public void rpt_ItemCommand(object source, RepeaterCommandEventArgs e) {
    Person p = People[e.Item.ItemIndex];
}

I don't know if using the index is reliable though.

0

精彩评论

暂无评论...
验证码 换一张
取 消