FindControl和FindControlRecursive在Formview中为Label返回null

时间:2014-05-01 03:51:33

标签: c# asp.net

新手试图使用FindControl和/或FindControlRecursive来获取FormView中的标签值,但是,不断获取NULL值。执行FormView后正在执行按钮单击(可以在执行的网页上看到FormView中的值,因此看起来数据已被绑定?)。无论使用FindControl还是FindControlRecursive,都无法找到Label控件的值。

这是.aspx页面(删节):

 <asp:FormView ID="frmOpportunity" runat="server" BackColor="#EAEAEA" BorderColor="Black"     BorderStyle="Solid" BorderWidth="1px" DataKeyNames="OpportunityID" DataSourceID="sds_OpportunitiesFiltered" EnableModelValidation="True" Style="text-align: left" Width="100%" OnPageIndexChanging="frmOpportunity_PageIndexChanging" OnDataBound="frmOpportunity_PreRender">

                        <ItemTemplate>

                            <table style="border: solid; color: orange;">
                               <tr>
                                  <td rowspan="22" valign="top">
                                        <strong><span class="auto-style11">Subject:</span></strong><br />
                                        <asp:Label ID="Label2" runat="server" BackColor="White" Font-Bold="False" Height="30px" Text='<%# Eval("Subject") %>' Width="800px"></asp:Label>
                                        <br />
                                        <br />
                                        <strong>Internal:</strong><br />
                                        <asp:Label ID="OpportunityLabel" runat="server" BackColor="White" Text='<%# Bind("Opportunity") %>' Width="800px" />
                                        <br />
                                        <br />
                                        <strong>Artists:</strong><br />
                                        <asp:Label ID="Specification" runat="server" BackColor="White" Text='<%# Bind("Specification") %>' Width="800px" />
                                        <br />
                                        <br />


                                    </td>
                                </tr> 
                          </table>
                      </ItemTemplate>
               </asp:FormView>

这是后面的代码(选项1 - FindControlRecursive):

 public static Control FindControlRecursive(Control rootControl, string controlID)
     {
         if (rootControl.ID == controlID) return rootControl;

         foreach (Control controlToSearch in rootControl.Controls)
         {
             Control controlToReturn =
                 FindControlRecursive(controlToSearch, controlID);
             if (controlToReturn != null) return controlToReturn;
         }
         return null;
     }

  protected void btnSend_Click(object sender, EventArgs e)
     {
            Label eSubject = (Label)FindControlRecursive(frmOpportunity,"Label2");
            mail.Subject = eSubject.Text; 
     }

在此版本中,eSubject返回NULL。

这是后面的代码(选项2 - FindControl):

 protected void btnSend_Click(object sender, EventArgs e)
          {
                 Label eSubject = (Label)frmOpportunity.FindControl("Label2");
                 mail.Subject = eSubject.Text; 
          }

在此版本中,eSubject返回NULL。

还尝试确保FormView处于正确模式并创建以下在PreRender和OnDataBound上的FormView中运行的以下情况,并且在两种情况下都创建了异常错误,因为FindControl返回NULL。

 protected void frmOpportunity_PreRender(object sender, EventArgs e)
     {
         if (frmOpportunity.CurrentMode == FormViewMode.ReadOnly)
         {
             Label ProductNameTextBox = frmOpportunity.FindControl("OpportunityLabel") as Label;
             string test = ProductNameTextBox.Text;

         }
     }

0 个答案:

没有答案