We help IT Professionals succeed at work.

Gridview Dynamic LinkButton Control with CommandName Problem

joeylu
joeylu asked
on
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(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
            Dim ph As PlaceHolder = CType(e.Row.Cells(1).FindControl("PlaceHolder"), 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?
Comment
Watch Question

Author

Commented:
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
Kaushal AroraTechnical Analyst

Commented:
The button is not firing the rowcommand event because you have given the commandname as "Select". In this case the GridView_SelectedIndexChanging 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.

Author

Commented:
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
Technical Analyst
Commented:
Try to add the button in the RowCreated Event rather in the RowDataBound event.
I think it should do the trick.