I am using User Control Pages (.ascx). I have a repeater and inside it I have n DropDownList and some textboxes. I need to trigger SelectedIndexChanged event to get values from textbox.
This is the error that I get: 'ddlEye_SelectedIndexChanged' is not a member of '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>
Codebehind:
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
I have also tried to dynamically create event for DropDownList but that still does not work.
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
Repeater Binding Code:
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'.
The reason is that your event handler is Private
. You either need to make it Protected
or Public
or you can't add the event-handler declaratively on the ascx but you need to add it programmatically in codebehind (by using Handles
or AddHandler
).
So for example:
Protected Sub ddlEye_SelectedIndexChanged(sender As Object, e As System.EventArgs)
' ...
End Sub
or (note the Handles
)
Private Sub ddlEye_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles ddlEye.SelectedIndexChanged
' ...
End Sub
Here you need to remove following from ascx
OnSelectedIndexChanged="ddlEye_SelectedIndexChanged"
Side-note: you use
AddHandler dlEye.SelectedIndexChanged, AddressOf ddlEye_SelectedIndexChanged
in VB.NET.
Update
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?
You can get the RepeaterItem
via NamingContainer
, you just need to cast it accordingly. Then you can use FindControl("ID")
to get the 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