How to get Text from a Listview?

Asked

Viewed 853 times

7

How can I catch the Text of my Label that is inside a ListView without using the FindControl (Item.FindControl("lblCodigoEdit") as Label).Text), or is access directly the value it has in mine lblCodigoEdit ?

foreach (var Item in livCamposForm.Items)
{
    var dataKey = livCamposForm.DataKeys[Item.DataItemIndex];
    if (dataKey == null)
        continue;

    var IdCampoFormulario = (Int32)dataKey.Value;

    var qrCampsForm = DBCtx.tb_CamposFormulario.Find(IdCampoFormulario);

    Int32 Conta = Convert.ToInt32((Item.FindControl("lblCodigoEdit") as Label).Text);
}

In Listview

<ItemTemplate>
<tr class="<%#setClass(Container.DataItem)%>">
  <td>
    <asp:Label ID="lblCodigoEdit" class="<%#setClass(Container.DataItem)%>" runat="server"
      Text='<%# Eval("Codigo")%>' />
  </td>
  <td>

The reason for this is to perform better since with the FindControl all control is charged when in reality I just needed the Text of control.

2 answers

6


I put an example of how I would do it without using the FindControl which is what you want.

Well actually data recovery and by itself Datakey of ListView and no more of label.

I want to make it clear that I don’t know if this is the right way and if you will have the performance gain you need, it is necessary to take a test with many records to know.

Aspx

<asp:ListView runat="server" DataKeyNames="CodigoPar, CodigoImpar" ID="livCamposForm">
<ItemTemplate>
    <tr>
        <td>
            <asp:Label ID="lblCodigoPar" runat="server"
                Text='<%# Eval("CodigoPar")%>' />
        </td>
        <td>
            <asp:Label ID="CodigoImpar" runat="server"
                Text='<%# Eval("CodigoImpar")%>' />
        </td>
    </tr>
</ItemTemplate>

<br />
<asp:Button ID="btnFazerAlgumaCoisa" Text="text" runat="server" OnClick="btnFazerAlgumaCoisa_Click" />
<br />
<asp:Label ID="lblPar" runat="server"></asp:Label>
<br />
<asp:Label ID="lblImpar" runat="server"></asp:Label>
<br />

C#

public class Teste
{
    public int CodigoImpar { get; set; }
    public int CodigoPar { get; set; }
}

public partial class WebForm1 : System.Web.UI.Page
{        
    protected void Page_Load(object sender, EventArgs e)
    {
        List<Teste> LstTeste = new List<Teste>
        {
            new Teste(){ CodigoPar = 0, CodigoImpar = 1},
            new Teste(){ CodigoPar = 2, CodigoImpar = 3},
            new Teste(){ CodigoPar = 4, CodigoImpar = 5},
            new Teste(){ CodigoPar = 6, CodigoImpar = 7},
            new Teste(){ CodigoPar = 8, CodigoImpar = 9},                
        };

        livCamposForm.DataSource = LstTeste;
        livCamposForm.DataBind();
    }

    protected void btnFazerAlgumaCoisa_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < livCamposForm.Items.Count; i++)
        {
            var codigoPar = livCamposForm.DataKeys[i].Values["CodigoPar"];
            lblPar.Text += codigoPar; 

            var codigoImpar = livCamposForm.DataKeys[i].Values["CodigoImpar"];
            lblImpar.Text += codigoImpar; 
        }  
    }
}

4

As far as I know there is no way to do this directly, except if you use javascript in your logic. The id of your controller when it is rendered is not the same that you use when developing, because the label is repeated n times according to the amount of items in the listview. You cannot access it directly. To find the control label, you need to tell the server the line and which element to find.

As I said, you can try using Javascript to find the element, using the class as selector or some "data-..." attribute. Perform your logic via JS and put in some hidden field (HiddenField) to use later on the server.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.