By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,986 Members | 1,905 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,986 IT Pros & Developers. It's quick & easy.

dynamically created linkbutton within custom control

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.