SelectedIndexChange Not Firing

2019-07-19 01:43发布

问题:

I'm having issue with a dropdownlist updating a textbox, both held within a listview, within an update panel which in turn is in an item template.

Updated

I have got this working with the same code without the above containers in a different web page on the same project, however having trouble linking it with the lisview and other containers.

I am unsure of where the problem lies, the onClick isn't firing unless there's a call back to the server, regardless whether the drop down is contained within the containers mentioned above.

Any help would be greatly appreciated, thanks in advance.

Using asp (1st) and VB code behind (2nd).

<InsertItemTemplate>
<asp:panel runat="server" ChildrenAsTriggers="true" UpdateMode="Always">
<asp:ListView ID="ListView1" runat="server" InsertItemPosition="FirstItem" IAllowPaging="True" EnableViewState="true">
<tr>
<td>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Details")%>' TextMode="MultiLine" />
</td>
<td>
<asp:DropDownList ID="DLL" runat="server" OnSelectedIndexChanged="DLL_SelectedIndexChanged" AutoPostBack="true "EnableViewState="true">
    <asp:ListItem>Select</asp:ListItem>
    <asp:ListItem Value="1">Yes</asp:ListItem>
    <asp:ListItem Value="2">No</asp:ListItem>
    <asp:ListItem Value="3">Maybe</asp:ListItem>
    <asp:ListItem Value="4">I dont know</asp:ListItem>
    <asp:ListItem Value="5">Can you repeat</asp:ListItem>
    <asp:ListItem Value="6">the question</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
</asp:panel>
</InsertItemTemplate>

Code behind

    Protected Sub DDL_SelectedIndexChanged(sender As Object, e As EventArgs)

    Dim ddl As DropDownList = DirectCast(sender, DropDownList) 
    Dim listviewItemThing = DirectCast(sender.parent.NamingContainer, ListViewItem) 
    Dim tb As TextBox = DirectCast(ddl.NamingContainer.FindControl("TextBox2"), TextBox)

    If ddl.SelectedValue = 1 Then
        tb.Text = My.Computer.FileSystem.ReadAllText("E:\Users\han\Documents\Templates\1.txt")
    ElseIf ddl.SelectedValue = 2 Then
        tb.Text = My.Computer.FileSystem.ReadAllText("E:\Users\han\Documents\Templates\2.txt")
    ElseIf ddl.SelectedValue = 3 Then
        tb.Text = My.Computer.FileSystem.ReadAllText("E:\Users\han\Documents\Templates\3.txt")
    ElseIf ddl.SelectedValue = 4 Then
        tb.Text = My.Computer.FileSystem.ReadAllText("E:\Users\han\Documents\Templates\4.txt")
    ElseIf ddl.SelectedValue = 5 Then
        tb.Text = My.Computer.FileSystem.ReadAllText("E:\Users\han\Documents\Templates\5.txt")
    ElseIf ddl.SelectedValue = 6 Then
        tb.Text = My.Computer.FileSystem.ReadAllText("E:\Users\han\Documents\Templates\6.txt")
    Else
        tb.Text = ""
    End If
End Sub

Update 2

As per request please see attached screen shot of browser console error in debug on VS2013

And expanded error.

Update 3

Added JQuery to try to force PostBack.

         function JsFunction() {
         __doPostBack('DLL_SelectedIndexChanged', '');
     }

ASP link to JQ

<asp:DropDownList ID="DDL" runat="server" Width="120px" OnSelectedIndexChanged="DDL_SelectedIndexChanged" AutoPostBack="true" CausesValidation="false" EnableViewState="true" onchange="javascript:JsFunction()">

回答1:

You have correct code for your drop down list, so error in other place.

As you see in error message: when you try submit form problem with HtmlEditorExtender.
So just remove or disable it for quick fixing problem.

As for error with HtmlEditorExtender we need a little bit information, of course, if you still need solve it.



回答2:

Assuming these controls are within the ItemTemplate of your ListView:

  • FindControl("DDL") won't work, because it's trying to find the control within the page;
  • ListView1.FindControl("TextBox2") won't work, because there will be multiple instances of TextBox2 within the ListView.

Try this instead:

Dim ddl As DropDownList = DirectCast(sender, DropDownList)
Dim tb As TextBox = DirectCast(ddl.NamingContainer.FindControl("TextBox2"), TextBox)


回答3:

I assume you haven't got the typo in your actual code:

OnSelectedIndexChanged="DLL_SelectedIndexChanged"

Where the event handler is DDL_SelectedIndexChanged.

Have you put a breakpoint on to check whether the event handler is not being called or if it is bailing out at some point after it fails to do the cast you want it to do?