0

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?

Usman Farooq
  • 109
  • 3
  • 11
  • you need to use `OnClientClick` of that button and write message in javascript. For example, http://stackoverflow.com/questions/14058116/confirm-postback-onclientclick-button-asp-net – techspider Jun 15 '16 at 17:50

1 Answers1

0

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