i use onKeyup do click button but it's do postback before make a event

Makki 21 Reputation points
2022-10-28T01:42:14.843+00:00
   <asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1" OnItemCommand="MyListView_ItemCommand">  

   <ItemTemplate>  

   <div id="item1" class="cart-item-container">  

   <div class="cart-item-main">  
   <div class="cart-item-info">  
   <ul class="cart-item-quantity">  
   <button class="B1" runat="server"  onclick="Unnamed_Click" style="display:none" >Add to Cart</button>  
   <asp:linkButton ID="imgDelete" AlternateText="Delete" runat="server"  
   CommandName="DeleteItem" CommandArgument='<%# Eval("Basket_Id")%>' ToolTip="Delete"  
   OnClientClick="return confirm('Are you sure you want to delete this Item?')"  
       CausesValidation="false" AutoPostBack ="true"><i class="fa-solid fa-trash"></i> </asp:linkButton>  
   <h3><%# Eval("MedicamentName") %> <span> X<%# Eval("Quantity") %></span></h3>   
   <li><p>Size : 5</p></li>  
   <li><p>Color: Red</p></li>  
   </ul>  

   <div class="cart-item-price">  
    <span class="left">Unit Price: SAR <%# Eval("Price") %></span>  
   <input id="Number1" class="test" runat="server" visible="false" Type="number" name="Number1" Value=<%# Eval("Medicament_Id") %>> </input>  
    <span class="right">Quantity: <input id="aQuantity" runat="server" class="test" name="aQuantity" Type="number" Value =<%# Eval("Quantity") %> onKeyup="{  
    document.querySelector('.B1').click();  

    }"></input></span>  
    <div class="clearfix"></div>  
   </div>  
   </div>  
   <div class="clearfix"></div>  

   </div>  
    </div>  
   </ItemTemplate>  
   </asp:ListView>  

-
<input id="aQuantity" runat="server" class="test" name="aQuantity" Type="number" Value =<%# Eval("Quantity") %> onKeyup="{
document.querySelector('.B1').click();

    }">  

This onKeyup when the input change should click

   <button class="B1" runat="server"  onclick="Unnamed_Click" style="display:none" >Add to Cart</button>  

but it's make refresh before do the event

c#

   protected void Page_Load(object sender, EventArgs e)  
           {  
               PEntitiesDB ctx = new PEntitiesDB();  
               if (Session["Users_Id"] == null)  
               {  
                   Response.Redirect("Login.aspx");  
                   return;  
               }  
               this.ListView1.DataBind();  
               aCount.InnerText = "My Shopping Cart (" + ListView1.Items.Count + ")";  
               long Users_Id = (long)Session["Users_Id"];  
               LPrice.InnerText = ctx.sp_GetTotlePrice(Users_Id).First().ToString();  
               LTotle.InnerText = LPrice.InnerText;  
           }  


           protected void MyListView_ItemCommand(object sender, ListViewCommandEventArgs e)  
           {  
               PEntitiesDB ctx = new PEntitiesDB();  
               if (e.CommandName == "DeleteItem")  
               {  
                   ctx.DelBasket(long.Parse(e.CommandArgument.ToString()));  
                   e.Item.Visible = false;  

               }  

           }  

           protected void Unnamed_Click(object sender, EventArgs e)  
           {  
               System.Threading.Thread.Sleep(5000);  
               long Users_Id = (long)Session["Users_Id"];  
               long Medicament_Id = 0;  
               long Quantity = 0;  
               PEntitiesDB ctx = new PEntitiesDB();  

               foreach (var i in ListView1.Items)  
               {  

                   HtmlInputGenericControl c = (HtmlInputGenericControl)i.FindControl("aQuantity");  
                   HtmlInputGenericControl h = (HtmlInputGenericControl)i.FindControl("Number1");  
                       if (long.Parse(c.Value) > 0)  
                       {  
                           Medicament_Id = long.Parse(h.Value);  
                           Quantity = long.Parse(c.Value);  
                               c.Value = "0";  
                               ctx.sp_SetInBasket(Quantity, Users_Id, Medicament_Id);  
                       }  

               }  
           }  
       }  
   }  

so how can help

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,254 questions
JavaScript API
JavaScript API
An Office service that supports add-ins to interact with objects in Office client applications.
867 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Lan Huang-MSFT 25,551 Reputation points Microsoft Vendor
    2022-10-28T09:13:32.047+00:00

    Hi @Makki ,
    Your purpose should be to use Unnamed_Click method, my suggestion is to use TextBox.TextChanged event and call Unnamed_Click directly.
    Data binding is placed in if (!IsPostBack){}

    <asp:TextBox ID="aQuantity"  Text=' < % # Ev al ("Quantity") %> ' TextMode="Number"  runat="server" AutoPostBack="true"  ontextchanged="Unnamed_Click"></asp:TextBox>  
    
      protected void Unnamed_Click(object sender, EventArgs e)  
            {  
                  
            }  
     protected void Page_Load(object sender, EventArgs e)  
            {  
                if (!IsPostBack)  
                {  
    

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments