joeylu
asked on
Gridview Dynamic LinkButton Control with CommandName Problem
the structure is like this:
Grivdivew
ItemTemplate
PlaceHolder
/ItemTemplate
/Gridview
And then I try to add some dynamic controls into the placehold control from code behind, I use the RowDataBound Event
Private Sub GridView1_RowDataBound(ByV al sender As Object, ByVal e As System.Web.UI.WebControls. GridViewRo wEventArgs ) Handles GridView1.RowDataBound
Dim ph As PlaceHolder = CType(e.Row.Cells(1).FindC ontrol("Pl aceHolder" ), PlaceHolder)
Dim Button1 As New LinkButton
Button1 .Text = "1234567"
Button1 .CommandName = "Select"
ph.Controls.Add(Button1 )
End Sub
The problem is the button CommandName is not functioning. And more strangely, if I add the button manually into the placeholder, like this:
Grivdivew
ItemTemplate
PlaceHolder
<asp:LinkButton id=button1 runat=server text=123456 commandName=select />
/PlaceHolder
/ItemTemplate
/Gridview
This works perfectly.
Any idea?
Grivdivew
ItemTemplate
PlaceHolder
/ItemTemplate
/Gridview
And then I try to add some dynamic controls into the placehold control from code behind, I use the RowDataBound Event
Private Sub GridView1_RowDataBound(ByV
Dim ph As PlaceHolder = CType(e.Row.Cells(1).FindC
Dim Button1 As New LinkButton
Button1 .Text = "1234567"
Button1 .CommandName = "Select"
ph.Controls.Add(Button1 )
End Sub
The problem is the button CommandName is not functioning. And more strangely, if I add the button manually into the placeholder, like this:
Grivdivew
ItemTemplate
PlaceHolder
<asp:LinkButton id=button1 runat=server text=123456 commandName=select />
/PlaceHolder
/ItemTemplate
/Gridview
This works perfectly.
Any idea?
ASKER
tks for the reply, however, my problem is not related to the postback issue, althought the dynamic control has issues with postback
after postback, my linkbutton control contains the commandName property, i can get it to add a client script like this:
OnClientClick = "alert('" & button1 .CommandName.ToString & "');
The problem is if i click the button, it doesn't fire the rowcommand event
after postback, my linkbutton control contains the commandName property, i can get it to add a client script like this:
OnClientClick = "alert('" & button1 .CommandName.ToString & "');
The problem is if i click the button, it doesn't fire the rowcommand event
The button is not firing the rowcommand event because you have given the commandname as "Select". In this case the GridView_SelectedIndexChan ging Event is getting fired and you neede to handle that event. If you want to write the code in Rowcommand Event then you need to define some othe commandName apart from the ones available in the GridView e.g Select, Update, Delete, Cancel.
Hope it helps solve your problem.
Hope it helps solve your problem.
ASKER
hi, tks for point out, i changed the command name "select" to something else, like "Testing"
and in the rowcommand I change the condition to
if e.commandname = "testing" then
.....
still, the same problem
and in the rowcommand I change the condition to
if e.commandname = "testing" then
.....
still, the same problem
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
http://bytes.com/topic/net/answers/779989-asp-vb-net-gridview-dynamic-link-buttons-event-handling