简体   繁体   English

updatepanel中的图像导致整页刷新

[英]image in updatepanel causes whole page refresh

i have an image control inside update panel, in the page load i am setting its url. 我在更新面板中有一个图像控件,在页面加载我正在设置其URL。

code behind in page_load page_load中的代码

string url = "example.com";
Image1.ImageUrl = url;

inside updatepanel in aspx 在aspx中的updatepanel内部

<asp:Image ID="Image1" runat="server" CssClass="image" />

i also have couple of submit buttons inside the updatepanel, and i am updating some texboxes and labels on button clicks. 我还在更新面板内有几个提交按钮,我正在按钮点击更新一些texbox和标签。

however this causes whole page to refresh. 但是这会导致整个页面刷新。 (the scrollbar goes up.) (滚动条上升。)

if move the image outside of update panel this doesn't happen. 如果将图像移到更新面板之外,则不会发生这种情况。 the problem is the layout doesn't work at all if i remove the image outside of updatepanel. 问题是,如果我删除updatepanel之外的图像,布局根本不起作用。 can anyone help me with this? 谁能帮我这个? thanks. 谢谢。

UPDATE UPDATE

i have realized this only happens in Chrome. 我意识到这只发生在Chrome中。 Does anyone have an idea ? 有没有人有想法?

UPDATE 2 On that page I have resolved the issue by removing the img from the updatepanel. 更新2在该页面上,我通过从updatepanel中删除img解决了该问题。 It was hell to get the layout work, but it worked. 让布局工作真是太棒了,但它确实奏效了。

However I have another page, where I am adding new imgs to the updatepanel when a user clicks load more. 但是我有另一个页面,当用户点击加载更多时,我在updatepanel中添加新的imgs。 Same deal, and obviously i can't move image out of update panel this time. 同样的交易,显然我这次不能将图像移出更新面板。 Here is the code. 这是代码。

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

        <!--Recent Uploads-->
        <div class="uploads">
            <h2>Uploads</h2>
            <asp:UpdatePanel ID="RecentUpload" runat="server" UpdateMode="Conditional">
                <Triggers>
                    <asp:AsyncPostBackTrigger ControlID="loaduploads"/>
                </Triggers>
                <ContentTemplate>
                    <asp:Button CssClass="uploadButton" ID="loaduploads" runat="server" Text="Load More" OnClick="loaduploads_Click" />
                </ContentTemplate>
            </asp:UpdatePanel>
        </div>

Code behind (upload index is a session variable) 代码隐藏(上传索引是会话变量)

upload_index += 5;
        for (int i = 0; i < upload_index; i++)
        {
            try
            {
                SSImage img = images[i];
                HyperLink imglink = new HyperLink();
                imglink.NavigateUrl = "/Image.aspx?id=" + img.id;
                imglink.ImageUrl = "/ShowImage.ashx?imgid=" + img.id;
                imglink.ToolTip = img.title;
                imglink.CssClass = "imgupload";
                Control contentpanel = RecentUpload.ContentTemplateContainer;
                contentpanel.Controls.AddAt(contentpanel.Controls.Count - 2, imglink);
            }
            catch (ArgumentOutOfRangeException)
            {
                loaduploads.Visible = false;
                break;
            }
        }

UPDATE 3 更新3

the problem does not happen with static images, rather happens when i am trying to load from showimage.ashx . 静态图像不会发生问题,而是在我尝试从showimage.ashx加载时showimage.ashx here is the code. 这是代码。

<%@ WebHandler Language="C#" Class="ShowImage" %>

using System;
using System.Web;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public class ShowImage : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        SqlDataReader rdr = null;
        SqlConnection conn = new SqlConnection();
        SqlCommand cmd = new SqlCommand();

        try
        {
            string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            conn = new SqlConnection(connStr);

            cmd = new SqlCommand("SELECT Image_data FROM [Image_table] WHERE Image_id = " + context.Request.QueryString["imgID"], conn);
            conn.Open();
            Object result = cmd.ExecuteScalar();
            //if nothing found throw exception
            if (result == null)
            {
                throw new Exception();
            }
            rdr = cmd.ExecuteReader();
            while (rdr.Read())
            {
                context.Response.ContentType = "image/jpg";
                context.Response.BinaryWrite((byte[])rdr["Image_data"]);
            }

            if (rdr != null)
                rdr.Close();
        }
        catch
        {

        }
        finally
        {
            conn.Close();
            conn.Dispose();
        }

    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}

You should use Asynchronous post back trigger of update panel here is the example: 您应该使用更新面板的异步回发触发器,例如:

<asp:AsyncPostBackTrigger ControlID="" EventName=""/>

where 'controlID' is a id of element which can cause the postback and 'eventname' is the particular event that is fired by defined control to cause postback 其中'controlID'是元素的id,可以导致回发,'eventname'是由定义的控件触发的特定事件,导致回发

Add another update panel and put your image tag into this update panel and then set property "updatemode" to "always". 添加另一个更新面板并将您的图像标记放入此更新面板,然后将属性“updatemode”设置为“always”。

try this one it will solve your problem for sure. 尝试这个,它肯定会解决你的问题。 if doesn't let me know. 如果不让我知道。

In my opinion, 在我看来,

You should try to add controls to the body instead of updating the ContentTemplate of the UpdatePanel ie Create a Panel control in ContentTemplate and add everything in that and update anything in that Panel not the ContentTemplate . 您应该尝试向主体添加控件,而不是更新UpdatePanelContentTemplate ,即在ContentTemplate中创建Panel控件并添加其中的所有内容并更新该Panel任何内容而不是ContentTemplate

Check the logic and aspx syntax which shows to add controls to Panel which is contained in the UpdatePanel > ContentTemplate 检查逻辑和aspx语法,该语法显示向Panel添加控件,该控件包含在UpdatePanel > ContentTemplate

ASPX ASPX

    <asp:UpdatePanel ID="RecentUpload" runat="server" UpdateMode="Conditional">
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="loaduploads"/>
        </Triggers>
        <ContentTemplate>
            <asp:Panel ID="pnlMyDynamicContent" runat="server">
                 <asp:Button CssClass="uploadButton" ID="loaduploads" runat="server" Text="Load More" OnClick="loaduploads_Click" />
            </asp:Panel>
        </ContentTemplate>
    </asp:UpdatePanel>

Code Behind 代码背后

    ad_index += 5;
    for (int i = 0; i < upload_index; i++)
    {
        try
        {
            SSImage img = images[i];
            HyperLink imglink = new HyperLink();
            imglink.NavigateUrl = "/Image.aspx?id=" + img.id;
            imglink.ImageUrl = "/ShowImage.ashx?imgid=" + img.id;
            imglink.ToolTip = img.title;
            imglink.CssClass = "imgupload";

            // Commented old code
            //Control contentpanel = RecentUpload.ContentTemplateContainer;
            //contentpanel.Controls.AddAt(contentpanel.Controls.Count - 2, imglink);

            //Add controls to Panel instead of updating the ContentTemplate itself
            pnlMyDynamicContent.Controls.AddAt(pnlMyDynamicContent.Controls.Count - 2, imglink);
        }
        catch (ArgumentOutOfRangeException)
        {
            loaduploads.Visible = false;
            break;
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM