【问题标题】:Finding a label using FindControl within an AJAX Accordion Control在 AJAX 手风琴控件中使用 FindControl 查找标签
【发布时间】:2012-08-27 17:58:45
【问题描述】:

我在页面中定义了一个 AJAX Accordion 控件,在每个手风琴分支中我都有一个从 SQL 数据库获取其值的标签

<cc1:Accordion DataSourceID="sqlDSSomeGroup" ID="acrd" runat="server"  
  <ContentTemplate>
     <asp:Label ID="lbl" runat="server" Text='<%#Eval("SomeGroupID") %>' />
  </ContentTemplate>
</cc1:Accordion>

标签显示的值正确。我的问题是如何使用 FindControl 在后面的代码中获取标签的值。现在,以下内容正确地找到了 Accordion。

Dim acc As AjaxControlToolkit.Accordion = CType(placeHolder.FindControl("acrd"), AjaxControlToolkit.Accordion)

但是当我尝试使用以下方法获取标签的值时,即使选择了不同的手风琴分支,我也只能像选择第一个手风琴一样获得该值。我知道我必须以某种方式在某处使用选定的索引,但我不知道在哪里以及如何使用。任何帮助将不胜感激?

Dim IDinCodeBehind As Label
IDinCodeBehind = CType(acc.FindControl("lbl"), Label)

【问题讨论】:

    标签: asp.net ajax vb.net accordion findcontrol


    【解决方案1】:

    我做了一个小演示来获取 selectedPane。在此窗格中访问控件集合并找到相应的标签

    public partial class demo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                // init data, demo purposes
                List<Person> liste = new List<Person>();
                liste.Add(new Person() { ID = 0, Name = "jon0" });
                liste.Add(new Person() { ID = 1, Name = "jon1" });
                liste.Add(new Person() { ID = 2, Name = "jon2" });
                liste.Add(new Person() { ID = 3, Name = "jon3" });
                liste.Add(new Person() { ID = 4, Name = "jon4" });
                Accordion1.DataSource = liste;
                Accordion1.DataBind();
            }
        }
        protected void btnGetName_Click(object sender, EventArgs e)
        {
            // get current pane by using Accordion1.SelectedIndex
            Label lblName = Accordion1.Panes[Accordion1.SelectedIndex].FindControl("lblName") as Label;
            Debug.WriteLine("Label: " + lblName.Text);
        }
    }
    public class Person
    {
        public string Name { get; set; }
        public int ID { get; set; }
    }
    

    这是我的 aspx 代码

    <asp:Accordion ID="Accordion1" runat="server" SelectedIndex="0">
        <HeaderTemplate>
             <asp:Label ID="lblID" runat="server" Text='<%#"Pane" + Eval("ID") %>' />
             <hr />
        </HeaderTemplate>
        <ContentTemplate>
         <asp:Label ID="lblName" runat="server" Text='<%#Eval("Name") %>' />
         <br />
         <br />
        </ContentTemplate>
    </asp:Accordion>
    <asp:Button ID="btnGetName" runat="server" Text="GetName" onclick="btnGetName_Click" />
    

    【讨论】:

    • 非常感谢 Pilgerstorfer Franz!我在寻找 Label lblName = Accordion1.Panes[Accordion1.SelectedIndex].FindControl("lblName") as Label;但是你也提供了所有其他的东西,我非常感激。很好的答案!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-10
    • 1970-01-01
    相关资源
    最近更新 更多