Results 1 to 11 of 11

Thread: Manually build a button event from inside a gridview control

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2010
    Posts
    4

    Manually build a button event from inside a gridview control

    Hello!

    I'm a new developer here, working in VB. Have a question that is stumping me as well as some others.

    I have a gridview control built. I am not using an <sqldatasource> tag, so I can't use it's autoGenerateSelect/Update/Delete functionality. I have built a template field that has an "edit" link button in it, and by clicking it will fire the grdName_SelectedIndexChanged event of the gridview.

    Now, it's in this event where I'm having trouble. I have it coded so that when this event fires, it will build a textbox and button control. Code:

    Protected Sub grdResults2_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles grdResults2.SelectedIndexChanged

    Dim row As GridViewRow = grdResults2.SelectedRow
    Dim cell As TableCell = row.Cells(6)
    Dim txtSeq As New TextBox
    Dim btnUpdate As New Button

    'build the textbox in the Search_Seq cell
    txtSeq.ID = "txtSeq"
    txtSeq.Width = Unit.Pixel(20)
    txtSeq.Text = row.Cells(6).Text
    cell.Controls.Add(txtSeq)

    'build the "update" button in the Search_Seq cell
    btnUpdate.ID = "btnUpdate"
    btnUpdate.Text = "Update"
    cell.Controls.Add(btnUpdate)




    End Sub


    So the textbox and button populate perfectly. I can run it in the browser and it works fine.

    How do I go about setting up the event for this button now? Because it's being manually built here in the gridview event, there is no button for me to "double_click" on in the design view to auto-build this event. There are some properties that need to be set on this button to link it up to a new event....and that's where I'm lost. Any help would be greatly appreciated!

    Hopefully I don't leave any additional information out. If I did, please let me know! I've attached a word document that shows a couple pictures of what it is I'm doing and need.

  2. #2
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    Re: Manually build a button event from inside a gridview control

    For the button you can do this:
    Code:
    AddHandler btnUpdate.Click, AddressOf gobutton
            Me.Controls.Add(btnUpdate)
    Then create the sub that will handle the buttonclick.

    Code:
       Private Sub gobutton( _
      ByVal sender As Object, _
      ByVal e As System.EventArgs)
    ''''''''do something...
        End Sub
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2010
    Posts
    4

    Re: Manually build a button event from inside a gridview control

    So, I now have this, but it's not working. I'm missing something here, and right now it feels like half my brain. Need some coffee or something....

    Protected Sub grdResults2_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles grdResults2.SelectedIndexChanged

    'build the "update" button in the 6th cell of the gridview control

    Dim btnUpdate As New Button
    btnUpdate.Text = "Update"
    btnUpdate.ID = "ButtonID"
    AddHandler btnUpdate.Click, AddressOf btnUpdate_Click
    cell.Controls.Add(btnUpdate)

    End Sub

    Protected Sub btnExport_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExport.Click

    'This is just another button I have in the app that has it's own functionality,
    'but using it here to change its text to verify the update button is working.

    btnExport.Text = "Update works!"

    End Sub

    However, what I think is happening is that as soon as you click the button, the page is executing its postback and it winds up not seeing that the button was clicked to trigger the event. Thoughts? Suggestions?

  4. #4
    Hyperactive Member
    Join Date
    Dec 2007
    Location
    Somewhere else today
    Posts
    355

    Re: Manually build a button event from inside a gridview control

    Quote Originally Posted by dsoukup View Post
    cell.Controls.Add(btnUpdate)
    That should read:

    Code:
     Me.Controls.Add(btnUpdate)
    Computerman
    It was much easier in VB6, but I am now liking Vb.Net alot more.

  5. #5
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    Re: Manually build a button event from inside a gridview control

    Yeah.
    If you want to add a button to a cell you must create it first.Anyhow i don't have VS handy right now so i can be sure but i guess...Ah,please hold on a minute.
    You said executing postback.Are you using asp.net or winforms .net ?
    Am not the least sure that this can work in asp.net.You should ask the question in asp.net forum if asp.net is what you are using because postback may
    1)make it not to work
    2)make duplicates
    3)remove the added button.
    Please check with asp.net forum.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  6. #6

    Thread Starter
    New Member
    Join Date
    Sep 2010
    Posts
    4

    Re: Manually build a button event from inside a gridview control

    Using ASP.NET.

    I was totally unaware this forums was for using Windows Forums :P

    I'll try and use the me.controls syntax.

  7. #7
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    Re: Manually build a button event from inside a gridview control

    No it's for asp.net also but there is a specific subforum for it :
    http://www.vbforums.com/forumdisplay.php?f=31
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  8. #8
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Manually build a button event from inside a gridview control

    Hey,

    Since you are doing this in ASP.Net, things start to get a little bit more tricky, as the time and place that the controls are created and initialized start to come into play.

    I would suggest that you have a look here:

    http://www.4guysfromrolla.com/articles/092904-1.aspx

    To familiarize yourself with the concepts.

    Gary

  9. #9

    Thread Starter
    New Member
    Join Date
    Sep 2010
    Posts
    4

    Re: Manually build a button event from inside a gridview control

    Alright, so read through that article. Wow there were a few things I didn't know about the page life cycle!

    So, here's what I'm dealing with. I essentially need to have a button being built in the Page_Init step of the page life cycle. That way, when I click the dynamic button control located in a gridview cell, the postback it causes doesn't cause it to disappear. I think I'm on the right track here?

    So, I have this, but it's not quite working. Multiple snippets from the events I'm utilizing to get this to work are pasted.

    'Page Initialization
    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
    Dim btnUpdate As New Button
    Me.Controls.Add(btnUpdate)
    AddHandler btnUpdate.Click, AddressOf btnUpdate_Click
    End Sub

    My gridview event I have, that will add a button into the specified cell.
    Public Sub grdResults2_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles grdResults2.SelectedIndexChanged
    Dim row As GridViewRow = grdResults2.SelectedRow
    Dim cell As TableCell = row.Cells(6)
    Dim txtSeq As New TextBox


    'build the textbox in this selected row
    txtSeq.ID = "txtSeq"
    txtSeq.Width = Unit.Pixel(20)
    txtSeq.Text = row.Cells(6).Text
    cell.Controls.Add(txtSeq)

    'build the "update" button in the 7th cell of the gridview control, for this row
    Dim btnUpdate As New Button
    btnUpdate.ID = "btnUpdate"
    btnUpdate.Text = "Update"
    cell.Controls.Add(btnUpdate)

    End Sub

    'The event that I'm trying to get to fire

    Public Sub btnUpdate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpdate.Click

    'This is just another non-dynamic button I have in my webform. Just testing to see if I can get it's text to change, which will tell me this event is firing.

    btnExport.Text = "nice!"
    End Sub

    I think I may have this set up wrong. I'm having a reported error the the btnUpdate_click event, and it states:

    Handles clause requires a WithEvents variable defined in the containing type or one of its base types.

    Not sure what this is...will have to go investigate.

    Anyone have any suggestions, or know what I'm doing wrong here?

    Thanks!

  10. #10
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    Re: Manually build a button event from inside a gridview control

    Of course.
    Remove Handles btnUpdate.Click
    Or if you have multiple names of btnUpdate change the name of the non-dynamic button
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  11. #11
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Manually build a button event from inside a gridview control

    dsoukup,

    When you are posting code into the forum, can you remember to surround it in [CODE][/CODE] tags, it makes it so much easier to read.

    Thanks

    Gary

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width