简体   繁体   English

如何从ASP.NET转发器中获取绑定项

[英]How to get the bound item from within an ASP.NET repeater

I have to set a LinkButton's OnClientClick attribute but I don't know what this value is until the LinkButton is bound to. 我必须设置一个LinkBut​​ton的OnClientClick属性,但在LinkBut​​ton绑定之前我不知道这个值是什么。 I'm trying to set the value when the repeater binds, but I can't workout how to get the 'boundItem/dataContext' 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 anyway to find out what the current rows bound item is? 反正有没有找出当前行绑定项目是什么?

Maybe you need to use ItemDataBound event. 也许你需要使用ItemDataBound事件。 It provides RepeaterItemEventArgs argument which has DataItem available 它提供了具有DataItem可用的RepeaterItemEventArgs参数

this.Repeater1.ItemDataBound += Repeater1_ItemDataBound;

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

I assume you are trying to get the value for the row that is currently being databound? 我假设您正在尝试获取当前正在数据绑定的行的值?

You can change your function to: 您可以将功能更改为:

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 now has the value of the field TestValue for the current row that is being databound. valueYouWant现在具有正在数据绑定的当前行的字段TestValue的值。 Using the DataBinding event is the best way to do this compared to the ItemDataBound because you don't have to search for a control and localize the code specifically to a control instead of a whole template. ItemDataBound相比,使用DataBinding事件是执行此操作的最佳方法,因为您不必搜索控件并将代码专门本地化为控件而不是整个模板。

The MSDN library had this as a sample event handler: MSDN库将此作为示例事件处理程序:

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

}

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

As you can see it is a simple demonstration of how to access the data item and get data from it. 如您所见,它是一个简单的演示,说明如何访问数据项并从中获取数据。 Adapting this to your scenario is an exercise left to the reader. 将此调整到您的场景是一个留给读者的练习。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM