Koray Durudogan
Koray Durudogan

Reputation: 634

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. I need to access TeklifIds in code-behind. 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.

Upvotes: 1

Views: 18912

Answers (3)

Nilesh Umaretiya
Nilesh Umaretiya

Reputation: 786

Repeater Code :

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

Code behind : rptBranch_ItemCommand

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

Upvotes: 0

Rahul Singh
Rahul Singh

Reputation: 21825

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. But I am not sure why you are adding the normal html tags directly.

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:-

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

Then, add the ItemDataBound event in your repeatre control like this:-

<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
    }
}

Upvotes: 4

Salah Akbari
Salah Akbari

Reputation: 39976

Place TeklifId in a Label control so that you can use ID and FindControl to get the values like this:

<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
     }
}

Upvotes: 1

Related Questions