0

I have a button within a formview control on my page.

Because the button is contained within the formview, my code-behind can't see it.

So I did this:

Dim btnSave As Button = CType(fvCourse.FindControl("btnSave"), Button)

And then I added an event handler like this:

AddHandler btnSave.Click, AddressOf btnSave_Click

Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    Response.write("hey!")
End Sub

The problem is, I don't think it's working because I never see the "hey!" on my page.

Am I missing something?

Thanks

3
  • 1
    Test it with a Msgbox or with a response.redirect()? This way u can be 100% sure. Commented Nov 8, 2012 at 16:29
  • 1
    Where are you adding the event handler? Commented Nov 8, 2012 at 16:30
  • I am doing this all in page load - thanks Commented Nov 8, 2012 at 16:41

2 Answers 2

2

I don't know about missing something, but I reckon you could do it a simpler way since you're using VB. Give your button a command name and command argument first:

<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
CommandArgument="1" CommandName="yes" />

These can be anything - typically you use the command name to determine which button a user clicked on, and the command argument to show the record id.

In your code-behind, attach a macro to the ItemCommand event of the FormView (which fires when something happens within it):

 Protected Sub FormView1_ItemCommand(sender As Object, e As System.Web.UI.WebControls.FormViewCommandEventArgs) Handles FormView1.ItemCommand

    Select Case e.CommandName.ToLower
        Case "yes"

            'test
            Label2.Text = "You chose " & e.CommandArgument.ToString

    End Select
End Sub Protected Sub FormView1_ItemCommand(sender As Object, e As System.Web.UI.WebControls.FormViewCommandEventArgs) Handles FormView1.ItemCommand

    Select Case e.CommandName.ToLower
        Case "yes"

            'test
            Label2.Text = "You chose " & e.CommandArgument.ToString

    End Select
End Sub

And in VB, that's all you need to do!

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

Comments

1

You should use the ItemCreated event of the FormView for such things. If the Button is in the ItemTemplate you need to check for the FormViewMode.ReadOnly, for EditItemTemplate you need to use Edit:

Private Sub fvCourse_ItemCreated(ByVal sender As Object, ByVal e As System.EventArgs) Handles fvCourse.ItemCreated
    Select Case fvCourse.CurrentMode
        Case FormViewMode.Edit
            Dim btnSave As Button = DirectCast(fvCourse.FindControl("btnSave"), Button)
            AddHandler btnSave.Click, AddressOf btnSave_Click
    End Select
End Sub

4 Comments

The button is in ItemTemplate. Why should it be in the _ItemCreated event? - thanks
@999cm999: Because event handlers must be added on every postback and this event is triggered on every postback. It also ensures that it's called only in the correct FormViewMode and you don't get a NullReferenceException.
should I put all my controls in _ItemCreated? Like all my asp:Labels, buttons, textboxes, etc? - thanks
I assume that you're mixing up something. If you handle ItemCreated and add the event-handler there, you're not creating your control(s) there. I've assumed that it's already added declaratively on aspx. So it's only a way to reference controls that are declared in a template. Hence i don't understand what you mean with "put all controls there".

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.