How to get Text from a ListView?

7

How can I get the Text of my Label that is within a ListView without using the FindControl ( Item.FindControl("lblCodigoEdit") as Label).Text ), ie directly access the value you have in my 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);
}

No 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 have a better performance since with FindControl all the control is loaded when in fact I only needed the Text of the control.

    
asked by anonymous 01.12.2015 / 12:11

2 answers

6

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

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

I want to make it clear that I do not know if this is the right way and if you will have the performance gain you need, you need to do 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; 
        }  
    }
}
    
04.12.2015 / 16:19
4

As far as I know there is no way to do this directly unless you use javascript in your logic. The id of your control when rendered is not the same as the one you use when developing because label is repeated n times according to the amount of listview items. You can not access it directly. To find the label control, 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 a selector or some "data -..." attribute. Perform your logic via JS and put in some hidden field ( HiddenField ) to use later on the server.

    
04.12.2015 / 14:51