开发者

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

开发者 https://www.devze.com 2023-01-31 17:51 出处:网络
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 cod

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 kin开发者_JAVA技巧d of event to trigger it but I don't know how you can put an event handler inside of an event handler.


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.


In VB you need use Directcast

Dim chk As Checkbox = DirectCast(Me.FormView1.FindControl("checkgen"), Checkbox)


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号