简体   繁体   English

转发器不会让我访问按钮,下拉菜单等控件

[英]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 . 我正在使用转发器ListOfArticles并且内部有ddlSizesbtnSelectArticle之类的控件。 Normally you can just double click the control and in the aspx.vb page you can specify an action. 通常,您只需双击控件,然后在aspx.vb页面中可以指定一个动作。 I have heard something about Findcontrol, but can't figure out or find much information that I understand. 我已经听说过有关Findcontrol的内容,但无法弄清或找到很多我了解的信息。 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. 我不想听起来像个驴子,但我确实希望为aspx.vb页面提供帮助,而不是C#或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 . 我尝试做的一个示例是,一旦您单击btnSelectArticle ,标签lblSelection收到以下值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. 在aspx.vb页面中,我只能选择此ddlSizes ,而无法识别ddlSizesbtnSelect类的控件。

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! 对解决方案的任何帮助都将是巨大的!

What you need to do is use the FindControl method to find the specific control in the selected repeater Item. 您需要做的是使用FindControl方法在选定的转发器项目中查找特定的控件。

so an example would be (within the ItemCommand method) 所以一个例子是(在ItemCommand方法中)

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: 是,要访问ddlSize DropDown的SelectedValue,您将需要创建以下代码:

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. 将CommandName添加到您的按钮,以便您随后可以控制ItemCommand方法中发生的情况。

eg 例如

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

In the ItemCommand use the code: 在ItemCommand中使用代码:

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

You can handle the event of dropdownlist in ItemCommand Event. 您可以在ItemCommand事件中处理dropdownlist事件。 Event bubbling concept comes here actually the child control bubble the evenet up to its parent ie 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 事件冒泡的概念实际上是这里的子控件将它的偶像泡到了父控件,即转发器控件,因此您最终可以在父控件事件中对其进行处理以获取更多详细信息, 这里您将深入了解转发器的所有事件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM