转发器中的下拉列表,所选索引已更改无效

时间:2011-05-19 16:33:47

标签: asp.net vb.net drop-down-menu repeater selectedindexchanged

我有一个带有下拉列表的转发器。当用户更改其索引时,我希望标签更改其值。 (ddlSizes值来自MySQL DB)

Sizes.aspx

<asp:DropDownList ID="ddlSizes" runat="server" AutoPostBack="True" DataSourceID="objdsSizes"  DataTextField="SizeName" DataValueField="SizeID" />

<asp:Label ID="lbldummy" runat="server" Text=""></asp:Label>

Sizes.aspx.vb

Protected Sub ddlSizes_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlSizes.SelectedIndexChanged
    lbldummy = ddlSizes.value
End Sub

但无法识别ddlSizes.SelectedIndexChanged。因此lbldummy的值不会改变。

有什么建议吗?谢谢。

3 个答案:

答案 0 :(得分:7)

您需要为DropDownList创建处理程序,在此期间您需要具有将发件人转换为DropDownList然后获取父控件并将其转换为{{1}的代码}}。然后,您可以引用RepeaterItem

中的任何其他控件
RepeaterItem

然后在你的ddlSizes DropDownList上添加Public Sub ddlSizes_SelectedIndexChanged(sender As Object, e As EventArgs) Dim ddlSizes As DropDownList = DirectCast(sender, DropDownList) Dim ri As RepeaterItem = DirectCast(ddlSizes.Parent, RepeaterItem) Dim lbldummy As Label = DirectCast(ri.FindControl("lbldummy"), Label) lbldummy.Text = ddlSizes.SelectedValue End Sub 并确保它有AutoPostBack =“True”set

答案 1 :(得分:1)

Text可能是默认属性,但我仍然指定它:

lbldummy.Text = ddlSizes.value

但是为此,你真的不需要做回发,你也可以通过Javascript实现这一点。做这样的事情:

<asp:DropDownList ID="ddlSizes" runat="server" onchange="return ddlSizes_change(this);" DataSourceID="objdsSizes"  DataTextField="SizeName" DataValueField="SizeID" />


function ddlSizes_change(dropdown)
{
     document.getElementById('<%= lbldummy.ClientID %>').innerHTML = 
         dropdown.options[myindex].value

    return true;
}

答案 2 :(得分:1)

这是一个例子(C#,但很容易适应VB.NET)。请注意我DdlSizes_SelectedIndexChanged内部如何使用FindControl查找相应的标签:

<%@ Page Language="C#" %>
<script type="text/c#" runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            rep.DataSource = Enumerable.Range(1, 5);
            rep.DataBind();
        }
    }

    protected void DdlSizes_SelectedIndexChanged(object sender, EventArgs e)
    {
        var ddl = (DropDownList)sender;
        var lbl = (Label)ddl.FindControl("lbldummy");
        lbl.Text = ddl.SelectedValue;
    }
</script>
<!DOCTYPE html>
<html>
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="Form1" runat="server">
        <asp:Repeater ID="rep" runat="server">
            <ItemTemplate>
                <asp:DropDownList ID="ddlSizes" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DdlSizes_SelectedIndexChanged">
                    <asp:ListItem Value="1" Text="item 1" />
                    <asp:ListItem Value="2" Text="item 2" />
                    <asp:ListItem Value="3" Text="item 3" />
                </asp:DropDownList>
                <asp:Label ID="lbldummy" runat="server" />
            </ItemTemplate>
        </asp:Repeater>
    </form>
</body>
</html>