Mounir
Mounir

Reputation: 291

image button doesn't work correctly in datalist

i have an image button in datalist to open a new page to send mail, but when i click on it for the first time , it doesn't work , it works when click for the second time , i use update panel inside datalist . here's the code

<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>

and the code behind :

 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);
        }
    }

Upvotes: 1

Views: 1190

Answers (2)

thevan
thevan

Reputation: 10354

Set UpdateMode="Conditional" in the UpdatePanel and Update your UpdatePanel in the CodeBehind like below:

<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);
}

Upvotes: 0

James Johnson
James Johnson

Reputation: 46047

With the example code you've posted, you'd be better off assigning the OnCommand event handler directly to the button.

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

And in your code:

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

Upvotes: 1

Related Questions