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

↘锁芯ラ 提交于 2019-11-30 07:14:18

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.

Julio

In VB you need use Directcast

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!