how to reach linkbutton programmatically?

Discussion in 'ASP .Net' started by Ben, Aug 13, 2007.

  1. Ben

    Ben Guest

    Hi,

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

    <Columns>
    <asp:TemplateField><ItemTemplate>
    <asp:LinkButton ID="lb1" runat="server" OnClientClick="return
    confirm(Sure?');"
    CommandName="Delete" Text="go" >
    </asp:LinkButton>
    </ItemTemplate></asp:TemplateField>
    ....

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

    Thanks for help
    Ben
     
    Ben, Aug 13, 2007
    #1
    1. Advertising

  2. Ben

    Teemu Keiski Guest

    Hi,

    see my blog post for background information

    Understanding the naming container hierarchy of ASP.NET databound controls
    http://aspadvice.com/blogs/joteke/a...-hierarchy-of-ASP.NET-databound-controls.aspx

    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
    http://blogs.aspadvice.com/joteke
    http://teemukeiski.net

    "Ben" <ben@sdcsaz> wrote in message
    news:...
    > Hi,
    >
    > i try to reach in code-behind a linkbutton embedded into an ItemTemplate
    > element of a gridview.
    > But i'm stuck ....
    >
    > <Columns>
    > <asp:TemplateField><ItemTemplate>
    > <asp:LinkButton ID="lb1" runat="server" OnClientClick="return
    > confirm(Sure?');"
    > CommandName="Delete" Text="go" >
    > </asp:LinkButton>
    > </ItemTemplate></asp:TemplateField>
    > ...
    >
    > my code:
    > Protected Sub Page_Load(ByVal sender As Object, ByVal e As
    > System.EventArgs) Handles Me.Load
    > Dim tm As TemplateField
    > Dim it As ITemplate
    > Dim lk As LinkButton
    > tm = GridView1.Columns.Item(0)
    > it = tm.ItemTemplate
    > lk=???
    >
    > Thanks for help
    > Ben
    >
     
    Teemu Keiski, Aug 13, 2007
    #2
    1. Advertising

  3. Ben

    Ben Guest

    Thanks for replying

    This is my attempt:

    Dim lk As LinkButton
    Dim gr As GridViewRow
    For Each gr In GridView1.Columns
    lk = CType(gr.FindControl("lb1"), LinkButton)
    lk.Visible = False
    Next

    But i get: Unable to cast object of type
    'System.Web.UI.WebControls.TemplateField' to type
    'System.Web.UI.WebControls.GridViewRow'




    "Teemu Keiski" <> schreef in bericht
    news:%...
    > Hi,
    >
    > see my blog post for background information
    >
    > Understanding the naming container hierarchy of ASP.NET databound controls
    > http://aspadvice.com/blogs/joteke/a...-hierarchy-of-ASP.NET-databound-controls.aspx
    >
    > 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
    > http://blogs.aspadvice.com/joteke
    > http://teemukeiski.net
    >
    > "Ben" <ben@sdcsaz> wrote in message
    > news:...
    >> Hi,
    >>
    >> i try to reach in code-behind a linkbutton embedded into an ItemTemplate
    >> element of a gridview.
    >> But i'm stuck ....
    >>
    >> <Columns>
    >> <asp:TemplateField><ItemTemplate>
    >> <asp:LinkButton ID="lb1" runat="server" OnClientClick="return
    >> confirm(Sure?');"
    >> CommandName="Delete" Text="go" >
    >> </asp:LinkButton>
    >> </ItemTemplate></asp:TemplateField>
    >> ...
    >>
    >> my code:
    >> Protected Sub Page_Load(ByVal sender As Object, ByVal e As
    >> System.EventArgs) Handles Me.Load
    >> Dim tm As TemplateField
    >> Dim it As ITemplate
    >> Dim lk As LinkButton
    >> tm = GridView1.Columns.Item(0)
    >> it = tm.ItemTemplate
    >> lk=???
    >>
    >> Thanks for help
    >> Ben
    >>

    >
    >
     
    Ben, Aug 13, 2007
    #3
  4. Ben

    Ben Guest

    i found it:

    For Each gr In GridView1.Rows







    "Teemu Keiski" <> schreef in bericht
    news:%...
    > Hi,
    >
    > see my blog post for background information
    >
    > Understanding the naming container hierarchy of ASP.NET databound controls
    > http://aspadvice.com/blogs/joteke/a...-hierarchy-of-ASP.NET-databound-controls.aspx
    >
    > 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
    > http://blogs.aspadvice.com/joteke
    > http://teemukeiski.net
    >
    > "Ben" <ben@sdcsaz> wrote in message
    > news:...
    >> Hi,
    >>
    >> i try to reach in code-behind a linkbutton embedded into an ItemTemplate
    >> element of a gridview.
    >> But i'm stuck ....
    >>
    >> <Columns>
    >> <asp:TemplateField><ItemTemplate>
    >> <asp:LinkButton ID="lb1" runat="server" OnClientClick="return
    >> confirm(Sure?');"
    >> CommandName="Delete" Text="go" >
    >> </asp:LinkButton>
    >> </ItemTemplate></asp:TemplateField>
    >> ...
    >>
    >> my code:
    >> Protected Sub Page_Load(ByVal sender As Object, ByVal e As
    >> System.EventArgs) Handles Me.Load
    >> Dim tm As TemplateField
    >> Dim it As ITemplate
    >> Dim lk As LinkButton
    >> tm = GridView1.Columns.Item(0)
    >> it = tm.ItemTemplate
    >> lk=???
    >>
    >> Thanks for help
    >> Ben
    >>

    >
    >
     
    Ben, Aug 13, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex
    Replies:
    2
    Views:
    598
  2. Sathyaish
    Replies:
    3
    Views:
    3,169
    Sathyaish
    Sep 8, 2005
  3. =?Utf-8?B?R2FiZQ==?=

    Programmatically creating a asp.net 2.0 linkbutton

    =?Utf-8?B?R2FiZQ==?=, Jul 20, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    8,637
    tfsmag
    Jul 20, 2006
  4. Sathyaish

    Linkbutton does not look like a linkbutton

    Sathyaish, Sep 8, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    217
    Sathyaish
    Sep 8, 2005
  5. David

    how to reach programmatically a fieldl in a datalist?

    David, Jan 8, 2007, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    740
    David
    Jan 8, 2007
Loading...

Share This Page