我正在使用用戶控制頁面(.ascx)。我有一箇中繼器,裏面有n個DropDownList和一些文本框。我需要觸發SelectedIndexChanged事件來從文本框中獲取值。DropDownList SelectedindexChanged inside repeater does not fire
這是我得到的錯誤: '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
直放站綁定代碼:
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
我假設你在每次回發時都要綁定'Repeater'(或者你的'UserControl'),而不是'If Not Page.IsPostaBack'。 –
不,我不會在每一個回帖中都有約束力 – Gericke
我相信你。但是,將數據綁定到數據綁定的位置並不是所有數據綁定的東西,而是調用'rptProducts.DataBind()'的部分。 –