locked
How to find label with FindControl in GridView RRS feed

  • Question

  • User643189141 posted

    This code not found:

            Label lbl = this.PregledNalogovGrid.FindControl("lblVoznikZacetnice") as Label;
    
            lbl.Visible = true;
    
    <asp:GridView ID="PregledNalogovGrid" BorderWidth="0px"  runat="server" AutoGenerateColumns="False" DataKeyNames="ID,IdSifra" Visible="false"
                OnRowUpdated="PregledNalogovGrid_RowUpdated" OnRowDeleting="ViewPregledNalogov_RowDeleting" onrowcommand="ViewPregledNalogov_RowCommand"
                EnableModelValidation="True" GridLines="Vertical"  SkinID="Skins"
                onselectedindexchanged="ProductsGrid_SelectedIndexChanged">
    
                <HeaderStyle CssClass="GridView_header"/>
                <FooterStyle CssClass="GridView_footer"/>
                <RowStyle CssClass="GridView_row"/>
                <AlternatingRowStyle CssClass="GridView_alt-row"/>
                <Columns>
    
    
    <asp:TemplateField HeaderText="Status">
        <ItemTemplate>
            <asp:LinkButton ID="LinkButton1" CommandName="UrediPotniNalog" runat="server"><%# "True" .Equals(Convert.ToString(Eval("Zakljucen"))) ? "Zaključen" : "Ni zaključen"%> </asp:LinkButton>
        </ItemTemplate>
        <ItemStyle Width="90px" />
    </asp:TemplateField>
    
    
    <asp:TemplateField HeaderText="Številka">
        <ItemTemplate>
            <asp:Label ID="Label9" runat="server"><%# Eval("Stevilka")%>/<%# Eval("Leto") %> </asp:Label> <asp:Label ID="lblVoznikZacetnice" runat="server"><%# Eval("VoznikZacetnice")%>-S </asp:Label>
        </ItemTemplate>
        <ItemStyle Width="90px" />
    </asp:TemplateField>
    Monday, January 23, 2012 12:17 PM

Answers

  • User34618493 posted

    http://www.faqssite.com/asp-net-2005/findcontrol-in-rowdatabound-event-of-gridview-t-52.html

    http://btewary.blogspot.com/2010/03/how-to-find-control-on-gridview.html

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 25, 2012 10:32 AM

All replies

  • User551462331 posted

    since lblVoznikZacetnice label is in itemtemplate.... this would be present in every row of gridview.... label from which row would this return

    Label lbl = this.PregledNalogovGrid.FindControl("lblVoznikZacetnice") as Label;

    instead.... refer any one of row of gridview and then use findcontrol... like this

    Label lbl = (Lable)this.PregledNalogovGrid.Rows[1].FindControl("lblVoznikZacetnice");

    index of Rows would be depend on which row number you want to read from...

    hope this helps...

    Monday, January 23, 2012 12:35 PM
  • User643189141 posted

    I was trying your code but returns error: Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index


        protected void Page_Load(object sender, EventArgs e)
        {     Label lblVoznikZacetnice = (Label)PregledNalogovGrid.Rows[1].FindControl("lblVoznikZacetnice");

    I think I have label in rows 1 ?

    Monday, January 23, 2012 12:41 PM
  • User551462331 posted

    where are you populating grid data? unless you do PregledNalogovGrid.DataBind(), you would not get anything in gridview...

    what is the requirement.... you might need to use gridview's databount event to do this

    hope this helps...

    Monday, January 23, 2012 12:43 PM
  • User643189141 posted

    The problem was, because the code has been before the databind.
    Now the problem with your code because  rows do not works.

    If I write [0] to deleted the first line, if you write [10] the tenth row is deleted.

    I tried something like this:
                     LblVoznikZacetnice label = (Label) PregledNalogovGrid.Columns [1]. FindControl ("lblVoznikZacetnice");
                     lblVoznikZacetnice.Visible = false;

    but does not work.
    If I have data in GridView1 and i want to call via lblVoznikZacetnice, I want to delete all data.

    Please help

    Monday, January 23, 2012 1:01 PM
  • User-533242366 posted

    What exactly are you trying to do.  To show or hide the rows in the Gridview has to be achived  in the GridView rowDatabound event, You cannot hide the rows in the Page load event..

    Monday, January 23, 2012 1:13 PM
  • User-1673382244 posted

    Hi

    try this

    Label lblParentID = (Label)gvwUsers.Rows[1].FindControl("lblParentID");
    Monday, January 23, 2012 1:37 PM
  • User643189141 posted

    Hello,

    I tried to use RowDataBound and works. But lblVoznikZacetnice.Visible = true; --- works true/false

    Not works if I want to use statement.

    For test I was trying Button7.Text = "dsadas"; and works.

    What is the problem here?

    protected void CustomersGridView_RowDataBound(Object sender, GridViewRowEventArgs e)    {
    
            var check = (from p in db.tbl_nastavitves
                         where p.Idkreiranopodjetje == Convert.ToInt32(Session["ID"]) && p.DodatneOznakeZaSluzbenoPotovanje == true
                         select p);
    
    
    
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    Label lblVoznikZacetnice = (Label)e.Row.Cells[1].FindControl("lblVoznikZacetnice");
                    if (check.Count() != 0) { lblVoznikZacetnice.Visible = true; };
                    Button7.Text = "dsadas";
                }
        }
    Monday, January 23, 2012 1:42 PM
  • User16212438 posted

    Have you tried taking Cells[1] out of the line that does the FindControl?

    Wednesday, January 25, 2012 8:39 AM
  • User34618493 posted

    http://www.faqssite.com/asp-net-2005/findcontrol-in-rowdatabound-event-of-gridview-t-52.html

    http://btewary.blogspot.com/2010/03/how-to-find-control-on-gridview.html

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 25, 2012 10:32 AM