button in gridview's header not fired event
-
I have a gridview that autogeneratecolumns is true.I want to adding button to gridview's header .The problem is button not fired event. My code : Protected Sub grdPoint_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdPoint.RowDataBound If e.Row.RowType = DataControlRowType.Header Then For i As Integer = 9 To 20 Dim btn As New Button AddHandler btn.Click, AddressOf click btn.ID = i btn.Text = e.Row.Cells(i).Text btn.EnableViewState = True btn.CausesValidation = False e.Row.Cells(i).Controls.Add(btn) Next End If End Sub Public Sub click(ByVal sender As Object, ByVal e As System.EventArgs) 'do something End Sub Please help....
where you are binding the grid view? Run time or design time?
Copy and paste is a design error
modified on Wednesday, July 2, 2008 3:41 AM
-
where you are binding the grid view? Run time or design time?
Copy and paste is a design error
modified on Wednesday, July 2, 2008 3:41 AM
-
I have a gridview that autogeneratecolumns is true.I want to adding button to gridview's header .The problem is button not fired event. My code : Protected Sub grdPoint_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdPoint.RowDataBound If e.Row.RowType = DataControlRowType.Header Then For i As Integer = 9 To 20 Dim btn As New Button AddHandler btn.Click, AddressOf click btn.ID = i btn.Text = e.Row.Cells(i).Text btn.EnableViewState = True btn.CausesValidation = False e.Row.Cells(i).Controls.Add(btn) Next End If End Sub Public Sub click(ByVal sender As Object, ByVal e As System.EventArgs) 'do something End Sub Please help....
-
You have created an event called sub click but when does that get assigned to the button?
We are not a Code Charity
I assigned at RowDatabound Event of gridview. Protected Sub grdPoint_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdPoint.RowDataBound If e.Row.RowType = DataControlRowType.Header Then For i As Integer = 9 To 20 Dim btn As New Button AddHandler btn.Click, AddressOf click btn.ID = i btn.Text = e.Row.Cells(i).Text btn.EnableViewState = True btn.CausesValidation = False e.Row.Cells(i).Controls.Add(btn) Next End If End Sub Is it wrong? I don't know where to assigned. Thank you all for answers
-
If you are filling the GridView at Page Load, inside
if not ispostback
block that is the problem. call the function outside the if block..Copy and paste is a design error
-
If you are filling the GridView at Page Load, inside
if not ispostback
block that is the problem. call the function outside the if block..Copy and paste is a design error
-
I assigned at RowDatabound Event of gridview. Protected Sub grdPoint_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdPoint.RowDataBound If e.Row.RowType = DataControlRowType.Header Then For i As Integer = 9 To 20 Dim btn As New Button AddHandler btn.Click, AddressOf click btn.ID = i btn.Text = e.Row.Cells(i).Text btn.EnableViewState = True btn.CausesValidation = False e.Row.Cells(i).Controls.Add(btn) Next End If End Sub Is it wrong? I don't know where to assigned. Thank you all for answers
Well you have created the button at runtime in your row data bound event. But how does the button know which onclick event to use? Either you have done it wrong or I am confused as to what you have done. :) I think you need to add this when your button is created: btn.Click += new EventHandler(sub_Click); We are not a Code Charity
-
I'm sorry. I miss understand .I binding gridview at design time .I use gridview and objectdatasource . My gridview set autogeneratecolumns to true.Sorry I'm new to 2005 . Thank you for you reply
Remove the coding from the RowDataBound and add following at page load and try For i As Integer = 2 To 19 Dim btn As New Button AddHandler btn.Click, AddressOf click btn.ID = i btn.Text = GridView1.HeaderRow.Cells(i).Text btn.EnableViewState = True btn.CausesValidation = False GridView1.HeaderRow.Cells(i).Controls.Add(btn) Next
Copy and paste is a design error
-
Well you have created the button at runtime in your row data bound event. But how does the button know which onclick event to use? Either you have done it wrong or I am confused as to what you have done. :) I think you need to add this when your button is created: btn.Click += new EventHandler(sub_Click); We are not a Code Charity
No .netman, what he doing is right, It is VB.net way. The problem is, he is creating the button on the fly. So when he click on the button it will automatically erased, as HTTP is stateless.
Copy and paste is a design error
-
I have a gridview that autogeneratecolumns is true.I want to adding button to gridview's header .The problem is button not fired event. My code : Protected Sub grdPoint_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdPoint.RowDataBound If e.Row.RowType = DataControlRowType.Header Then For i As Integer = 9 To 20 Dim btn As New Button AddHandler btn.Click, AddressOf click btn.ID = i btn.Text = e.Row.Cells(i).Text btn.EnableViewState = True btn.CausesValidation = False e.Row.Cells(i).Controls.Add(btn) Next End If End Sub Public Sub click(ByVal sender As Object, ByVal e As System.EventArgs) 'do something End Sub Please help....
Thank you for all help. I find 2 place to put the code to addhandler for button at gridview's header. first at Pageload don't use if not page ispostback second at RowCreated this 2 place have same result .I don't know the best place to put this code but my code is complete .Thank you again to all :-D