473,396 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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.CommandArgument = col.HeaderTex
cell.Controls.Add(lnk

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

My problem is that when run, the control puts the following :
<a id="LinkID">Here</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 4575
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.WebControls.DataGrid

...

Private Sub MyDataGrid_ItemDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs) Handles
MyBase.ItemDataBound
Dim lnk As New LinkButton
lnk.Text = "Here"
lnk.ID = "LinkID"
AddHandler lnk.Command, AddressOf lnkSort_Command
lnk.CommandName = "cmdSort"

lnk.CommandArgument = col.HeaderText
e.Item.Controls(0).Controls.Add(lnk)

End Sub

Public Sub lnkSort_Command(ByVal sender As System.Object, ByVal e As
System.Web.UI.WebControls.CommandEventArgs)
...
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(cell)
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.None
btn.Font.Underline = True
btn.BackColor = btn.BackColor.Transparent

AddHandler btn.Click, AddressOf btn_Click
cell.Controls.Add(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.None
btn.Font.Underline = True
btn.BackColor = btn.BackColor.Transparent

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

btn.Attributes.Add("OnClick", "javascript:__doPostBack('" & lnk1.ID &
"','')")
cell.Controls.Add(btn)

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

context.Request.Form("__EVENTTARGET")

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
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...
1
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 ...
2
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)...
3
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 =...
4
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...
5
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...
6
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...
3
by: pbd22 | last post by:
Hi. How do I add the runat=server attribute on a buttonfield link dynamically? thanks!
3
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.