开发者

dynamically created Dropdownlist in a dynamically created updatepanel will generate a full page reload

I have a page witch builds lots of updatepanel with controls in them on the pre_init. (and sometimes I have updatepanels inside another updatepanel)

In a table cell, I have a dropdownlist and a checkbox. They are both autopostack. They have the exact same code behind (one on checkedChanged and the other on selectedindexchaged).

The checkbox will work perfectly. The dropdownlist on the other hand is causing a full page reload ...

Anyone has a clue why ? I've lost soooooo many time on this ...

Thanks !

CodeBehind : Public Class ProductBuilder2 Inherits System.Web.UI.Page

Private Sub ProductBuilder2_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit

    BuildProduct(Request.QueryString("ProductID"))

End Sub


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

    If Not Page.IsPostBack Then
        BindProductID()
    End If

End Sub


Protected Sub BuildProduct(ByVal ProductID As Integer)

    Dim xMasterProd As New cProduct(ProductID)

    If xMasterProd.IsGroup Then
        For Each xKitProd As cKit In xMasterProd.Kit
            If xKitProd.Product.IsGroup Then
                Renderer.Controls.Add(CreateGroup(xKitProd.Product, xKitProd.DoNotInclude))
            End If

            If Not xKitProd.Product.IsGroup Then
                Renderer.Controls.Add(CreateProduct(xKitProd.Product, xKitProd.DoNotInclude))
            End If
        Next
    End If

    If Not xMasterProd.IsGroup Then
        Renderer.Controls.Add(CreateProduct(xMasterProd, False))
    End If


End Sub


Public Function CreateProduct(ByRef xProduct As cProduct, _
                              ByVal DoNotInclude As Boolean) As UpdatePanel

    Dim xTable As New Table
    Dim xTR As New TableRow
    Dim xTD As New TableCell

    xTable.CssClass = "Container"

    Dim lblProductName As New Label
    lblProductName.Text = xProduct.Name
    xTD.CssClass = "ProductName"
    xTD.Attributes.Add("colspan", "3")
    xTD.Controls.Add(lblProductName)
    xTR.Controls.Add(xTD)

    If DoNotInclude Then
        Dim xChkDNI As New CheckBox
        xChkDNI.Text = "Do not include"
        xChkDNI.CssClass = "DoNotInclude"
        xChkDNI.AutoPostBack = True
        AddHandler xChkDNI.CheckedChanged, AddressOf DoNotInclude_CheckedChanged
        xTD.Controls.Add(xChkDNI)
        xTR.Controls.Add(xTD)
    End If
    xTable.Controls.Add(xTR)

    xTR = New TableRow
    xTD = New TableCell
    Dim lblProductDescription As New Label
    lblProductDescription.Text = xProduct.Description
    xTD.Attributes.Add("colspan", "3")
    xTD.CssClass = "ProductDescription"
    xTD.Controls.Add(lblProductDescription)
    xTR.Controls.Add(xTD)
    xTable.Controls.Add(xTR)

    For Each xOpt As cOption In xProduct.OptionLabels
        xTR = New TableRow
        xTD = New TableCell
        xTD.CssClass = "ProductDescription"
        xTD.Style.Add("width", "150px;")
        Dim xlblOptionLabel As New Label
        xlblOptionLabel.Text = xOpt.LabelText & " : "
        xTD.Controls.Add(xlblOptionLabel)
        xTR.Controls.Add(xTD)

        xTD = New TableCell
        Dim DropOptionID As New DropDownList
        xTD.Controls.Add(DropOptionID)
        xTR.Controls.Add(xTD)

        xTable.Controls.Add(xTR)

        For Each OptionID As cOption In xOpt.Options
            Dim xListItem As New ListItem
            xListItem.Text = OptionID.LabelText
            xListItem.Value = OptionID.ID
            If OptionID.IsDefault Then xListItem.Selected = True
            DropOptionID.Items.Add(xListItem)
        Next

        xTD = New TableCell
        Dim lblMessage As New Label
        lblMessage.Text = xOpt.Message
        lblMessage.CssClass = "Message"
        If xOpt.Message = "" Then lblMessage.Visible = False
        xTD.Controls.Add(lblMessage)
        xTR.Controls.Add(xTD)

        xTable.Controls.Add(xTR)

    Next

    Dim UP As UpdatePanel = CreateUpdatePanel("UP-" & xProduct.ID)
    UP.ContentTemplateContainer.Controls.Add(xTable)

    Return UP

End Function


Public Function CreateGroup(ByRef xProduct As cProduct, _
                            ByVal DoNotInclude As Boolean) As UpdatePanel

    '1st create the group container
    Dim xTable As New Table
    Dim xTR As New TableRow
    Dim xTD As New TableCell

    xTable.CssClass = "Container"

    xTD.CssClass = "ProductGroup"
    xTD.Attributes.Add("colspan", "3")

    Dim xLabel As New Label
    xLabel.Text = xProduct.Name & " : "
    xTD.Controls.Add(xLabel)

    Dim DropProdID As New DropDownList
    DropProdID.AutoPostBack = True
    DropProdID.ID = "DropProdID" & xProduct.ID
    AddHandler DropProdID.SelectedIndexChanged, AddressOf DropProdID_SelectedIndexChanged
    Dim Trigger As New PostBackTrigger
    Trigger.ControlID = DropProdID.ID
    xTD.Controls.Add(DropProdID)
    xTR.Controls.Add(xTD)

    If DoNotInclude Then
        Dim xChkDNI As New CheckBox
        xChkDNI.Text = "Do not include"
        xChkDNI.CssClass = "DoNotInclude"
        xChkDNI.AutoPostBack = True
        AddHandler xChkDNI.CheckedChanged, AddressOf DoNotInclude_CheckedChanged
        xTD.Controls.Add(xChkDNI)
        xTR.Controls.Add(xTD)
    End If

    xTable.Controls.Add(xTR)

    For Each xKitProd As cKit In xProduct.Kit
        Dim xListItem As New ListItem
        xListItem.Text = xKitProd.Product.Name
        xListItem.Value = xKitProd.Product.ID
        If xKitProd.IsDefault Then
            xListItem.Selected = True
            If Not xKitProd.Product.IsGroup Then
                xTR = New TableRow
                xTD = New TableCell
                xTD.Controls.Add(CreateProduct(xKitProd.Product, xKitProd.DoNotInclude))
                xTR.Controls.Add(xTD)
                xTable.Controls.Add(xTR)
            End If
            For Each xProd As cKit In xKitProd.Product.Kit
                xTR = New TableRow
                xTD = New TableCell
                xTD.Controls.Add(CreateProduct(xProd.Product, xProd.DoNotInclude))
                xTR.Controls.Add(xTD)
                xTable.Controls.Add(xTR)
            Next
        End If
        DropProdID.Items.Add(xListItem)
    Next

    Dim UP As Update开发者_JS百科Panel = CreateUpdatePanel("UP-" & xProduct.ID)
    UP.ContentTemplateContainer.Controls.Add(xTable)
    UP.Triggers.Add(Trigger)
    Return UP

End Function


Protected Sub DoNotInclude_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)

    Dim xChk As CheckBox = CType(sender, CheckBox)
    Dim xTable As Table = xChk.Parent.Parent.Parent
    Dim UP As UpdatePanel = xTable.Parent.Parent
    Dim Index As Integer = 1

    For Each xTR As TableRow In xTable.Rows
        If Index <> 1 Then
            xTR.Visible = Not xChk.Checked
        End If
        Index += 1
    Next

    UP.Update()

End Sub


Protected Sub DropProdID_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)

    Dim DropProdID As DropDownList = CType(sender, DropDownList)
    Dim xTable As Table = DropProdID.Parent.Parent.Parent
    Dim UP As UpdatePanel = xTable.Parent.Parent
    Dim Index As Integer = 1

    For Each xTR As TableRow In xTable.Rows
        If Index <> 1 Then
            xTR.Visible = False
            'xTable.Rows.RemoveAt(Index)
        End If
        Index += 1
    Next

    'Dim xProduct As New cProduct(DropProdID.SelectedValue)

    'If xProduct.IsGroup Then

    'End If

    'If Not xProduct.IsGroup Then
    '    Dim xTR As New TableRow
    '    Dim xTD As New TableCell
    '    xTD.Controls.Add(CreateProduct(xProduct, False))
    '    xTR.Controls.Add(xTD)
    '    xTable.Controls.Add(xTR)
    'End If

    UP.Update()

End Sub



Public Sub RenderPanel(ByRef xPanel As Panel)

    Renderer.Controls.Add(xPanel)

End Sub


Public Function CreateUpdatePanel(ByVal ID As String) As UpdatePanel

    Dim UP As New UpdatePanel()

    UP.ID = ID
    UP.UpdateMode = UpdatePanelUpdateMode.Conditional
    UP.RenderMode = UpdatePanelRenderMode.Inline
    UP.ChildrenAsTriggers = False

    Return UP

End Function


Private Sub ProductID_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ProductID.SelectedIndexChanged

    Response.Redirect("ProductBuilder2.aspx?ProductID=" & ProductID.SelectedValue)

End Sub


Protected Sub BindProductID()

    Dim xProd As New cProduct

    ProductID.DataTextField = "Name"
    ProductID.DataValueField = "ID"

    ProductID.DataSource = xProd.ToList()
    ProductID.DataBind()

End Sub

End Class

.aspx :

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="ProductBuilder2.aspx.vb" Inherits="OLO3._0._0.ProductBuilder2" %>

<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>

OLO3.0 / Product Builder .Container {margin: 10px 10px 10px 10px; background-color: #ffffff; border: 1px solid #c2c2c2; width: 98%;} .ProductGroup {background-color: #c3e7b4; border: 1px solid #417b2a; padding: 5px 5px 5px 5px; font: 10pt Verdana; font-weight: bold; color: #417b2a;} .ProductName {background-color: #d8e4f0; border: 1px solid #567db0; padding: 5px 5px 5px 5px; font: 10pt Verdana; font-weight: bold; color: #567db0;} .ProductDescription {padding: 5px 5px 5px 5px; font: 9pt Verdana;} .Message {background-color: #f8d7d8; border: 1px solid #ed2227; padding: 2px 2px 2px 2px; font: 10pt Verdana;} .DoNotInclude {font : 9pt Verdana; margin-left: 20px; font-weight: normal;}

<asp:ScriptManager ID="SM1" runat="server"></asp:ScriptManager>

<telerik:RadFormDecorator ID="RadFormDecorator1" runat="server" Skin="Sitefinity" DecoratedControls="All" />

<table border="0" cellpadding="0" cellspacing="0" width="100%">
    <tr>
        <td colspan="3" align="center"><img src="../Img/Logo.png" alt="Paradox" style="padding-top: 20px; padding-bottom: 20px;" /></td>
    </tr>
    <tr>
        <td style="width: 150px;" valign="top">Product Tree (150px)
            <asp:DropDownList ID="ProductID" runat="server" AppendDataBoundItems="true" Width="140" AutoPostBack="true"><asp:ListItem Text="Select a product" Value=""></asp:ListItem></asp:DropDownList>
        </td>
        <td valign="top" style="background-color: #f2f2f2;">

            <asp:Panel ID="Renderer" runat="server">

            </asp:Panel>

            <asp:UpdatePanel ID="UPMessage" runat="server" UpdateMode="Conditional" RenderMode="Inline">
                <ContentTemplate>
                    <asp:Label ID="lblmessage" runat="server"></asp:Label>
                </ContentTemplate>
            </asp:UpdatePanel>

        </td>
        <td style="width: 300px;" valign="top">Cart Resume (300px)</td>
    </tr>
    <tr>
        <td colspan="3">Footer</td>
    </tr>
</table>

</form>


if you have your update panels within update panels you may not trigger a partial postback for the outside of the panel. May sure you specify the triggers with inline or code behind. Without seeing the code this is what I would suggest.


Found it ... had to set ChildrendHasTrigger=true ... :P

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜