在Formview中访问控件

在Formview中访问控件

本文介绍了使用FindControl:在Formview中访问控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个简单的婚礼清单"应用程序,客人可以在其中保留要为新娘和新郎购买的礼物.保留"页面在几个面板中包装了一些字段,所有字段都包装在FormView中.

I'm developing a simple Wedding List application, where guests can reserve the present they want to buy for the bride and groom. The Reserve page wraps a few fields inside a couple of panels, all wrapped inside a FormView.

用户输入他们的姓名,电子邮件和要保留的项目数量,页面将在数据库中进行必要的保留.

The user enters their name, email and the quantity of items that they want to reserve, and the page will make the necessary reservations in the DB.

我的第一个问题是在FormView_ItemCommand中,我无法在FormView中引用任何其他控件....不需要ListViews或DetailViews?

My first problem was that in FormView_ItemCommand, I couldn't reference any of the other controls in the FormView.... I figured this was a case for FindControl - but why do I need to for a Formview when I've never needed it for ListViews or DetailViews?

第二,我知道以下代码有效..

Secondly, I know the following code works..

Dim oCtrl as TextBox = Me.fvwReservation.FindControl("txtEmail")
Dim Test As String = oCtrl.Text

...但是为什么我不能使用...

...but why can't I use...

Dim Test As String = Me.fvwReservation.FindControl("txtEmail").Text

??

最后,我认为这不是我需要的,但是我一直在研究递归的FindControl变体,但实际上并没有找到一个真正可以编译的变体!有什么建议吗?

Finally, I don't think I need it on this occasion, but I've been researching recursive FindControl variants, but I haven't actually found one that actually compiles! Any suggestions?

一个帖子有很多-预先感谢.

It's a lot for one post - thanks in advance.

免费代码段:

<asp:FormView ID="fvwReservation" runat="Server" DataSourceID="dsGift">
     <ItemTemplate>
      <asp:Panel runat="server" ID="pnlDetails">
       <h3>Reserve Item: <%#Eval("ShortDesc")%></h3>
       <p>You have chosen to reserve the <em><%#Eval("LongDesc")%></em> gift.</p>
       <p>Please enter your details below to confirm the reservation.</p>
      </asp:Panel>
      <asp:Panel runat="server" ID="pnlConfirm">
       <div class="row">
        <asp:Label runat="server" CssClass="label">Name:</asp:Label><asp:TextBox ID="txtName" MaxLength="50" runat="server" CssClass="Field" />
        <asp:RequiredFieldValidator ID="rfvName" runat="server" ErrorMessage="You must specify your Name" ControlToValidate="txtName" />
       </div>
       <div class="row">
        <asp:Label runat="server" CssClass="label">Email:</asp:Label><asp:TextBox ID="txtEmail" MaxLength="100" runat="server" CssClass="Field"/>
        <asp:RequiredFieldValidator ID="rfvEmail" runat="server" ErrorMessage="You must specify your Email Address" ControlToValidate="txtEmail" />
        <asp:RegularExpressionValidator ID="regexEmail" ValidationExpression="^(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6}$" runat="server" ErrorMessage="Please enter a valid Email Address" ControlToValidate="txtEmail" />
       </div>
       <div class="row">
        <asp:Label runat="server" CssClass="label">Quantity (max <%#Eval("QtyRemaining")%>):</asp:Label><asp:TextBox ID="iQty" MaxLength="2" runat="server" CssClass="Field" />
        <asp:RangeValidator ID="rvQty" runat="server" ErrorMessage="The Quantity mmust be between 1 and 10" MinimumValue="1" MaximumValue="10" ControlToValidate="iQty" />
       </div>
       <div class="row">
        <asp:Label runat="server" CssClass="label">&nbsp;</asp:Label>
        <asp:Button ID="btnReserve" Text="Confirm Reservation" CommandName="Reserve" runat="server" />
       </div>
      </asp:Panel>
     </ItemTemplate>
    </asp:FormView>

推荐答案

对于第二个问题,FindControl返回一个通用Control,并且必须将其强制转换为特定类型的控件,才能访问该特定类型的属性.控制.

For your second question, FindControl returns a generic Control, and must be cast to the specific type of control in order to obtain access to the properties of that specific type of control.

您可以在一个衬套中完成此操作,如下所示:

You can do it in a one liner, like this:

Dim Test As String = CType(Me.fvwReservation.FindControl("txtEmail"), TextBox).Text

关于您的第一个问题,我也想知道答案.

Regarding your first question, I would love to know the answer to that as well.

编辑

浏览了其他一些StackOverflow响应(特别是和这一个).因为FormView模板中的控件直到该模板为活动模板才存在,所以您不能在后面的代码中直接引用它们.因此,您必须在适当的事件中使用FindControl来访问控件.

Looked through a few other StackOverflow responses (specifically this one and this one). As the controls in the FormView template do not exist until the template is the active template, you cannot directly refer to them in the code behind. Thus, you must use FindControl during an appropriate event to access the controls.

这篇关于使用FindControl:在Formview中访问控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 01:19