Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
1.20/5 (2 votes)
See more:
Hi Everyone,

I have a textbox 'txtQuantity' and a button 'ADD'. I have written the code for generating dynamic textboxes inside the txtQuantity_TextChanged event. i want to generate the textboxes automatically when i write some text or quantity inside the textbox. after filling all the dynamic textboxes i want to add that values in database.
But my problem is on clicking the button 'ADD' the dynamic textboxes are generating as well as the button_Click event is working inserting null value into database. But i want to valid the textchanged event first.How will i do it ? kindly help.

C#
protected void txtQuantity_TextChanged(object sender, EventArgs e)
 {
     con.Open();
     using (SqlCommand comm = new SqlCommand("select [Serial_Number_Required] from Products where Product=@Product ", con))
     {
         //string Role = (string)(Session["Role"]);
         // 2. define parameters used in command object
         SqlParameter para = null;
         para = new SqlParameter();
         para.ParameterName = "@Product";
         para.Value = ddlProductName.SelectedValue;
         comm.Parameters.Add(para);

         //Pass @Pages parameter

         SqlDataReader reade = comm.ExecuteReader();
         while (reade.Read())
         {
             Session["Serial_Number_Required"] = Convert.ToString(reade["Serial_Number_Required"]);
         }
         con.Close();
         //Button Add = (Button)PreviousPage.FindControl("Button2");
         if (Session["Serial_Number_Required"].ToString() == "Y")
         {

             fun1();
         }


     }

         }

XML
<asp:TextBox ID="txtQuantity" runat="server"  Width="150px" 
                    Height="16px" BackColor="White" AutoPostBack="True"  ontextchanged="txtQuantity_TextChanged" 
                    ></asp:TextBox>


XML
<asp:Button ID="Button2" runat="server" BackColor="#F4F4F4" CssClass="style4" Text="Add to GatePass"
                    Font-Size="8.5pt"   onchange="txtQuantity.TextChanged" Height="22px" onclick="Button2_Click"   
                                OnClientClick="javascript:return ValidateField()" Font-Bold="False" 
                    Width="120px" CausesValidation="False" >
Posted
Updated 15-Oct-14 1:43am
v4
Comments
Sergey Alexandrovich Kryukov 15-Oct-14 4:27am    
Not clear. There is no such concept as "call an event". You cannot even invoke an event anywhere except the declaring class. As the class is from the .NET FCL, you cannot invoke it. This is such a full-proof feature of .NET — this is never really needed, and .NET protects you from doing so. If you need an advice, explain what did you want to achieve by "calling an event". Such problems are easy to solve.
—SA
Member 10578683 15-Oct-14 4:32am    
Clearly i want to fire textchanged event first before adding
Sinisa Hajnal 15-Oct-14 5:12am    
Not clear at all.

You should have boolean ValidateInput function which you will call wherever you need it - including your text-changed event. Not use event as validating function. You turned it around.
Sergey Alexandrovich Kryukov 15-Oct-14 11:33am    
You cannot "fire" this event. This is a foolproof feature. So, please explain your purpose.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900