CustomX
CustomX

Reputation: 10113

Repeater won't let me access controls like buttons, dropdown, etc

I'm using a repeater ListOfArticles and have controls inside it like ddlSizes and btnSelectArticle. Normally you can just double click the control and in the aspx.vb page you can specify an action. I have heard something about Findcontrol, but can't figure out or find much information that I understand. I don't want to sound like an ass, but I would really prefer help for the aspx.vb page and not in C# or Javascript.

An example of what I'm trying to do is, once you've clicked btnSelectArticle the label lblSelection receives the following values Amount: txtAmount - Size: ddlSizes.SelectedValue.

<asp:Repeater ID="rptListOfArticles" runat="server" DataSourceID="objdsArticleList">

<asp:DropDownList ID="ddlSizes" runat="server" AutoPostBack="True" DataSourceID="objdsSizes"  DataTextField="SizeName" DataValueField="SizeID" OnSelectedIndexChanged="ddlSizes_SelectedIndexChanged" />

<asp:Button ID="btnSelect" runat="server" Text="Select" OnClick="btnSelect_OnClick" />

<asp:Label ID="lblSelection" runat="server" Text=""></asp:Label>

In the aspx.vb page I can only select this and my controls like ddlSizes and btnSelect aren't recognized.

Protected Sub rptListOfArticles_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs) Handles rptListOfArticles.ItemCommand
End Sub

Any help towards a solution would be great!

Upvotes: 1

Views: 1295

Answers (2)

Tim B James
Tim B James

Reputation: 20364

What you need to do is use the FindControl method to find the specific control in the selected repeater Item.

so an example would be (within the ItemCommand method)

Dim lblSelection as Label = CType(e.Item.FindControl("lblSelection"), Label)
lblSelection.Text = "Your Text"

Edit ** To Answer your questions in the comments:

Yes to access the SelectedValue of the ddlSize DropDown you will need to create this:

Dim ddlSize As DropDownList = Ctype(e.Item.FindControl("ddlSize"), DropDownList)

The Repeater will know when to call this method when any Buttons are Clicked within the Repeater. Add a CommandName to your buttons so that you can then control what happens in the ItemCommand method.

e.g.

<asp:Button id="btnDoSomething" runat="server" text="Run ItemCommand" CommandName="Command1" />

In the ItemCommand use the code:

If e.CommandName = "Command1" Then
    ' run your code
End If

Upvotes: 2

Devjosh
Devjosh

Reputation: 6486

You can handle the event of dropdownlist in ItemCommand Event. Event bubbling concept comes here actually the child control bubble the evenet up to its parent i.e repeater control so you can handle it in parent control event eventually for more details HERE you will have indepth insight of all events of repeater

Upvotes: 1

Related Questions