如何找到嵌套的内部控制外部控制 [英] How to find outer control from nested inner control

查看:192
本文介绍了如何找到嵌套的内部控制外部控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下嵌套结构。我需要的是从CustomControl1 code背后的基础上在RadioButtonList选择的值范围内筛选DB Linq查询。

  X MainPage1
点¯x----面板1(模式弹出)
点¯x---------的UpdatePanel(upMailOrStatusAction,在面板1)
点¯x--------------单选按钮列表(rblActionLevel上的UpdatePanel)
点¯x-------------- SubForm1(上面板1)
点¯x------------------- CustomControl1(上Subform1)
点¯x------------------------ DropDownList的(上CustomControl1)

我想类似下面找到了控制,但它说:'upMailOrStatusAction'的名称在当前情况下不存在。

  RadioButtonList的RBL = upMailOrStatusAction.FindControl(rblActionLevel)作为单选按钮列表;

什么是找到RadioButtonList控件的最好方法?是的,我是相当新本!

感谢您,
吉姆萨瓦尼,GA

好吧,这里是弹出ASPX:

 < ASP:面板ID =pnlAddMailOrStatusAction=服务器的CssClass =modalPopupLarge风格=显示:无;>
            < ASP:的UpdatePanel ID =upMailOrStatusAction=服务器的UpdateMode =条件ChildrenAsTriggers =假RenderMode =块>
                <&触发器GT;
                    < ASP:AsyncPostBackTrigger控件ID =rblActionType/>
                < /触发器>
                <&的ContentTemplate GT;
                    < D​​IV CLASS =borderDiv>
                        <表类=borderTable0的cellpadding =0CELLSPACING =0WIDTH =100%>
                            <&THEAD GT;
                                < TR align =left>
                                    百分位合并单元格=9>动作详情< /第i
                                < / TR>
                            < / THEAD>
                            <&TBODY GT;
                                &所述; TR>
                                    &所述; TD列跨度=1>
                                        <标签类=labelField属性>行动水平:其中; /标签>
                                    < / TD>
                                    &所述; TD列跨度=1行跨度=1ALIGN =左>
                                        < ASP:RadioButtonList的ID =rblActionLevel=服务器的AutoPostBack =真RepeatDirection =水平
                                            RepeatLayout =流>
                                            < ASP:列表项选择=真值=基地文本=基地&安培; NBSP;&安培; NBSP; />
                                            < ASP:ListItem的值=覆盖文本=覆盖/>
                                        < / ASP:RadioButtonList的>
                                    < / TD>
                                    &所述; TD列跨度=1ALIGN =左>
                                        < ASP:标签ID =lblMSCoverage=服务器级=labelField属性文本=报道:/>
                                    < / TD>
                                    &所述; TD列跨度=1ALIGN =左>
                                        < ASP:标签ID =txtMSCoverage=服务器/>
                                    < / TD>
                                    < TD合并单元格=5>
                                        &安培; NBSP;
                                    < / TD>
                                < / TR>
                                &所述; TR>
                                    < TD合并单元格=9>
                                        <小时/>
                                    < / TD>
                                < / TR>
                                &所述; TR>
                                   < TD合并单元格=9>
                                    < ST:MailAddSubform =服务器ID =mailAddSubform/>
                                    < ST:StatusActionAddSubform =服务器ID =statusActionAddSubform可见=假/>
                                   < / TD>
                                < / TR>
                                &所述; TR>
                                    < TD合并单元格=9>
                                        <小时/>
                                    < / TD>
                                < / TR>
                            < / TBODY>
                        < /表>
                    < / DIV>
                < /&的ContentTemplate GT;
            < / ASP:的UpdatePanel>
         < / ASP:面板>

这是子窗体的aspx:

 <%@控制语言=C#AutoEventWireup =真codeBehind =MailAddSubform.ascx.cs
继承=Company.Solutions.Web.Controls.MailAddSubform%GT;

<%@注册标签preFIX =ST变量名=MailActionLookupSRC =〜/控制/ StMailActionLookup.ascx%>


    
        
            行动:
        
        
            
        
    
    
        
        
            信息:
        
        
            
        
    

这里是自定义控件的aspx:

 <%@控制语言=C#AutoEventWireup =真codeBehind =StMailActionLookup.ascx.cs继承=Company.Solutions.Web.Controls。 StMailActionLookup%GT;

    
      
        
            
            
            
        
        
             
        
      
        
        
            过滤器
            
            
            
                

最后,这里是背后自定义控件code。寻找计算器为我在哪里特林查找单选按钮列表:

 使用系统;
使用System.Collections.Generic;
System.Collections中使用;
使用System.Configuration;
使用System.Data这;
使用System.Linq的;
使用System.Linq.Ex pressions;
使用的System.Web;
使用System.Web.Security;
使用System.Web.UI程序;
使用System.Web.UI.HtmlControls;
使用System.Web.UI.WebControls;
使用System.Web.UI.WebControls.WebParts;
使用System.Xml.Linq的;
使用Company.Solutions.Data;
使用Company.Solutions.Data.Model;
使用Company.Solutions.Business.ViewModels;
使用Company.Solutions.Business.Helpers;
命名空间Comapny.Solutions.Web.Controls
{
    公共部分类StMailActionLookup:System.Web.UI.UserControl
    {
        保护无效的Page_Load(对象发件人,EventArgs的发送)
        {
           BindForm();
        }        保护无效BindForm()
        {
            IEnumerable的行动= GetClaimMailActions(用getFilter());
            ddlLookup.DataSource =行动;
            ddlLookup.DataTextField =codeAndDescription
            ddlLookup.DataValueField =actionCd;
            ddlLookup.DataBind();
        }        //保护无效ddlLookup_DataBound1(对象发件人,发送System.EventArgs)
        // {
        // ddlLookup.Items.Insert(0,新的ListItem(<请选择一个作用>,的String.Empty));
        //}        私人MailActionFilters用getFilter()
        {
            MailActionFilters过滤器=新MailActionFilters();
            如果(chkForms.Checked)
                    过滤器| = MailActionFilters.Forms;
            如果(chkRequested.Checked)
                    过滤器| = MailActionFilters.RequestedInfo;
            如果(chkOther.Checked)
                    过滤器| = MailActionFilters.Other;            返回过滤器;
        }        公共IEnumerable的GetClaimMailActions(MailActionFilters过滤器)
        {
            RelationalDataContext分贝= RelationalDataContext.Create();
            清单<防爆pression<&Func键LT; ClaimMailAction,布尔>>> predicates =新的List<防爆pression<&Func键LT; ClaimMailAction,布尔>>>();            常量字符串MAIL_ACTIONS =0;
            为const char表格='F';
            为const char REQUESTED_INFO ='R';
            为const char排除='X';            //计算器帮助            // RadioButtonList的RBL =(单选按钮列表)Control.Parent.FindControl(rblActionLevel);
            如果((chkForms.Checked&安培;&安培; chkRequested.Checked和放大器;&安培;!!chkOther.Checked)|(chkForms.Checked&安培;&安培; chkRequested.Checked和放大器;&安培; chkOther.Checked))
            {
                predicates.Add(CMA => cma.ActionCd.StartsWith(MAIL_ACTIONS)及及(cma.Esolutions code == NULL || cma.Esolutions code =排除)!);
            }其他{                如果((滤波器放大器; MailActionFilters.Forms)== MailActionFilters.Forms)
                    predicates.Add(CMA => cma.ActionCd.StartsWith(MAIL_ACTIONS)及和放大器; cma.Esolutions code ==表格);                如果((滤波器放大器; MailActionFilters.RequestedInfo)== MailActionFilters.RequestedInfo)
                    predicates.Add(CMA => cma.ActionCd.StartsWith(MAIL_ACTIONS)及和放大器; cma.Esolutions code == REQUESTED_INFO);                如果((滤波器放大器; MailActionFilters.Other)== MailActionFilters.Other)
                    predicates.Add(CMA => cma.ActionCd.StartsWith(MAIL_ACTIONS)及和放大器;!(cma.Esolutions code == NULL ||(cma.Esolutions code =排除和放大器;&放; cma.Esolutions code =形式放大器;!&安培; cma.Esolutions code = REQUESTED_INFO)))!;
            }            VAR predicate = predicateBuilder.Make< ClaimMailAction>();
            predicates.ForEach(委托​​(前pression<&Func键LT; ClaimMailAction,布尔>>表达式)
            {
                predicate = predicate.Or(表达式);
            });            VAR QRY = db.ClaimMailActions.Where(predicate)。选择(C =>新建{c.ActionCd,codeAndDescription =的String.Format({0} - {1},c.ActionCd, c.ActionDesc)});            返回qry.ToList();
        }
    }
}

新的code列表。我的同事使用该另一个查找。有人能告诉我我会怎么做这个查找类似的东西?即使低效率的,如果它的工作原理就这样吧。

 的HtmlForm形式;        的foreach(在Page.Controls变种CTL [0] .Controls)        {            如果(CTL是的HtmlForm)            {                形式= CTL作为的HtmlForm;                的ContentPlaceHolder支架= form.FindControl(DefaultContent)作为的Con​​tentPlaceHolder;                如果(架!= NULL)                {                   占位符paymentControlHolder = holder.FindControl(plcPaymentForm)作为占位符;                    如果(paymentControlHolder!= NULL)                    {                        IListener监听;                        的foreach(在paymentControlHolder.Controls变种C)                        {                            如果(c是IListener)                            {                                监听= C为IListener;                                rblPaymentType.SelectedIndexChanged + = listener.AddHandler();                            }                        }                    }                }            }        }

好吧,我想这一点,但还没有完全想通了,但如何确定单选按钮的选定值:

 的HtmlForm形式;
        的foreach(在Page.Controls变种CTL [0] .Controls){
            如果(CTL是的HtmlForm){
                形式= CTL作为的HtmlForm;
                的ContentPlaceHolder支架= form.FindControl(DefaultContent)作为的Con​​tentPlaceHolder;
                如果(架!= NULL){
                    单选按钮列表rblControlHolder = holder.FindControl(rblActionLevel)作为单选按钮列表;
                    如果(rblControlHolder!= NULL){                    }
                }
            }
        }


解决方案

我不知道是否我完全理解你想要达到的目标。但是,如果你想找到你的页面上的控制,你可能需要一个解决方案,我发布的这里

 公共静态控制FindControlRecursive(控制父,字符串控件ID)
 {
     如果(控件== parent.ID)
         回到父母;     的foreach(在parent.Controls控制CTRL)
     {
         控制TMP = FindControlRecursive(CTRL,控件ID);
         如果(TMP!= NULL)
             返回TMP;
     }     返回null;
 }

这是一个递归执行标准查找控制。但是,明智地选择你的父母。如果你有一个大的页面,你指示要搜索根目录,然后它会遍历所有页面的控制,直到最深嵌套控制。你也可以走相反的路,基本上是从你的控制开始递归上去,直到你到达页面的水平。会是另一种选择。

我发现这个递归找到的唯一的问题是有网页上的转发器时可能获得的问题。你不应该遍历中继的内部控制。由于其结构存在一些问题,这将失去它的ViewState并非如此。一旦我的时间我会后这个递归方法的更新。

编辑:

你得到的单选按钮,所选条目如下:

 单选按钮列表将myInstance = //找到我的单选按钮列表
串了selectedValue = myInstance.SelectedValue;

I have the following nested structure. What I need is to filter a DB Linq query from within CustomControl1 code behind based on the value of the RadioButtonList selection.

x MainPage1 
x---- Panel1 (modal popup)
x--------- UpdatePanel (upMailOrStatusAction, on Panel1)
x-------------- RadioButtonList (rblActionLevel, on UpdatePanel)
x-------------- SubForm1 (on Panel1)
x------------------- CustomControl1 (on Subform1)
x------------------------ DropDownList (on CustomControl1)

I am trying something like the following to find the control, but it says "The name 'upMailOrStatusAction' does not exist in the current context.

RadioButtonList rbl = upMailOrStatusAction.FindControl("rblActionLevel") as RadioButtonList;

What is the best way to find the RadioButtonList control? Yes, I am fairly new with this!

Thank you, Jim in Suwanee, GA

Ok, Here is the Popup aspx:

            <asp:Panel ID="pnlAddMailOrStatusAction" runat="server" CssClass="modalPopupLarge" Style="display: none;">
            <asp:UpdatePanel ID="upMailOrStatusAction" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="false" RenderMode="Block">
                <Triggers>
                    <asp:AsyncPostBackTrigger ControlID="rblActionType" />
                </Triggers>
                <ContentTemplate> 
                    <div class="borderDiv"> 
                        <table class="borderTable0" cellpadding="0" cellspacing="0" width="100%">
                            <thead>
                                <tr align="left">
                                    <th colspan="9">Action Detail</th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr>
                                    <td colspan="1">
                                        <label class="labelfield">Action Level:</label>
                                    </td>
                                    <td colspan="1" rowspan="1" align="left">
                                        <asp:RadioButtonList ID="rblActionLevel" runat="server" AutoPostBack="True" RepeatDirection="Horizontal"
                                            RepeatLayout="Flow">
                                            <asp:ListItem Selected="True" Value="Base" Text="Base&nbsp;&nbsp;" />
                                            <asp:ListItem Value="Coverage" Text="Coverage" />
                                        </asp:RadioButtonList>
                                    </td>
                                    <td colspan="1" align="left">
                                        <asp:Label ID="lblMSCoverage" runat="server" class="labelfield" Text="Coverage:" />
                                    </td>
                                    <td colspan="1" align="left">
                                        <asp:Label ID="txtMSCoverage" runat="server" />
                                    </td>
                                    <td colspan="5">
                                        &nbsp;
                                    </td>
                                </tr>
                                <tr>
                                    <td colspan="9">
                                        <hr />
                                    </td>
                                </tr>
                                <tr>
                                   <td colspan="9">
                                    <st:MailAddSubform runat="server" ID="mailAddSubform" />
                                    <st:StatusActionAddSubform runat="server" ID="statusActionAddSubform" Visible="false" />
                                   </td>
                                </tr>
                                <tr>
                                    <td colspan="9">
                                        <hr />
                                    </td>
                                </tr> 
                            </tbody>    
                        </table>
                    </div>
                </ContentTemplate>
            </asp:UpdatePanel>
         </asp:Panel>

And here is the subform aspx:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="MailAddSubform.ascx.cs" 
Inherits="Company.Solutions.Web.Controls.MailAddSubform" %>

<%@ Register TagPrefix="st" TagName="MailActionLookup" Src="~/Controls/StMailActionLookup.ascx" %>

Action: Message:

And here is the custom control aspx:

 <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="StMailActionLookup.ascx.cs" Inherits="Company.Solutions.Web.Controls.StMailActionLookup" %>

Filters

And finally, here is the code behind for the custom control. Look for StackOverflow for where I am tring to lookup the radio button list:

using System;
using System.Collections.Generic;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Company.Solutions.Data;
using Company.Solutions.Data.Model;
using Company.Solutions.Business.ViewModels;
using Company.Solutions.Business.Helpers;


namespace Comapny.Solutions.Web.Controls
{
    public partial class StMailActionLookup : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
           BindForm();
        }

        protected void BindForm()
        {
            IEnumerable actions = GetClaimMailActions(GetFilter());
            ddlLookup.DataSource = actions;
            ddlLookup.DataTextField = "CodeAndDescription";
            ddlLookup.DataValueField = "actionCd";
            ddlLookup.DataBind();
        }

        //protected void ddlLookup_DataBound1(object sender, System.EventArgs e)
        //{
        //    ddlLookup.Items.Insert(0, new ListItem("<Please Choose an Action>", String.Empty));
        //} 

        private MailActionFilters GetFilter()
        {
            MailActionFilters filters = new MailActionFilters();
            if (chkForms.Checked)
                    filters |= MailActionFilters.Forms;
            if (chkRequested.Checked)
                    filters |= MailActionFilters.RequestedInfo;
            if (chkOther.Checked)
                    filters |= MailActionFilters.Other;

            return filters;
        }

        public IEnumerable GetClaimMailActions(MailActionFilters filter)
        {
            RelationalDataContext db = RelationalDataContext.Create();
            List<Expression<Func<ClaimMailAction, bool>>> predicates = new List<Expression<Func<ClaimMailAction, bool>>>();

            const string MAIL_ACTIONS = "0";
            const char FORMS = 'F';
            const char REQUESTED_INFO = 'R';
            const char EXCLUDE = 'X';



            //StackOverflow help

            //RadioButtonList rbl = (RadioButtonList) Control.Parent.FindControl("rblActionLevel");




            if ((!chkForms.Checked && !chkRequested.Checked && !chkOther.Checked) | (chkForms.Checked && chkRequested.Checked && chkOther.Checked))
            {
                predicates.Add(cma => cma.ActionCd.StartsWith(MAIL_ACTIONS) && (cma.EsolutionsCode == null || cma.EsolutionsCode!= EXCLUDE));
            } else {

                if((filter & MailActionFilters.Forms) == MailActionFilters.Forms)
                    predicates.Add(cma => cma.ActionCd.StartsWith(MAIL_ACTIONS) && cma.EsolutionsCode == FORMS);

                if((filter & MailActionFilters.RequestedInfo) == MailActionFilters.RequestedInfo)
                    predicates.Add(cma => cma.ActionCd.StartsWith(MAIL_ACTIONS) && cma.EsolutionsCode == REQUESTED_INFO);

                if((filter & MailActionFilters.Other) == MailActionFilters.Other)
                    predicates.Add(cma => cma.ActionCd.StartsWith(MAIL_ACTIONS) && (cma.EsolutionsCode == null || (cma.EsolutionsCode != EXCLUDE && cma.EsolutionsCode != FORMS && cma.EsolutionsCode != REQUESTED_INFO)));
            }

            var predicate = PredicateBuilder.Make<ClaimMailAction>();
            predicates.ForEach(delegate(Expression<Func<ClaimMailAction, bool>> expr)
            {
                predicate = predicate.Or(expr);
            });

            var qry = db.ClaimMailActions.Where(predicate).Select(c => new { c.ActionCd, CodeAndDescription = string.Format("{0} - {1}", c.ActionCd, c.ActionDesc) });

            return qry.ToList();
        }
    }
}

New code list. My co-worker used this for another lookup. Could someone show me how I would do something similar for this lookup? Even if inefficient, if it works so be it.

HtmlForm form;



        foreach(var ctl in Page.Controls[0].Controls)

        {

            if(ctl is HtmlForm)

            {

                form = ctl as HtmlForm;

                ContentPlaceHolder holder = form.FindControl("DefaultContent") as ContentPlaceHolder;

                if (holder != null)

                {

                   PlaceHolder paymentControlHolder = holder.FindControl("plcPaymentForm") as PlaceHolder;

                    if (paymentControlHolder != null)

                    {

                        IListener listener;

                        foreach (var c in paymentControlHolder.Controls)

                        {

                            if (c is IListener)

                            {

                                listener = c as IListener;

                                rblPaymentType.SelectedIndexChanged += listener.AddHandler();

                            }

                        }

                    }

                }





            }

        }

Ok, I am trying this, but have not quite figured out yet how to determine the selected value of the radio button:

HtmlForm form;
        foreach (var ctl in Page.Controls[0].Controls) { 
            if (ctl is HtmlForm) { 
                form = ctl as HtmlForm; 
                ContentPlaceHolder holder = form.FindControl("DefaultContent") as ContentPlaceHolder; 
                if (holder != null) {
                    RadioButtonList rblControlHolder = holder.FindControl("rblActionLevel") as RadioButtonList; 
                    if (rblControlHolder != null) { 

                    }
                }
            }
        }

解决方案

I'm not sure whether I fully understood what you're trying to achieve. But if you want to find a control on your page, you might want a solution as I posted here.

 public static Control FindControlRecursive(Control parent, string controlId)
 {
     if (controlId == parent.ID)
         return parent;

     foreach (Control ctrl in parent.Controls)
     {
         Control tmp = FindControlRecursive(ctrl, controlId);
         if (tmp != null)
             return tmp;
     }

     return null;
 }

It is a recursive implementation of the standard find control. But choose your parent wisely. If you have a large page and you indicate that to be the search root, then it will traverse all of the controls of the page till the deepest nested control. You could also go the reversed way, basically starting from your control and recursively go up till you reach the page level. Would be another option.

The only issue I found with this recursive find is that you might get problems when having a repeater on your page. You shouldn't traverse the repeater's inner controls. Due to its architecture there are some problems that it will loose it's viewstate otherwise. Once I've time I'll post an update of this recursive method.

Edit:
You get the selected entry of the radio button as follows:

RadioButtonList myInstance = //find my radio button list
string selectedValue = myInstance.SelectedValue;

这篇关于如何找到嵌套的内部控制外部控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆