Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / August 2007

Tip: Looking for answers? Try searching our database.

how to reach linkbutton programmatically?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ben - 13 Aug 2007 19:44 GMT
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 - 13 Aug 2007 19:53 GMT
Hi,

see my blog post for background information

Understanding the naming container hierarchy of ASP.NET databound controls
http://aspadvice.com/blogs/joteke/archive/2007/02/25/Understanding-the-naming-co
ntainer-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.

Signature

Teemu Keiski
AspInsider, ASP.NET MVP
http://blogs.aspadvice.com/joteke
http://teemukeiski.net

> Hi,
>
[quoted text clipped - 23 lines]
> Thanks for help
> Ben
Ben - 13 Aug 2007 21:48 GMT
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'

> Hi,
>
[quoted text clipped - 41 lines]
>> Thanks for help
>> Ben
Ben - 13 Aug 2007 22:06 GMT
i found it:

For Each gr In GridView1.Rows

> Hi,
>
[quoted text clipped - 41 lines]
>> Thanks for help
>> Ben

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.