Question

I have an AJAX Accordion control defined in the page and within each accordion branch I have a label that gets its value from SQL database

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

The labels are showing the correct value. My question is how to get the value of the label in the code behind using FindControl. Right now, the following finds the Accordion correctly.

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

But when I am trying to get the value of the label using the following, I only get the value as if the first accordion was selected, even if a different accordion branch is selected. I know I have to somehow use selected index somewhere, but I don't know where and how. Any help will be appreciated?

Dim IDinCodeBehind As Label
IDinCodeBehind = CType(acc.FindControl("lbl"), Label)
Was it helpful?

Solution

I did a small demo to get the selectedPane. Within this pane access Controls collection and find the according label

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; }
}

And here is my aspx code

<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" />
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top