how to reach linkbutton programmatically?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ben

    #1

    how to reach linkbutton programmatically?

    Hi,

    i try to reach in code-behind a linkbutton embedded into an ItemTemplate
    element of a gridview.
    But i'm stuck ....

    <Columns>
    <asp:TemplateFi eld><ItemTempla te>
    <asp:LinkButt on ID="lb1" runat="server" OnClientClick=" return
    confirm(Sure?') ;"
    CommandName="De lete" Text="go" >
    </asp:LinkButton>
    </ItemTemplate></asp:TemplateFie ld>
    ....

    my code:
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As
    System.EventArg s) Handles Me.Load
    Dim tm As TemplateField
    Dim it As ITemplate
    Dim lk As LinkButton
    tm = GridView1.Colum ns.Item(0)
    it = tm.ItemTemplate
    lk=???

    Thanks for help
    Ben


  • Teemu Keiski

    #2
    Re: how to reach linkbutton programmaticall y?

    Hi,

    see my blog post for background information

    Understanding the naming container hierarchy of ASP.NET databound controls


    Answer is that you need to loop through GridView's Rows, use FindControl
    against the row to locate the LinkButton. that is if you need to access them
    after they are (the grid is) being bound.

    However if you need to do something like attach event handler to the
    LinkButton, you can (and you should) do it in RowCreated event of GridView
    which is raised for every row, when they are created (instantiated). And if
    you need to set something based o the data to which GridView is bound, you'd
    use RowDataBound event which also is raised for every row but only when
    GridView's DataBind is called.

    --
    Teemu Keiski
    AspInsider, ASP.NET MVP



    "Ben" <ben@sdcsazwrot e in message
    news:eWEh8nd3HH A.1204@TK2MSFTN GP03.phx.gbl...
    Hi,
    >
    i try to reach in code-behind a linkbutton embedded into an ItemTemplate
    element of a gridview.
    But i'm stuck ....
    >
    <Columns>
    <asp:TemplateFi eld><ItemTempla te>
    <asp:LinkButt on ID="lb1" runat="server" OnClientClick=" return
    confirm(Sure?') ;"
    CommandName="De lete" Text="go" >
    </asp:LinkButton>
    </ItemTemplate></asp:TemplateFie ld>
    ...
    >
    my code:
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As
    System.EventArg s) Handles Me.Load
    Dim tm As TemplateField
    Dim it As ITemplate
    Dim lk As LinkButton
    tm = GridView1.Colum ns.Item(0)
    it = tm.ItemTemplate
    lk=???
    >
    Thanks for help
    Ben
    >

    Comment

    • Ben

      #3
      Re: how to reach linkbutton programmaticall y?

      Thanks for replying

      This is my attempt:

      Dim lk As LinkButton
      Dim gr As GridViewRow
      For Each gr In GridView1.Colum ns
      lk = CType(gr.FindCo ntrol("lb1"), LinkButton)
      lk.Visible = False
      Next

      But i get: Unable to cast object of type
      'System.Web.UI. WebControls.Tem plateField' to type
      'System.Web.UI. WebControls.Gri dViewRow'




      "Teemu Keiski" <joteke@aspalli ance.comschreef in bericht
      news:%23N909sd3 HHA.4672@TK2MSF TNGP05.phx.gbl. ..
      Hi,
      >
      see my blog post for background information
      >
      Understanding the naming container hierarchy of ASP.NET databound controls

      >
      Answer is that you need to loop through GridView's Rows, use FindControl
      against the row to locate the LinkButton. that is if you need to access
      them after they are (the grid is) being bound.
      >
      However if you need to do something like attach event handler to the
      LinkButton, you can (and you should) do it in RowCreated event of GridView
      which is raised for every row, when they are created (instantiated). And
      if you need to set something based o the data to which GridView is bound,
      you'd use RowDataBound event which also is raised for every row but only
      when GridView's DataBind is called.
      >
      --
      Teemu Keiski
      AspInsider, ASP.NET MVP


      >
      "Ben" <ben@sdcsazwrot e in message
      news:eWEh8nd3HH A.1204@TK2MSFTN GP03.phx.gbl...
      >Hi,
      >>
      >i try to reach in code-behind a linkbutton embedded into an ItemTemplate
      >element of a gridview.
      >But i'm stuck ....
      >>
      ><Columns>
      ><asp:TemplateF ield><ItemTempl ate>
      ><asp:LinkButto n ID="lb1" runat="server" OnClientClick=" return
      >confirm(Sure?' );"
      > CommandName="De lete" Text="go" >
      > </asp:LinkButton>
      ></ItemTemplate></asp:TemplateFie ld>
      >...
      >>
      >my code:
      >Protected Sub Page_Load(ByVal sender As Object, ByVal e As
      >System.EventAr gs) Handles Me.Load
      > Dim tm As TemplateField
      > Dim it As ITemplate
      > Dim lk As LinkButton
      > tm = GridView1.Colum ns.Item(0)
      > it = tm.ItemTemplate
      > lk=???
      >>
      >Thanks for help
      >Ben
      >>
      >
      >

      Comment

      • Ben

        #4
        Re: how to reach linkbutton programmaticall y?

        i found it:

        For Each gr In GridView1.Rows







        "Teemu Keiski" <joteke@aspalli ance.comschreef in bericht
        news:%23N909sd3 HHA.4672@TK2MSF TNGP05.phx.gbl. ..
        Hi,
        >
        see my blog post for background information
        >
        Understanding the naming container hierarchy of ASP.NET databound controls

        >
        Answer is that you need to loop through GridView's Rows, use FindControl
        against the row to locate the LinkButton. that is if you need to access
        them after they are (the grid is) being bound.
        >
        However if you need to do something like attach event handler to the
        LinkButton, you can (and you should) do it in RowCreated event of GridView
        which is raised for every row, when they are created (instantiated). And
        if you need to set something based o the data to which GridView is bound,
        you'd use RowDataBound event which also is raised for every row but only
        when GridView's DataBind is called.
        >
        --
        Teemu Keiski
        AspInsider, ASP.NET MVP


        >
        "Ben" <ben@sdcsazwrot e in message
        news:eWEh8nd3HH A.1204@TK2MSFTN GP03.phx.gbl...
        >Hi,
        >>
        >i try to reach in code-behind a linkbutton embedded into an ItemTemplate
        >element of a gridview.
        >But i'm stuck ....
        >>
        ><Columns>
        ><asp:TemplateF ield><ItemTempl ate>
        ><asp:LinkButto n ID="lb1" runat="server" OnClientClick=" return
        >confirm(Sure?' );"
        > CommandName="De lete" Text="go" >
        > </asp:LinkButton>
        ></ItemTemplate></asp:TemplateFie ld>
        >...
        >>
        >my code:
        >Protected Sub Page_Load(ByVal sender As Object, ByVal e As
        >System.EventAr gs) Handles Me.Load
        > Dim tm As TemplateField
        > Dim it As ITemplate
        > Dim lk As LinkButton
        > tm = GridView1.Colum ns.Item(0)
        > it = tm.ItemTemplate
        > lk=???
        >>
        >Thanks for help
        >Ben
        >>
        >
        >

        Comment

        Working...