简体   繁体   中英

JavaScript alert from button inside asp.net DataList

how to show a JavaScript alert or bootstrap modal message from the button click event inside the Asp.Net DataList. Here is my DataList:

     <asp:DataList ID="dListProduct" runat="server" RepeatColumns="4" OnItemCommand="dListProduct_ItemCommand" AutoPostBack="true">
    <ItemTemplate>
        <div>
            <table class="table-responsive" border="1">
                <tr>
                     <td>
                          <asp:Label runat="server" Text="Id" ></asp:Label><asp:Label ID="lblPId" runat="server" Text='<%# Eval("Product_Id")%>' Visible="true"></asp:Label>
                    </td>
                </tr>
                <tr>
                     <td>
                         <asp:Button id="btnAddtoCart" runat="server" Text="Add to Cart" CommandName="save" CommandArgument="save" OnClick="btnAddtoCart_Click"/>
                    </td>
                </tr>
            </table>
        </div>
    </ItemTemplate>
</asp:DataList>

and here is the code behind:

 protected void dListProduct_ItemCommand(object source, DataListCommandEventArgs e)
        {
            if (e.CommandName == "save")
            {
                Label Id = (Label)e.Item.FindControl("lblPId");
                Label Name = (Label)e.Item.FindControl("lblProductName");
                Label Stock = (Label)e.Item.FindControl("lblStock");
                Label Price = (Label)e.Item.FindControl("lblPrice");
                if (Stock.Text.Equals("0"))
                    return;

                bool check = false;

                foreach (Cart c in GenericCart.cart)
                {
                    if (c.ProductId == Convert.ToInt64(Id.Text))
                    {
                        c.Qty += 1;
                        c.Total = (c.Price * c.Qty);
                        check = true;
                        Response.Write("<script>alert('Hello');</script>");
                        break;
                    }
                }
                if (!check)
                {
                    Cart c = new Cart();
                    c.ProductId = Convert.ToInt64(Id.Text);
                    c.ProductName = Name.Text;
                    c.Qty = 1;
                    c.Price = (float)Convert.ToDecimal(Price.Text);
                    c.Total = (float)Convert.ToDecimal(Price.Text);
                    GenericCart.cart.Add(c);
                    Response.Write("<script>alert('Hello');</script>");
                }              
            }
        }

but this script message never get displayed. Can anyone help?

You can either use OnClientClick attribute on your button if you want the alert to fire before the codebehind or use Page.RegisterStartupScript in the codebehind to register a javascript function to execute after the postback is completed.

MSDN article on usage of RegisterStartupScript

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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