ASP.NET, VB: how to access controls inside a FormView from the code behind?
Asked Answered
F

3

8

I have a checkbox and a panel inside of a FormView control, and I need to access them from the code behind in order to use the checkbox to determine whether or not the panel is visible. This is the code that I originally used, but since I put the controls inside of the FormView, it no longer works.

Protected Sub checkGenEd_CheckedChanged(ByVal sender As Object, _
                                         ByVal e As System.EventArgs)
    If checkGenEd.Checked = True Then
        panelOutcome.Visible = True
    Else
        panelOutcome.Visible = False
    End If
End Sub 

I've started to figure this out based on other questions I looked up on here, but all of them were in C# instead of VB, so this is as far as I got:

Protected Sub FormView1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles FormView1.DataBound
    If FormView1.CurrentMode = FormViewMode.Edit Then

    End If
End Sub

So yeah I'm not sure exactly how to finish it. I'm sorry, this might be pretty basic, but I'm new at this and any help would be appreciated!

EDIT: here's my code now:

Protected Sub FormView1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles FormView1.DataBound
    If FormView1.CurrentMode = FormViewMode.Edit Then

        CheckBox checkGenEd = formview1.FindControl("checkGenEd");
        Panel panelOutcome = formview1.FindControl("panelOutcome");

    End If
End Sub

It's also saying that checkGenEd and panelOutcome are not declared.

EDIT: I changed my code to this but it still doesn't work:

Protected Sub FormView1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles FormView1.DataBound
    If FormView1.CurrentMode = FormViewMode.Edit Then

        Dim checkGenEd As CheckBox = FormView1.FindControl("checkGenEd")
        Dim panelOutcome As Panel = FormView1.FindControl("panelOutcome")

        If checkGenEd.Checked = True Then
            panelOutcome.Visible = True
        Else
            panelOutcome.Visible = False
        End If

    End If
End Sub

There aren't any errors anymore, but nothing happens when I click the checkbox. I think there needs to be some kind of event to trigger it but I don't know how you can put an event handler inside of an event handler.

Flick answered 18/12, 2010 at 1:49 Comment(0)
S
10

With FormView, you have to use find control, as in:

CheckBox checkGenEd = (CheckBox)formview1.FindControl("checkGenEd");
Panel panelOutcome = (Panel)formview1.FindControl("panelOutcome");

You cannot reference a control directly by ID.

HTH.

Sustentacular answered 18/12, 2010 at 1:55 Comment(3)
Okay that makes sense, but now it's telling me that 'CheckBox' is a type and cannot be used as an expression.Flick
I think that's typically a syntax error, can you post what you have? You could also try the alternate syntax above...Sustentacular
Dim checkGenEd As CheckBox = CType(FormView1.FindControl("checkGenEd"), CheckBox) for the vb equivalent casting.Derive
P
3

In VB you need use Directcast

Dim chk As Checkbox = DirectCast(Me.FormView1.FindControl("checkgen"), Checkbox)
Penthea answered 20/8, 2011 at 0:28 Comment(1)
You posted same thing as question #7129264Ashcan
L
2

FormView has its own event framework. A normal control within a FormView won't generate the postback events you are looking for. I initially made the same mistake. I wanted, like you, to generate some kind of postback that could be intercepted at the server end. Once we get back to the server we can look at the values in checkboxes etc depending on whatever business rules apply. This is what I did.

First of all put all relevant controls within an

<EditItemTemplate> 

section within the FormView. (There are other Template tags that may be more appropriate). To generate the postback have a button (for example) like the one below. (This has to be within the EditItemTemplate section as well):

<asp:linkbutton id="UpdateButton"
    text="Update"
    commandname="Update"
    runat="server"/>

You can intercept this at the server with the FormView event ItemCommand. For example:

Private Sub FormView1_ItemCommand(sender As Object, e As System.Web.UI.WebControls.FormViewCommandEventArgs) Handles FormView1.ItemCommand
    'your code here
End Sub

Once you are back at the server you can then start looking at the various controls to see what they hold, using findControl if necessary. The button command shown above is an example so you might want to use another control.

Landan answered 6/10, 2012 at 11:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.