Dave
Dave

Reputation: 3208

Accessing dynamically created elements from inside of a repeater

I need to create a series of dynamically created buttons inside of a repeater. These buttons names are coming from a database. When a user clicks a button, some hidden text beside the name appears. And disappears when clicked again. The text will have to be either defined in the html or the code behind.

Here is some code to demonstrate what I'm thinking.

 <asp:Repeater ID="Repeater1" DataSourceID="DecisionDetailsDS" runat="server" >
        <ItemTemplate>
            <asp:Button ID="DecisionButton" runat="server" OnClick="BTN_ShowText" BorderStyle="None"
                Text='<%# Eval("Decision_Type_Dsc") %>' />
            <asp:Label ID="DecisionLabel" Visible="false" runat="server" />
        <p></p>
        </ItemTemplate>
        <AlternatingItemTemplate>
            <asp:Button ID="DecisionButton" runat="server" OnClick="BTN_ShowText" BorderStyle="None"
                Text='<%# Eval("Decision_Type_Dsc") %>' />
             <asp:Label ID="DecisionLabel" Visible="false" runat="server" />                          <p></p>
        </AlternatingItemTemplate>
</asp:Repeater>

public void BTN_ShowText(Object sender, EventArgs e)
{

    Label TestLabel = (Label)FindControl("DecisionLabel");
    Button TestButton = (Button)FindControl("DecisionButton");

    switch(TestButton.Text)
    {
    case "Dismissed":
        TestLabel.Text = "Testing 1 2 3";
        break;

    case "Anything":
        TestLabel.Text = "Testing 2 3 4";
        break;
    }

    if (TestLabel.Visible == false)
    {
        TestLabel.Visible = true;
    }
    else
    {
        TestLabel.Visible = false;
    }

    }
}

I know this doesn't work. I get nulls returned. Whereas I need the id of the button and it's corresponding label. Hopefully this gives you an idea of what I'm trying to do. Any suggestions are appreciated especially if you provide my some code to work with. I thought maybe using jquery?

I am new to .net/jquery programming. Hopefully someone can help me. Thanks!

Upvotes: 1

Views: 390

Answers (2)

womp
womp

Reputation: 116977

FindControl is a method that can be executed on any control, or the Page object. However, it's not a recursive find - that is, if you call it on the Page object, it won't look for the ID in any of the subcontrols..

Try doing this in your BTN_ShowText method instead:

Button clickedButton = sender as Button;

Label TestLabel = (Label)clickedButton.Parent.FindControl("DecisionLabel"));
Button TestButton = (Button)clickedButton.Parent.FindControl("DecisionButton");

The Parent object will be the repeater's ItemTemplate instance that the button was clicked in, so it should hold the controls you are looking for.

Upvotes: 1

hunter
hunter

Reputation: 63512

<asp:Repeater ID="Repeater1" DataSourceID="DecisionDetailsDS" runat="server" OnItemDataBound="Repeater1_DataBound">
    <ItemTemplate>
        <asp:Button ID="DecisionButton" runat="server" OnClick="BTN_ShowText" BorderStyle="None" />
        <asp:Label ID="DecisionLabel" Visible="false" runat="server" />
    </ItemTemplate>
</asp:Repeater>

then in your code behind

protected void Repeater1_DataBound(object sender, RepeaterItemEventArgs e)
{
    (e.Item.FindControl("DecisionButton") as Button).Text 
        = Convert.ToString(e.DataItem["Decision_Type_Dsc"]);
}

and your button event could do something like this:

public void BTN_ShowText(Object sender, EventArgs e)
{
    (sender as Button).Text;
}

That should get you started.

Upvotes: 0

Related Questions