0

I am trying to add a "super header" row to my gridview. It seems I can only do this in codebehind. In the header, I want to add a textbox and attach a textchange event handler. But, while the code generates the header row and textbox, it does not seem to attached the event handler. Here is my code:

Protected Sub GridViewBegroting_DataBound(ByVal sender As Object, ByVal e As EventArgs) Handles testGrid.DataBound
    Dim myGridView As GridView = sender
    If myGridView.Controls.Count > 0 Then
        AddSuperHeader(myGridView)
    End If
End Sub

Protected Sub AddSuperHeader(ByVal gridView As GridView)
    Dim myTable As Table = gridView.Controls(0)
    Dim cell As TableHeaderCell = MakeCell("Search", 4)
    Dim tb As HtmlGenericControl = MakeTextBox()
    Dim myNewRow As GridViewRow = New GridViewRow(0, -1, DataControlRowType.Header, DataControlRowState.Normal)
    cell.Controls.Add(tb)
    myNewRow.Cells.Add(cell)
    myTable.Rows.AddAt(0, myNewRow)
End Sub

Protected Function MakeTextBox() As HtmlGenericControl
    Dim div1 As HtmlGenericControl = New HtmlGenericControl("div")
    Dim span1 As HtmlGenericControl = New HtmlGenericControl("span")
    Dim tb As TextBox = New TextBox

    AddHandler tb.TextChanged, AddressOf TestGridView_Search
    tb.ID = "testSearchTextBox"
    tb.Attributes("placeholder") = "Search Term"
    div1.Controls.Add(span1)
    div1.Controls.Add(tb)

    Return div1
End Function

Protected Function MakeCell(Optional ByVal text As String = "", Optional ByVal span As Int32 = 1) As TableHeaderCell

    Dim header As New TableHeaderCell()
    header.ColumnSpan = span
    header.Text = text
    header.CssClass = "table-header"
    Return (header)
End Function

Protected Sub TestGridView_Search(ByVal sender As Object, ByVal e As EventArgs)        
        Dim i As Integer = 0        
End Sub

This creates all the correct controls, but the event does not fire. There are no errors but the break point on TestGridView_Search does not fire. Also, this is a user control (ASCX). I have tried doing this by attaching a javascript ajax function, but that does not seem to work in ASCX files.

2
  • Are you binding the GridView in an IsPostBack check? If so remove it. Otherwise the event will not be bound to the Control again on PostBack and will not trigger. Commented May 13, 2018 at 19:51
  • @VDWWD Thanks for the response. But, even when I remove it from an IsPostBack check, the event still does not fire. Commented May 13, 2018 at 20:23

2 Answers 2

0

ASP.NET is nothing else than your standard HTML webpage with a binding to the .NET framework. The server is handling your code. Whatever you code, needs to be written in js (inlc. jquery) and html. Well, when we code in ASP.NET using the visual studio, we do not see any of the js or html, except what we write in the .aspx or code behind file. That is, because the server processes what we wrote and adds the necessary js and html to the outputed stream to the client requesting that page. Knowing that, how do we achieve the idea of event handling or some things, which html and js can not do natively? By entering a state which was defined via js to postback to server. Example: When we click on the textbox text and change it, we need to make a postback to the server. The js and html which was written from the asp server does all of that for us. We do not have to worry about that. We just have to remember what it does. As default, textbox controls do have postback set to false. Because, whenever the user would do someting with it, the js of that page would do a postback. That is not a good user experience, when we do not want that. In your case, that is exactly what we want. So in our code behind file (because the control is generated there), at the control declaration, we can set the textbox attribute AutoPostBack to true. That should hopefully solve your problem.

Example:

textbox.AutoPostBack = True

Regards,

Maheshvara

Sign up to request clarification or add additional context in comments.

Comments

0

Viewstate isn't aware of your dynamic control, you'll have to reconstruct it:

Source: https://stackoverflow.com/a/6780138/1821637

I would try the asp:Repetaer control instead of the GridView:

https://www.aspsnippets.com/Articles/Repeater-control-Tutorial-with-example-in-ASPNet-using-C-and-VBNet.aspx

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.