簡體   English   中英

圖像按鈕在數據列表中無法正常工作

[英]image button doesn't work correctly in datalist

我在數據列表中有一個圖像按鈕可以打開一個新頁面來發送郵件,但是當我第一次單擊它時,它不起作用,當第二次單擊時它可以工作,我使用了數據列表中的更新面板。 這是代碼

<asp:UpdatePanel ID="up1" runat="server">
    <ContentTemplate>          
        <asp:ImageButton ID="ImgBtnMail" runat="server" ImageUrl="../../../../Icons/Send-Email.jpg" 
             Width="22" Height="18" CommandName="DoMail" 
             CommandArgument='<%#DataBinder.Eval(((System.Web.UI.WebControls.DataListItem)(Container)).DataItem, "AdID")%>' />
    </ContentTemplate>
</asp:UpdatePanel>

以及后面的代碼:

 protected void DLAds_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if(e.CommandName=="DoMail")
        {
            string TargetPage = "window.open('SendMail.aspx?',null,'height=150, width=150,left=500 ,top=300 ,status= no, resizable= no, scrollbars=no, toolbar=no,location=no,menubar=no ');";
            ImageButton MailBtn = (ImageButton)e.Item.FindControl("ImgBtnMail");
            MailBtn.Attributes.Add("onclick", TargetPage);
        }
    }

使用您發布的示例代碼,最好將OnCommand事件處理程序直接分配給按鈕。

<asp:ImageButton ID="ImageButton1" runat="server" OnCommand="ImageButton1_Command" ...>

在您的代碼中:

protected void ImageButton1_Command(object sender, EventArgs e)
{
    string targetPage = "..."
    ((ImageButton)sender).Attributes.Add("onclick", targetPage);
}

在UpdatePanel中設置UpdateMode =“ Conditional”,並在CodeBehind中更新UpdatePanel,如下所示:

<asp:UpdatePanel ID="up1" runat="server" UpdateMode="Conditional">

protected void ImageButton1_Command(object sender, EventArgs e)
{
   up1.Update();
   string targetPage = "..."
   ((ImageButton)sender).Attributes.Add("onclick", targetPage);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM