1

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!

CustomX
  • 9,948
  • 30
  • 85
  • 115

2 Answers2

2

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
Tim B James
  • 20,084
  • 4
  • 73
  • 103
  • so if I were to have `lblSelection.Text` = `ddlSize.SelectedValue` I would need to make a `Dim ddlSize as Dropdown`? – CustomX May 20 '11 at 10:00
  • yup!absolutely you have to make the control object in code behind and initialize it using e.findcontrol – Devjosh May 20 '11 at 10:18
  • and how does the repeater know when its called upon? Like when I click the button btnSelect, how does it know to pull the function? – CustomX May 20 '11 at 11:08
  • in the itemCommand, do you mean if e.CommandName and not ItemCommand? – CustomX May 20 '11 at 11:25
  • WOOT you're a life saver Tim :) – CustomX May 20 '11 at 11:28
  • EXTRA: If I wanted to do the same for a dropdownlist, in the aspx page add `OnSelectedIndexChanged = "ddlChanged"` and in the aspx.vb page `if e.CommandName = "ddlChanged"`? – CustomX May 20 '11 at 11:31
  • For this you will need to get the Index of the Repeater Item in which the DropDownList resides and then find the controls within this RepeaterItem. Probably best start a new question for this, or give it a google search first. – Tim B James May 20 '11 at 11:48
  • Well I tried this with: http://stackoverflow.com/questions/6061857/dropdownlist-in-a-repeater-selected-index-changed-not-working, nothing really useful came out of it. – CustomX May 20 '11 at 23:11
1

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

Devjosh
  • 6,450
  • 4
  • 39
  • 61