Repeater中的DropDownList客户端更改事件

时间:2016-12-29 20:05:34

标签: asp.net vb.net

我有一个转发器

 <asp:Repeater ID = "rpQuestion8" runat = "server" >
                    <ItemTemplate>
                        <tr>
                            <td>
                                <asp:DropDownList ID = "ddCounty" runat = "server" AutoPostBack="true" />
                                <input type = "hidden" id = "hidSurveyCustomerAreaID" runat = "server" value = '<%#showData(Container.DataItem, "SurveyCustomerAreaID")%>' />
                            </td>
                              <td>
                                <asp:radiobutton id = "radStillStandHuntMethod" text = "Still/Stand" runat = "server" GroupName = "Question8HuntMethod"/>
                                &nbsp;&nbsp;&nbsp;
                                <asp:radiobutton id = "radDogHuntMethod" text = "Dog" runat = "server" GroupName = "Question8HuntMethod"/>
                            </td>
                            <td>
                                <asp:TextBox ID = "txtDaysHunted" runat = "server" Width = "50" text  = '<%#showData(Container.DataItem, "DaysHunted")%>'/>
                                <asp:CompareValidator ID = "cvNumDays" runat = "server" ControlToValidate = "txtDaysHunted" Operator = "DataTypeCheck" Type = "Integer" Text = "*" ErrorMessage = "Please enter a whole number for Days Hunted." />
                            </td>
                            <td>
                                <asp:radiobutton id = "radYesUnprocessedFood" text = "Yes" runat = "server" GroupName = "Question8UnprocessedFood" />
                                &nbsp;&nbsp;&nbsp;
                                <asp:radiobutton id = "radNoUnprocessedFood" text = "No" runat = "server" GroupName = "Question8UnprocessedFood" />
                            </td>
                            <td>
                                <asp:TextBox ID = "txtNumSeen" runat = "server" Width = "50"  text  = '<%#showData(Container.DataItem, "NumberSeen")%>' />
                                <asp:CompareValidator ID = "cvBearsSeen" runat = "server" ControlToValidate = "txtNumSeen" Operator = "DataTypeCheck" Type = "Integer" Text = "*" ErrorMessage = "Please enter a whole number for Number of Bears Seen." />
                            </td>
                            <td>
                                <asp:radiobutton id = "radYes" text = "Yes" runat = "server" GroupName = "Question8Harvest" />
                                &nbsp;&nbsp;&nbsp;
                                <asp:radiobutton id = "radNo" text = "No" runat = "server" GroupName = "Question8Harvest" />
                            </td>
                        </tr>
                    </ItemTemplate>
                </asp:Repeater>

我想要达到的目标是:  每当ddCounty.selectedValue ==&#34;&#34;  而禁用同一行/项中的所有字段。  否则,启用同一项或多行中的字段。

我试过这个代码,但它永远不会触发

  Private Sub ddCounty_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles ddCounty.SelectedIndexChanged
        Dim ddCounty As DropDownList = CType(sender, DropDownList)
        Dim Item As RepeaterItem = CType(ddCounty.NamingContainer, RepeaterItem)
        Dim txtNumDays As TextBox = CType(Item.FindControl("txtDaysHunted"), TextBox)

        If ddCounty.SelectedValue = "" Then
            txtNumDays.Enabled = False
        Else
            txtNumDays.Enabled = True
        End If
    End Sub

另外,我不知道如何使用JavaScript实现这一目标。 提前谢谢!

1 个答案:

答案 0 :(得分:1)

如果您只想在客户端禁用控件,则无需调用SelectedIndexChanged服务器端事件。

您可以使用JavaScript在客户端轻松实现。

<asp:DropDownList ID="ddCounty" runat="server" onchange="ddCountyChange(this);" />

将此脚本放在Repeater控件按钮上。如果选择的值为&#34;&#34;。

,它将基本上禁用兄弟输入控件
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script type="text/javascript">
    function ddCountyChange(sender) {
        if (sender.value === "") {
            $(sender).parent().siblings().find("input").attr("disabled", "disabled");
        } else {
            $(sender).parent().siblings().find("input").removeAttr('disabled');
        }
    }
</script>