473,785 Members | 2,388 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

dynamically created linkbutton within custom control

I have created a control that inherits from datagrid. Before I render the grid html I want to create a linkbutton. I am using the following code to do so

Dim lnk As New LinkButto
lnk.Text = "Here
lnk.ID = "LinkID
AddHandler lnk.Command, AddressOf lnkSort_Comman
lnk.CommandName = "cmdSort

lnk.CommandArgu ment = col.HeaderTex
cell.Controls.A dd(lnk

I then also have the following event hander sub
Private Sub lnkSort_Command (ByVal sender As Object, ByVal e As CommandEventArg s
End Su

My problem is that when run, the control puts the following :
<a id="LinkID">Her e</a> in the html. Obviously i do not have any postback event. What am i doing wrong. If i do this within a regular aspx page it seems to be just fine. Again, this is within a inherited control

th
dav

Jul 21 '05 #1
3 4610
Hi Dave,

Thank you for using the community. I am Luke and I am review this issue
currently. As I understand, the problem is how to add eventhandler to a
Linkbutton in custom DataGrid control. Your code is correct, and the key
point is where we put it. Normally, ItemDataBound is the proper place we
put the code:

Public Class MyDataGrid
Inherits System.Web.UI.W ebControls.Data Grid

...

Private Sub MyDataGrid_Item DataBound(ByVal sender As Object, ByVal e As
System.Web.UI.W ebControls.Data GridItemEventAr gs) Handles
MyBase.ItemData Bound
Dim lnk As New LinkButton
lnk.Text = "Here"
lnk.ID = "LinkID"
AddHandler lnk.Command, AddressOf lnkSort_Command
lnk.CommandName = "cmdSort"

lnk.CommandArgu ment = col.HeaderText
e.Item.Controls (0).Controls.Ad d(lnk)

End Sub

Public Sub lnkSort_Command (ByVal sender As System.Object, ByVal e As
System.Web.UI.W ebControls.Comm andEventArgs)
...
End Sub

You amy try above code to see if it can help.

Luke
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
Jul 21 '05 #2
Well, the problem may be a bit more complex. The custom control's primary goal was to have non-scrollable headings. The control works fine from this respect. WIthin the Sub Render I am creating the headings for the control in a table format. I am then appending the grid contents (without headings) then closing the table. In effect I am surrounding the grid contents with a div element that can scroll. This works fine.

I am using the following code to create the headings within the Render event. In this sample i am using a button instead of linkbutton.

The button renders ok (i could not get the linkbutton to render with postback). I still have the problem of the event is not wired to the button.

Any help is appreciated.

Dim col As DataGridColumn

For Each col In cols
Dim cell As New TableCell
row.Cells.Add(c ell)
Dim cellWidth As Integer = Convert.ToInt32 (CalculateWidth (col, cols, i).Value)
i = i + 1
If i = cols.Count Then
cellWidth = cellWidth + ScrollbarWidth
End If
Dim btn As New Button
btn.Text = "hello"
btn.BorderStyle = BorderStyle.Non e
btn.Font.Underl ine = True
btn.BackColor = btn.BackColor.T ransparent

AddHandler btn.Click, AddressOf btn_Click
cell.Controls.A dd(btn)
Next
Jul 21 '05 #3
Within this situation, we may consider use some other things instead of
addhandler. For example, attach the click the event manually:

1. Set different ID for eache button in the Table:

For Each col In cols
...
i = i + 1
...
Dim btn As New Button
btn.Text = "hello"
btn.BorderStyle = BorderStyle.Non e
btn.Font.Underl ine = True
btn.BackColor = btn.BackColor.T ransparent

btn.ID= "LinkID" & i.ToString()
2. add the OnClick attribute:

btn.Attributes. Add("OnClick", "javascript:__d oPostBack('" & lnk1.ID &
"','')")
cell.Controls.A dd(btn)

3. If the controls's Load or render method check:

context.Request .Form("__EVENTT ARGET")

If we found this string leads with LinkID, the postback should be fired by
the buttons.

Luke
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
Jul 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
15679
by: Jerad Rose | last post by:
I searched for a while trying to find the answer to this, but to no avail. I am trying to find the best way (or any way) to dynamically show and hide groups of TR's. For example, I have a section of about 6 table rows that all need to be hidden if a certain condition is met. But I don't want to hide the whole table. And, I don't want to split up the table because the columns need to remain intact. I thought about using runat=server...
1
2589
by: Gopal Krish | last post by:
I'm have coded a simple menu (using link buttons as menu items) in a user control to be reused across many ASPX pages. In the page_load method I dynamically create the link buttons as follows LinkButton myLB = new LinkButton(); ......... .........
2
1949
by: djc | last post by:
On the page_load event I am querying a database and binding data to some text boxes, list boxes, and a repeater control. When the page loads it uses the value of one of the database fields (status) to determine what options should be available for this particular item (which is an issue... small issue tracking system). Each of these options is an action that may be performed on the issue and I am dynamically creating LinkButtons for each...
3
347
by: dave | last post by:
I have created a control that inherits from datagrid. Before I render the grid html I want to create a linkbutton. I am using the following code to do so Dim lnk As New LinkButto lnk.Text = "Here lnk.ID = "LinkID AddHandler lnk.Command, AddressOf lnkSort_Comman lnk.CommandName = "cmdSort lnk.CommandArgument = col.HeaderTex cell.Controls.Add(lnk
4
2222
by: Mike Lowery | last post by:
I have an ASP.Net page that simply generates a dynamic page using Response.Write() statements to generate the HTML. This works great except that one of the things I want to generate is a LinkButton. I know this can be done by creating a Panel and adding it using Panel.Controls.Add(LinkButton) but the problem is there are no controls on the page--I want to dynamically add everything using simple Response.Write statements. But it doesn't...
5
4717
by: =?Utf-8?B?TWFyYyBXb29sZnNvbg==?= | last post by:
Hi, I have a strange issue occurring with LinkButtons that are dynamically added to each (data) row of my DataGrid on that grid's ItemDataBound event. Each LinkButton is assigned its own event handler to deal with the Command event, but for some reason I can get this to work in a C# project but not in a newly created VB.NET one. The relevant VB.NET is as follows:
6
2599
by: ree321 | last post by:
I have a linkbutton which is added to a column in a datagrid dynamically using a template I created. When I change the data in it by not databinding and then when I later retreive the data from the control after a post back it goes back to the value which was dynamically bounded. I have set up similar process with a checkbox template and it remebers the value. Are Linkbuttons not capable of this? and should I use something else?
3
3534
by: pbd22 | last post by:
Hi. How do I add the runat=server attribute on a buttonfield link dynamically? thanks!
3
5275
by: Allen Chen [MSFT] | last post by:
Hi Richard, Quote from Richard================================================== However I also want to be able to remove the panes. I have tried to include this, but find that when I first add the pane the remove event does not get handled, though thereafter it is handled without problems. ==================================================
0
9647
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9491
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10163
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10104
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9959
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7510
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5532
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4063
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3668
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.