在updatepanel里放置一个imagebutton用来显示登陆窗口的校验码,如果看不清可以点击按钮会刷新图片进行更换。
单问题是第一次载入页面的情况下点击imagebutton并不会刷新图片,只要在点击updatepanel以外的按钮,页面提交一次后才好用。请问有什么办法。

解决方案 »

  1.   

    为何一定要用updatePanel?
    直接用ajax不行吗?
      

  2.   

    在Page_Load事件调用imagebutton的click函数
    if (!IsPostBack) imagebutton_Click(null,null);另外一种方法是,不用imagebutton,在update panel里放一个Image控件,在panel外放一个button,点button就刷新image
      

  3.   

    那是因为点击updatepanel里面的图片没有出发事件,设置它的trigger属性呢?
      

  4.   

    updatepanel里放置一个imagebutton?别放里面不就完了吗
      

  5.   

    显示登陆窗口的校验码根本不需要ImageButton,用Image即可<head runat="server">
        <script type="text/javascript" language="javascript">
    //重新获取验证字符
    function changeImage()
    {
        //单击触发图片重载事件,完成图片验证码的更换
    document.getElementById("imgRandom").src = document.getElementById("imgRandom").src + '?';
    }
    </script>
    </head>
    <body>
    <form id="form1" runat="server">
    <img id="imgRandom" alt="验证码" src="RandomImage.aspx" />
    <a href="javascript:changeImage();">看不清?</a>
        </form>
    </body>
      

  6.   

    按照楼上的已经完成。非常感谢,也谢谢其他朋友。但我不明白的的是问什么只有在页面发生POSTBACK后UPdatePANEL里的能产生异步。而第一次也在页面则无效?
      

  7.   

    那是因为你把imagebutton放在了updatepanel里面,印象中,默认情况下updatepanel会拦截放在里面控件的事件,有一个属性可以改变这种行为
      

  8.   

    是啊,Ajax并不是用得越多越好滴
      

  9.   

    用updatepanel里面有个方法可以达到局部刷新啊
      

  10.   

    我写了一个demo,你可以对照一下程序逻辑设计。
    <%@ Page Language="C#" %><script runat="server"> 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                SetupImageButton1();
        }    private string[] _imgs = new string[]{  
            "http://tbn0.google.cn/images?q=tbn:sF2evLSXhP4fiM:",
            "http://tbn0.google.cn/images?q=tbn:m_KwmPo5IBPwQM:",
            "http://tbn0.google.cn/images?q=tbn:tViPPpsM_7Vf3M:",
            "http://tbn0.google.cn/images?q=tbn:75T9RItC-dVhRM:",
            "http://tbn0.google.cn/images?q=tbn:UtnLnGedopRdTM:",
            "http://tbn0.google.cn/images?q=tbn:-Sp9Uli8w9seHM:"
        };
        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            SetupImageButton1();
        }    static Random Rnd = new Random();    private void SetupImageButton1()
        {
            if (_imgs.Length <= 1)
                throw new NotSupportedException();        string newUrl;
            do
                newUrl = _imgs[Rnd.Next(_imgs.Length)];
            while (newUrl == this.ImageButton1.ImageUrl);   //避免用户切换图片时由于设置相同的图片引起“图片未切换”错觉
            this.Label2.Text = newUrl;
            this.ImageButton1.ImageUrl = newUrl;
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            this.Label1.Text = "您选择了:" + this.ImageButton1.ImageUrl;
        }
    </script><!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>测试通过UpdatePanel来更新ImageButton的图片</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server" />
        <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
            <ContentTemplate>
                <asp:ImageButton ID="ImageButton1" runat="server" OnClick="ImageButton1_Click" ToolTip="点击图片可以切换"
                    Height="100px" />
                url=
                <asp:Label ID="Label2" runat="server" Text="Label" EnableViewState="false"></asp:Label>
            </ContentTemplate>
        </asp:UpdatePanel>
        <asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
            <ContentTemplate>
                <asp:Button ID="Button1" runat="server" Text="在另一个UpdatePanel中测试ImageButton有没有保留状态"
                    OnClick="Button1_Click" />
                <asp:Label ID="Label1" runat="server" EnableViewState="False"></asp:Label>
            </ContentTemplate>
        </asp:UpdatePanel>
        </form>
    </body>
    </html>
      

  11.   

    为 _imgs 设置值的代码我使用了 .net 3.0的语法,如果你不支持3.0,自己修改为较低版本的语法即可。