How to get related item from ASP.NET repeater

I need to set the LinkButton attribute OnClientClick, but I do not know what this value is until the LinkButton is bound. I am trying to set the value when the relay communicates, but I cannot train how to get the value "boundItem / dataContext" ...

<asp:Repeater ID="Repeater1" runat="server">
    <ItemTemplate>
        <asp:LinkButton  Text="HelloWorld" ID="Hyper1" runat="server" OnDataBinding="Repeater1_DataBinding" >
        </asp:LinkButton> 
    </ItemTemplate> 
</asp:Repeater>

protected void Page_Load(object sender, EventArgs e)
{
    var list = new List<TestObject>();
    list.Add(new TestObject() {TestValue = "testing1"});
    list.Add(new TestObject() { TestValue = "testing2" });
    list.Add(new TestObject() { TestValue = "testing3" });

    this.Repeater1.DataSource = list;
    this.Repeater1.DataBind();
}

public void Repeater1_DataBinding(object sender, EventArgs e)
{
    var link = sender as HyperLink;
    //link.DataItem ???
}

Is there any way to find out what is related to the current lines?

+3
source share
3 answers

Maybe you need to use the ItemDataBound event. It provides the RepeaterItemEventArgs argument, which has an available DataItem.

this.Repeater1.ItemDataBound += Repeater1_ItemDataBound;

void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    var dataItem = e.Item.DataItem;
}
+5
source

, , ?

:

public void Repeater1_DataBinding(object sender, EventArgs e) 
{ 
    var link = sender as HyperLink;
    string valueYouWant = Eval("TestValue").ToString();

    // You could then assign the HyperLink control to whatever you need
    link.Target = string.Format("yourpage.aspx?id={0}", valueYouWant);
} 

valueYouWant TestValue , . DataBinding - ItemDataBound, , .

+3

MSDN :

public void BindData(object sender, EventArgs e)
{
    Literal l = (Literal) sender;
    DataGridItem container = (DataGridItem) l.NamingContainer;
    l.Text = ((DataRowView) container.DataItem)[column].ToString();

}

(. http://msdn.microsoft.com/en-us/library/system.web.ui.control.databinding.aspx)

, , . - , .:)

+1

Source: https://habr.com/ru/post/1751380/


All Articles