自定义控件不会更新部分回发

时间:2016-06-03 08:19:30

标签: vb.net postback ascx

我有一个自定义控件,我在aspx页面中包含了2。我希望能够从第一个中选择一个值,然后通过部分回发填充第二个值。我花了很多时间在google搜索关于如何做到这一点的答案,但我无法在页面中获得第二个控件以在部分回发后使用新值进行渲染。

CustomDDL.ascx.vb

Imports System.Data
Public Class CustomDDL
    Inherits System.Web.UI.UserControl

    'Input Variables
    Private _OptionText As List(Of String)
    Public Property OptionText() As List(Of String)
        Get
            Return ViewState("OptionText")
        End Get
        Set(value As List(Of String))
            ViewState("OptionText") = value
        End Set
    End Property
    Private _OptionValue As List(Of String)
    Public Property OptionValue() As List(Of String)
        Get
            Return ViewState("OptionValue")
        End Get
        Set(value As List(Of String))
            ViewState("OptionValue") = value
        End Set
    End Property
    Public Property UpdateMode() As UpdatePanelUpdateMode
        Get
            Return Me.DDLUpdatePanel.UpdateMode
        End Get
        Set(ByVal value As UpdatePanelUpdateMode)
            Me.DDLUpdatePanel.UpdateMode = value
        End Set
    End Property

    Public Sub Update()
        Me.DDLUpdatePanel.Update()
    End Sub
    Public Property Width As Integer = 200
    Public Property TextboxHeight As Integer = 30
    Public Property OptionHeight As Integer = 30
    Public Property OptionsToView As Integer = 10
    Public Property TextColor As String = "black"
    Public Property BackgroundColor1 As String = "white"
    Public Property BackgroundColor2 As String = "white"
    Public Property ImageUrl As String = ""
    Public Property TextBoxFontSize As Integer = 12
    Public Property OptionFontSize As Integer = 12
    'Output Variables
    Public Property SelectedText() As String
        Get
            Return TB_Text.Text
        End Get
        Set(value As String)
            TB_Text.Text = value
        End Set
    End Property
    Public Property SelectedValue() As String
        Get
            Return TB_Value.Text
        End Get
        Set(value As String)
            TB_Value.Text = value
        End Set
    End Property
    Public Event SelectedIndexChanged(sender As Object, e As EventArgs)
    Sub btnButton_Click(sender As Object, e As EventArgs) Handles Clicker.Click
        RaiseEvent SelectedIndexChanged(Me, e)
        bindRepeater()
        TB_Text.Text = SelectedText
        TB_Value.Text = SelectedValue
        Selected.InnerHtml = SelectedText
    End Sub

    Private Sub Page_Init(sender As Object, e As System.EventArgs) Handles Me.Init
        Dim stophere As String = ""
    End Sub



    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Selected.Style.Add("font-size", CStr(TextBoxFontSize) + "px")
        DDLOuter.Style.Add("width", CStr(Width) + "px")
        outer.Style.Add("width", CStr(Width) + "px")
        outer.Style.Add("line-height", CStr(TextboxHeight) + "px")
        outer.Style.Add("height", CStr(TextboxHeight) + "px")
        outer.Style.Add("cursor", "pointer")
        divInnerTB.Style.Add("line-height", CStr(TextboxHeight) + "px")
        divInnerTB.Style.Add("height", CStr(TextboxHeight) + "px")
        divInnerTB.Style.Add("width", CStr(Width - 40) + "px")
        divDropimage.Style.Add("line-height", CStr(TextboxHeight) + "px")
        divDropimage.Style.Add("height", CStr(TextboxHeight) + "px")
        Selected.Style.Add("width", CStr(Width - 20) + "px")
        spacer.Style.Add("height", "3px")
        spacer.Style.Add("width", CStr(Width) + "px")
        spacer.Style.Add("color", TextColor)
        spacer.Style.Add("background-color", BackgroundColor1)
        outer.Attributes.Add("onClick", "return toggleDDL('" + Me.ID + "_DDLOuter')")
        If ImageUrl = "" Then
            DropImage.Visible = False
        Else
            DropImage.ImageUrl = ImageUrl
        End If
        If Not IsNothing(OptionText) Then
            Dim amountOptions As Integer = OptionText.Count
            Dim scrollY As String = ""
            Dim scrollheight As Integer = 0
            Dim scrollnumber As Integer = 0
            If amountOptions < OptionsToView Then
                scrollY = "hidden"
                scrollnumber = amountOptions
            Else
                scrollY = "scroll"
                scrollnumber = OptionsToView
            End If
            scrollheight = scrollnumber * OptionHeight
            DDLOuter.Style.Add("overflow-y", scrollY)

            DDLOuter.Style.Add("max-height", CStr(scrollheight) + "px")
            bindRepeater()

        End If

    End Sub
    Private Sub bindRepeater()
        Dim table As New DataTable
        table.Columns.Add("Item", System.Type.GetType("System.String"))
        table.Columns.Add("Value", System.Type.GetType("System.String"))
        table.Columns.Add("Style", System.Type.GetType("System.String"))
        table.Columns.Add("alternatingStyle", System.Type.GetType("System.String"))
        table.Columns.Add("transfer", System.Type.GetType("System.String"))
        For i = 0 To OptionText.Count - 1
            If Not IsNothing(OptionValue) Then
                If Not IsNothing(OptionValue(i)) Then
                    table.Rows.Add(OptionText(i), OptionValue(i), "font-size:" + CStr(OptionFontSize) + "px;cursor:pointer;padding:5px;height:" + CStr(OptionHeight - 10) + "px;width:" + CStr(Width) + "px; color:" + TextColor + ";background-color:" + BackgroundColor1 + ";", "font-size:" + CStr(OptionFontSize) + "px;cursor:pointer;padding:5px;height:" + CStr(OptionHeight - 10) + "px;width:" + CStr(Width) + "px; color:" + TextColor + ";background-color:" + BackgroundColor2 + ";", "transferItem(this.id,""" + Me.ID + "_TB_Text"",""" + Me.ID + "_Selected"",""" + Me.ID + "_Clicker"",""" + OptionValue(i) + """,""" + Me.ID + "_TB_Value"");")
                Else
                    table.Rows.Add(OptionText(i), "", "font-size:" + CStr(OptionFontSize) + "px;cursor:pointer;padding:5px;height:" + CStr(OptionHeight - 10) + "px;width:" + CStr(Width) + "px; color:" + TextColor + ";background-color:" + BackgroundColor1 + ";", "font-size:" + CStr(OptionFontSize) + "px;cursor:pointer;padding:5px;height:" + CStr(OptionHeight - 10) + "px;width:" + CStr(Width) + "px; color:" + TextColor + ";background-color:" + BackgroundColor2 + ";", "transferItem(this.id,""" + Me.ID + "_TB_Text"",""" + Me.ID + "_Selected"",""" + Me.ID + "_Clicker"","" "",""" + Me.ID + "_TB_Value"");")
                End If
            Else
                table.Rows.Add(OptionText(i), "", "font-size:" + CStr(OptionFontSize) + "px;cursor:pointer;padding:5px;height:" + CStr(OptionHeight - 10) + "px;width:" + CStr(Width) + "px; color:" + TextColor + ";background-color:" + BackgroundColor1 + ";", "font-size:" + CStr(OptionFontSize) + "px;cursor:pointer;padding:5px;height:" + CStr(OptionHeight - 10) + "px;width:" + CStr(Width) + "px; color:" + TextColor + ";background-color:" + BackgroundColor2 + ";", "transferItem(this.id,""" + Me.ID + "_TB_Text"",""" + Me.ID + "_Selected"",""" + Me.ID + "_Clicker"","" "",""" + Me.ID + "_TB_Value"");")
            End If
        Next
        repeater.DataSource = table
        repeater.DataBind()
    End Sub

    Private Sub Page_Unload(sender As Object, e As System.EventArgs) Handles Me.Unload
        Dim stophere As String = ""
    End Sub
End Class

CustomDDL.ascx

<%@ Control Language="vb" ClassName="CustomControl.CustomDDL" AutoEventWireup="false"
    CodeBehind="CustomDDL.ascx.vb" Inherits="CustomControlLibrary.CustomDDL" %>
<style type="text/css">
    .DDLOuter
    {
        transition: height 1s;
        transition-timing-function: linear;
    }

    .Open
    {
        transition-property: height;
        transition-timing-function: linear;
        transition-duration: 0s;
    }
</style>
<script type="text/javascript">
    var DDLID;
    if (typeof toggleDDL != 'function') {
        function toggleDDL(id) {
            DDLID = id;
            clearClass(id, resetClass);

        }
    }
    if (typeof clearClass != 'function') {
        function clearClass(id, callBack) {
            var divs = document.getElementsByClassName("Open");
            for (var i = 0; i < divs.length; i++) {
                if (divs.item(i).id != id) {
                    divs.item(i).style.height = "0%";

                }
            }
            return callBack(id, toggleClass);
        }
    }
    if (typeof resetClass != 'function') {
        function resetClass(id, callBack) {
            var divs = document.getElementsByClassName("Open");
            for (var i = 0; i < divs.length; i++) {
                if (divs.item(i).id != id) {
                    var DivClass2 = divs.item(i).className;
                    divs.item(i).className = DivClass2.replace("Open", "")
                }
            }
            return callBack(id);
        }
    }
    if (typeof toggleClass != 'function') {
        function toggleClass(id) {
            if (document.getElementById(id).style.height == "100%") {
                document.getElementById(id).style.height = "0%";
                var DivClass = document.getElementById(id).className;
                setTimeout(function () {
                    document.getElementById(id).className = DivClass.replace("Open", "")
                }, 1000);
            } else {
                document.getElementById(id).style.height = "100%";
                setTimeout(function () {
                    document.getElementById(id).className += " Open"

                }, 1000);

            }
            return false;
        }
    }

    if (typeof transferItem != 'function') {
        function transferItem(id, target, display, button, OptVal, OptTarget) {
            var value = document.getElementById(id).innerHTML;
            document.getElementById(target).value = value;
            document.getElementById(OptTarget).value = OptVal;
            document.getElementById(display).innerHTML = value;
            document.getElementById(button).click();
            toggleDDL(DDLID);
        }
    }


</script>
<asp:UpdatePanel ID="DDLUpdatePanel" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
        <div>
            <div id="outer" runat="server" style='border-radius: 3px; color: #404040; font-size: 13px;
                background-color: White; cursor: pointer;'>
                <div runat="server" id="divInnerTB" style="float: left;">
                    <div runat="server" style="margin-left: 10px; font-size: 16px;" id="Selected">
                    </div>
                    <div style="clear: both;">
                    </div>
                </div>
                <div id="divDropimage" runat="server" style="float: right; width: 30px; text-align: center;
                    margin-right: 10px;">
                    <asp:Image runat="server" ID="DropImage" Style="max-height: 30px; max-width: 30px;
                        vertical-align: middle;" />
                </div>
                <div style="clear: both;">
                </div>
                <asp:TextBox runat="server" ID="TB_Text" Style="display: none;"></asp:TextBox>
                <asp:TextBox runat="server" ID="TB_Value" Style="display: none;"></asp:TextBox>
            </div>
            <div id="DDLOuter" class="DDLOuter" runat="server" style="height: 0%; position: absolute;
                overflow-x: hidden; margin-top: -3px">
                <div id="DDLInner">
                    <div runat="server" id="spacer" style="height: 3px">
                        &nbsp;</div>
                    <asp:Button runat="server" ID="Clicker" Style="display: none;" />
                    <asp:Repeater runat="server" ID="repeater">
                        <ItemTemplate>
                            <div style='<%#DataBinder.Eval(Container, "DataItem.style")%>'>
                                <asp:Label onClick='<%#DataBinder.Eval(Container, "DataItem.transfer")%>' runat="server"
                                    ID="Item" Text='<%#DataBinder.Eval(Container, "DataItem.Item")%>'></asp:Label>
                            </div>
                        </ItemTemplate>
                        <AlternatingItemTemplate>
                            <div style='<%#DataBinder.Eval(Container, "DataItem.alternatingstyle")%>'>
                                <asp:Label onClick='<%#DataBinder.Eval(Container, "DataItem.transfer")%>' runat="server"
                                    ID="Item" Text='<%#DataBinder.Eval(Container, "DataItem.Item")%>'></asp:Label>
                            </div>
                        </AlternatingItemTemplate>
                    </asp:Repeater>
                </div>
            </div>
        </div>
    </ContentTemplate>
</asp:UpdatePanel>

WebForm1.aspx的

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body style="background-color: Gray">
    <form id="form1" runat="server">
    <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server" EnableScriptGlobalization="True"
        EnableScriptLocalization="True" EnablePartialRendering="true">
    </asp:ToolkitScriptManager>
    <div>
        <asp:UpdatePanel runat="server" ID="update1" UpdateMode="Conditional">
            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="first" EventName="SelectedIndexChanged" />
            </Triggers>
            <ContentTemplate>
                <asp:CustomDDL runat="server" ID="first" />
                <asp:CustomDDL runat="server" ID="second" />
            </ContentTemplate>
        </asp:UpdatePanel>
    </div>
    </form>
</body>
</html>

Webform1.aspx.vb

Public Class WebForm1
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim a As New List(Of String)
            Dim b As New List(Of String)

            a.Add("First")
            b.Add("1")
            a.Add("First")
            b.Add("1")
            a.Add("First")
            a.Add("First")

            b.Add("1")
            b.Add("1")
            first.OptionValue = b
            first.OptionText = a
        End If
    End Sub

    Private Sub testthis_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles first.SelectedIndexChanged
        Dim a As New List(Of String)
        Dim b As New List(Of String)

        a.Add("second")
        b.Add("1")
        a.Add("second")
        b.Add("1")
        a.Add("second")
        a.Add("second")

        b.Add("1")
        b.Add("1")
        second.OptionValue = b
        second.OptionText = b

    End Sub
End Class

有人对我在哪里出错了吗?

1 个答案:

答案 0 :(得分:0)

您是否尝试在WebForm1.aspx中的customDDL中添加AutoPostBack="true"

<asp:CustomDDL runat="server" ID="first" AutoPostBack="true" />