简体   繁体   English

在代码隐藏中从中继器获取值

[英]Get a value from Repeater in code-behind

<asp:Repeater ID="Repeater1" runat="server" DataSourceID="EntityDataSourceTeklifler">
     <ItemTemplate>
          <div class="panel panel-primary">
               <div class="panel-body">
                    <strong>Teklif Kodu:</strong>&nbsp;<%#Eval("TeklifId") %><br />
                    <strong>Teklif Tarihi:</strong>&nbsp;<%#Eval("TeklifTarih") %><br />
                    <strong>Teklifi Hazırlayan:</strong>&nbsp;<%#Eval("Name") %>&nbsp;<%#Eval("Surname") %><br />
                    <strong>Firma Adı:</strong>&nbsp;<%#Eval("FirmaAdi") %><br />
                    <strong>Sipariş:</strong>&nbsp;<%#Eval("FUrunId") %><br />
                    <strong>Teklif Tutarı:</strong>&nbsp;<%#Eval("TeklifTutar") %><br />
               </div>
          </div>
    </ItemTemplate>
</asp:Repeater>

As you can see I have a Repeater and it displays my data without a problem.如您所见,我有一个Repeater ,它可以毫无问题地显示我的数据。 I need to access TeklifId s in code-behind.我需要在代码隐藏中访问TeklifId I am going to make something like:我要做类似的事情:

if(TeklifId == 1)
{
  //do something
}
else if(TeklifId == 2)
{
  //do something else
}

And to do this, I need to get all TeklifId while it is adding to the Repeater .为此,我需要在添加到Repeater时获取所有TeklifId

Ideally you should include the data withing some ASP.NET controls like Label, Textbox control within ItemTemplate tag because it is easy to work with them.理想情况下,您应该将数据与一些 ASP.NET 控件(如Label, Textbox控件)一起包含在ItemTemplate标记中,因为它们易于使用。 But I am not sure why you are adding the normal html tags directly.但我不确定你为什么要直接添加普通的 html 标签。

Anyways, to find the value you will have to find it within the ItemDataBound control of repeater control but for that you will have to make the strong tag a server control by adding runat="server" attrribute like this:-无论如何,要找到该值,您必须在转发器控件的ItemDataBound控件中找到它,但为此,您必须通过添加runat="server"属性使strong标记成为服务器控件,如下所示:-

<strong id="TeklifId" runat="server">Teklif Kodu:</strong>&nbsp;<%#Eval("TeklifId") %>

Then, add the ItemDataBound event in your repeatre control like this:-然后,在您的重复控件中添加 ItemDataBound 事件,如下所示:-

<asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand"

Finally in the code behind you can find the value like this:-最后在后面的代码中你可以找到这样的值:-

protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
   if (e.Item.ItemType == ListItemType.AlternatingItem || 
       e.Item.ItemType == ListItemType.Item)
    {
       HtmlGenericControl TeklifId = e.Item.FindControl("TeklifId") as HtmlGenericControl;
       string TeklifId = TeklifId.InnerText;  //value here
    }
}

Place TeklifId in a Label control so that you can use ID and FindControl to get the values like this:TeklifId放在Label控件中,以便您可以使用IDFindControl获取如下值:

<asp:Label ID="TeklifId" runat="server" Text='<%#Eval("TeklifId") %>'></asp:Label>

And then:进而:

foreach (RepeaterItem item in Repeater1.Items)
{
     var TeklifId = (Label)item.FindControl("TeklifId");
     if (TeklifId == 1)
     {
         //do something
     }
}

Repeater Code :中继器代码:

<td>
   <span runat="server" id="lbBranchname" style="font-style:italic;"><%# Eval("branchname")%></span>
</td>

Code behind : rptBranch_ItemCommand背后的代码:rptBranch_ItemCommand

HtmlGenericControl lbBranchname = e.Item.FindControl("lbBranchname") as HtmlGenericControl;
BranchName = lbBranchname.InnerText;

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

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