thirdDeveloper
thirdDeveloper

Reputation: 895

why the image isn't shown when it's in an UpdatePanel?

I have an image that placed in UpdatePanel. I set it's ImageUrl in button_click event.the image are in App_Data/imagesDirectory. Why isn't the image shown in the web page?

<asp:Panel ID="Panel1" runat="server" style="direction: ltr">
<asp:ListBox ID="photosListBox" runat="server" Rows="1"></asp:ListBox>
<asp:Button ID="selectButton" runat="server" Text="select" 
    onclick="selectButton_Click" />
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<br />
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
    <ContentTemplate>
        <asp:Image ID="ph" runat="server" />
        <br />
        <br />
        <asp:Button ID="submit" runat="server" onclick="submit_Click" 
            Text="submit" />
        <br />
        <br />
    </ContentTemplate>
</asp:UpdatePanel>
</asp:Panel>

im just set ImageUrl property in it that related the image control.however, the button code:

UpdatePanel2.Visible = true;
        submit.Visible = true;
        photosListBox.Visible = false;
        selectButton.Visible = false;

        Users sentUser = (Users)Session["user"];
        Gallery sentGallery = (Gallery)Session["gallery"];
        string selectedName = photosListBox.SelectedItem.ToString();
        int selectedId = Convert.ToInt32(photosListBox.SelectedItem.Value);

        ModelContainer ml = new ModelContainer();
        Users u = ml.UsersSet.Where(t => t.Username == sentUser.Username).First();
        Gallery g = u.Gallery.Where(t => t.Name == sentGallery.Name && t.Id == sentGallery.Id).First();
        Photo p = g.Photo.Where(t => t.Name == selectedName && t.Id == selectedId).First();

        ph.ImageUrl = MapPath(p.PhotoAdd);
        nameTextBox.Text = p.Name;
        descriptionTextBox.Text = p.Description;
        uploadDateTimeLabel.Text = p.UploadDateTime.ToString();

i have also set ImageUrl attribute in PreRender event of the page.but it is'nt work:

protected void Page_PreRender(object sender, EventArgs e) 
    {
        ph.ImageUrl = imageU;
    }

imageU is a protected field of the page class

Upvotes: 0

Views: 213

Answers (2)

JBrooks
JBrooks

Reputation: 10013

Your photosListBox is not in the update panel so the selected value is not sent back to the server when submit_Click() is executing.

Upvotes: 0

Aghilas Yakoub
Aghilas Yakoub

Reputation: 28970

You must define ImageUrl in the PreRender event of your page

1 Find our data in event

2 Save data in variable of your page

3 Set attribute of Image on PreRender

Upvotes: 1

Related Questions