我正在使用用户控制页面 (.ascx)。我有一个中继器,里面有 n DropDownList 和一些文本框。我需要触发 SelectedIndexChanged 事件以从文本框中获取值。
这是我得到的错误: “ddlEye_SelectedIndexChanged”不是“ASP.controls_claims_laborder_ascx”的成员。
HTML:
<asp:Repeater ID="rptProducts" runat="server">
<HeaderTemplate>
<tr class="Header">
<td>Eye</td>
<td>Tarrif</td>
<td>Description</td>
<td>Lab Price</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:DropDownList ID="ddlEye" runat="server" OnSelectedIndexChanged="ddlEye_SelectedIndexChanged" AutoPostBack="true">
<asp:ListItem Text="N/A" />
<asp:ListItem Text="Left" />
<asp:ListItem Text="Right" />
</asp:DropDownList>
</td>
<td>
<asp:TextBox ID="txtTariff" runat="server" ReadOnly="true" Text='<%# DataBinder.Eval(Container.DataItem, "Code") %>' />
</td>
<td>
<asp:TextBox ID="txtDescription" runat="server" ReadOnly="true" Text='<%# DataBinder.Eval(Container.DataItem, "Description") %>' />
</td>
<td>
<asp:TextBox ID="txtLabPrice" runat="server" ReadOnly="true" Text='<%# DataBinder.Eval(Container.DataItem, "Invoice") %>' />
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
代码隐藏:
Private Sub ddlEye_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles ddlEye.SelectedIndexChanged
Dim dEye As DropDownList = DirectCast(sender, DropDownList)
If ddlEye.SelectedItem.Value = "Right" Then
lblLeftLensDesc.Text = "Description of the left lens"
lblRightLensDesc.Text = dEye.SelectedItem.Text
ElseIf ddlEye.SelectedItem.Value = "Left" Then
lblLeftLensDesc.Text = dEye.SelectedItem.Text
lblRightLensDesc.Text = "Description of the right lens"
Else
lblLeftLensDesc.Text = "Description of the left lens"
lblRightLensDesc.Text = "Description of the right lens"
End If
End Sub
我也尝试过为 DropDownList 动态创建事件,但仍然不起作用。
Private Sub rptProducts_ItemDataBound(sender As Object, e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptProducts.ItemDataBound
Dim dlEye As DropDownList = e.Item.FindControl("ddlEye")
Dim txtDesc As TextBox = e.Item.FindControl("txtDescription")
If Not dlEye Is Nothing AndAlso Not txtDesc Is Nothing Then
dlEye.SelectedIndexChanged += New EventHandler(ddlEye_SelectedIndexChanged)
dlEye.SelectedValue = txtDesc.Text
End If
End Sub
中继器绑定(bind)代码:
Private Sub loadLineItems(ByVal strTransactionID As String)
Dim objLineItems As ArrayList = Managers.LineItem.GetLineItems(CInt(strTransactionID))
If objLineItems.Count > 0 Then
rptProducts.DataSource = objLineItems
rptProducts.DataBind()
Else
rptProducts.DataSource = Nothing
rptProducts.DataBind()
End If
End Sub
If Not IsPostBack Then
loadLineItems(Session("pstrTransactionID").ToString())
End If
最佳答案
This is the error that I get: 'ddlEye_SelectedIndexChanged' is not a member of 'ASP.controls_claims_laborder_ascx'.
原因是您的事件处理程序是Private
。您要么需要使其成为 Protected
或 Public
,要么您不能在 ascx 上以声明方式添加事件处理程序,但您需要在代码隐藏中以编程方式添加它(通过使用 Handles
或 AddHandler
)。
例如:
Protected Sub ddlEye_SelectedIndexChanged(sender As Object, e As System.EventArgs)
' ...
End Sub
或(注意句柄
)
Private Sub ddlEye_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles ddlEye.SelectedIndexChanged
' ...
End Sub
这里你需要从 ascx 中删除以下内容
OnSelectedIndexChanged="ddlEye_SelectedIndexChanged"
旁注:你使用
AddHandler dlEye.SelectedIndexChanged, AddressOf ddlEye_SelectedIndexChanged
在 VB.NET 中。
更新
Thank you so much it worked. Perhaps you can help me with another issue, I want to get the textbox value in the same row as the DropDownList when setectedIndex changes?
您可以通过NamingContainer
获取RepeaterItem
,您只需要对其进行相应的转换即可。然后您可以使用 FindControl("ID")
获取 TextBox
:
Protected Sub ddlEye_SelectedIndexChanged(sender As Object, e As System.EventArgs)
Dim dEye As DropDownList = DirectCast(sender, DropDownList)
Dim item = DirectCast(dEye.NamingContainer, RepeaterItem)
Dim txtTariff = DirectCast(item.FindControl("txtTariff"), TextBox)
' ...
End Sub
关于vb.net - 中继器内部的 DropDownList SelectedindexChanged 不会触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22805281/