Imagebutton onclick 在 updatepanel 中不起作用,我怎样才能使它工作?

发布于 2025-01-05 00:53:39 字数 1175 浏览 6 评论 0原文

 <asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" ChildrenAsTriggers="true"   
 runat="server">
  <ContentTemplate> 
     <asp:ImageButton ID="imagebutton1" runat="server" ImageUrl="~/image.jpg" Width="125" 
     Height="125"  onclick="imagebutton1_Click" />
     <asp:Timer ID="timer1" runat="server" Interval="10000" ontick="timer1_Tick" />
     <asp:Label ID="label1" Visible="false" runat="server" Text="" />
  </ContentTemplate>
</asp:UpdatePanel>

如果我不使用更新面板,整个页面都会刷新。

protected void imagebutton1_Click(object sender, ImageClickEventArgs e){
    string link = label1.Text;
    Page.ClientScript.RegisterStartupScript(this.GetType(), "OPEN", "window.open(" + link +   
    ",'mywindow','width=200,height=200,');", true);
}

这是计时器处理程序中的变量“link”

Random r = new Random();

if (datatable1.Rows.Count > 0)
{
    int randomnumber = r.Next(0, i);
    DataRow datarow1= datatable1.Rows[randomnumber ];
    imagebutton1.ImageUrl = (String)datarow1["image"];
    imagebutton1.DataBind();
    label1.Text = (String)datarow1["Link"];
}
 <asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" ChildrenAsTriggers="true"   
 runat="server">
  <ContentTemplate> 
     <asp:ImageButton ID="imagebutton1" runat="server" ImageUrl="~/image.jpg" Width="125" 
     Height="125"  onclick="imagebutton1_Click" />
     <asp:Timer ID="timer1" runat="server" Interval="10000" ontick="timer1_Tick" />
     <asp:Label ID="label1" Visible="false" runat="server" Text="" />
  </ContentTemplate>
</asp:UpdatePanel>

If I don't use an update panel the whole page refreshes.

protected void imagebutton1_Click(object sender, ImageClickEventArgs e){
    string link = label1.Text;
    Page.ClientScript.RegisterStartupScript(this.GetType(), "OPEN", "window.open(" + link +   
    ",'mywindow','width=200,height=200,');", true);
}

This is the variable "link" here in the timer handler

Random r = new Random();

if (datatable1.Rows.Count > 0)
{
    int randomnumber = r.Next(0, i);
    DataRow datarow1= datatable1.Rows[randomnumber ];
    imagebutton1.ImageUrl = (String)datarow1["image"];
    imagebutton1.DataBind();
    label1.Text = (String)datarow1["Link"];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

陌若浮生 2025-01-12 00:53:39

我必须在更新面板中添加回发触发器,如下所示才能使其工作。

<Triggers>
<asp:PostBackTrigger  ControlID="ImageButton"/>
</Triggers>

I had to just add postback triggers as shown below in the updatepanel to make it work.

<Triggers>
<asp:PostBackTrigger  ControlID="ImageButton"/>
</Triggers>
机场等船 2025-01-12 00:53:39

很难说,因为您并没有真正包含问题,但如果您希望在 UpdatePanel 刷新时运行一段客户端代码,请使用客户端 pageLoad() 方法

此外,您正在使用服务器端事件来执行客户端操作。请改用 OnClientClick 处理程序:

<asp:ImageButton ID="imagebutton1" runat="server" ImageUrl="~/image.jpg" Width="125" 
    Height="125"  OnClienClick="return openPopUp(this.href);" />

function openPopUp(link)
{
   window.open(this.href,'mywindow','width=200,height=200,');
   return false;
}

It's hard to say since you didn't really include a question, but if you want a piece of client side code to run when an UpdatePanel refreshes, use the client side pageLoad() method.

Also, you are using a server side event to do a client side operation. Use the OnClientClick handler instead:

<asp:ImageButton ID="imagebutton1" runat="server" ImageUrl="~/image.jpg" Width="125" 
    Height="125"  OnClienClick="return openPopUp(this.href);" />

function openPopUp(link)
{
   window.open(this.href,'mywindow','width=200,height=200,');
   return false;
}
-黛色若梦 2025-01-12 00:53:39

尝试更改为 UpdateMode="Always"

Try changing to UpdateMode="Always"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文