How to terminate a recursive function and return a value

时间:2015-05-12 22:29:57

标签: c# asp.net

I'm trying to find a TextBox control inside a ASP.NET page using a recursive function. When this control is found, i would like to terminate the function and return it.

My main problem is that i'm unable to stop the recursive function and return the control.

Here's my code:

global

2 个答案:

答案 0 :(得分:8)

You're missing one more check, right here:

    <%= form_for :new_participants, url: add_participant_path( :p => @project.id), html: { :multipart => true, :class=> "form-horizontal", id: "basicForm" } do |f| %> 

    <% if @new_participants.errors.any? %>

   <h2>OOPS!</h2>
      <ul>

      <%  @new_participants.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul></div>
<% end %>

 <div class="form-group ">
 <label class="form-label dk-aqua"> Email: <span class="asterisk">*</span></label>
 <%=    f.text_field :email, :autofocus => true, :required => true, :maxlength => 55, :placeholder => 'Email(s)', :class => 'form-control'  %>
 </div>

<%= f.submit 'INVITE',  :class => 'btn btn-aqua btn-lg btn-block', 
           :style => 'margin-bottom:-5px' %>  
<% end %>

答案 1 :(得分:1)

private void button1_Click(object sender, EventArgs e)
{
    Control ctrl = GetControlByName(this, "archHalfRoundWindowGroup");
}

public Control GetControlByName(Control Ctrl, string Name)
{
    Control ctrl = new Control();
    foreach (Control x in Ctrl.Controls)
    {
        if (x.Name == Name)
            return ctrl=x;
        if (x.Controls.Count > 0)
        {
            ctrl= GetControlByName(x, Name);
            if (ctrl.Name != "")
                return ctrl;
        }
        if (ctrl.Name != "")
            return ctrl;
        }
        return ctrl;
    }