开发者

UpdatePanel async postback not updating content

I have an issue very similar to this question. There is a dropdown on my page that causes a postback, during which the ImageUrl property of an ASP:Image is changed. When that postback happens, any value that is in the FileUpload is cleared. That's the problem I'm trying to solve, but I ran into this issue in the process.

I'm trying to solve the problem by wrapping the dropdown and image in an UpdatePanel. Here is my ASP markup:

<asp:UpdatePanel ID="upPanel" runat="server">
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="myDropdown" 
           EventName="SelectedIndexChanged" />
    </Triggers>
    <ContentTemplate>
        <tr valign="top">
        <td>Tag:</td>
        <td>
            <asp:DropDownList ID="myDropdown" runat="server" 
                        AppendDataBoundItems="true" DataTextField="Name" 
                        DataValueField="ID" AutoPostBack="true">
                <asp:ListItem Value="" />
            </asp:DropDownList>
        </td>
        </tr>
        <TR vAlign="top">
        <TD width="150">Thumbnail:</TD>
        <TD>
                <asp:Image id="imgThumbnail" Runat="server" 
                 开发者_Go百科    AlternateText="No Image Found" 
                     Visible="false"></asp:Image><BR>
        </TD>
        </TR>
    </ContentTemplate>
</asp:UpdatePanel>

EDIT: my code-behind doing the update is here:

Private Sub myDropdown_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles myDropdown.SelectedIndexChanged
        If (myDropdown.SelectedValue <> "-1") Then
            imgThumbnail.ImageUrl = Application("AppPath") + "/Modules/Broadcaster/ImageGen.ashx?tag=" + myDropdown.SelectedValue
        Else
            imgThumbnail.ImageUrl = Application("AppPath") + "/Modules/Broadcaster/ImageGen.ashx?defaultTag=" + _modID.ToString()
        End If
End Sub

I can see the async postback happening in FireBug, but the image URL does not change. What am I doing wrong?


You're missing the code that's doing the update (the code that is called by the selected index changing within the dropdown); however, I'm going to venture a guess that your problem is being caused because you are loading the DDL through a control instead of programmatically.

The reason you may be running into this issue is because the page load function is called before the datasource controls are populated, which means that the DDL is not populated by the time you are looking for a value, thus your image is coming up with a blank.

Example:

 Dim sTemp As String = "images/myimagenumber" & myDropdown.SelectedIndex & ".jpg"

This will return "images/myimagenumber.jpg" as the value of the sTemp string because there is no value or index selected the the moment the page loads.

I suggest you load the values of the dropdownlist manually (programmatically) and then in the page_load subroutine make sure that it's only repopulating the dropdown when the page loads for the first time.

VB.Net Example:

 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
     If Page.IsPostback = False Then
          runDBLFillSubHere()
     End If

     'Run Rest of Code Here'
 Sub

I've run into this a couple times over the years and it always ends up being because the DDL isn't populated before I am accessing it.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜