Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. how can I correct my code?

how can I correct my code?

Scheduled Pinned Locked Moved ASP.NET
questiondebugging
8 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    strawberrysh
    wrote on last edited by
    #1

    I write this code for showing totalprice in a gridview footer.when this page loaded user input his quantity on textboxes and press a button,after that must see the total price in gridview footer.here is my click event code:(but when I input something in m textboxes and press the button the total price in footer is 0)I used breakpoint and saw the qty is "".how should I correct my code?

    protected void Button1_Click(object sender, EventArgs e)
    {
    if (GridView1.Rows.Count > 0)
    {
    for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
    {
    TextBox qty = (TextBox)GridView1.Rows[i].FindControl("textbox2");
    Label pr = (Label)GridView1.Rows[i].FindControl("label3");
    float total = 0;
    if (!string.IsNullOrEmpty(qty.Text))
    {
    float qty1 = float.Parse(qty.Text);

                    float pr1 = float.Parse(pr.Text);
                    total = total + (qty1 \* pr1);
                }
                Label t = (Label)GridView1.FooterRow.FindControl("label3");
                t.Text = total.ToString();
            }
    
        }
    
    B 1 Reply Last reply
    0
    • S strawberrysh

      I write this code for showing totalprice in a gridview footer.when this page loaded user input his quantity on textboxes and press a button,after that must see the total price in gridview footer.here is my click event code:(but when I input something in m textboxes and press the button the total price in footer is 0)I used breakpoint and saw the qty is "".how should I correct my code?

      protected void Button1_Click(object sender, EventArgs e)
      {
      if (GridView1.Rows.Count > 0)
      {
      for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
      {
      TextBox qty = (TextBox)GridView1.Rows[i].FindControl("textbox2");
      Label pr = (Label)GridView1.Rows[i].FindControl("label3");
      float total = 0;
      if (!string.IsNullOrEmpty(qty.Text))
      {
      float qty1 = float.Parse(qty.Text);

                      float pr1 = float.Parse(pr.Text);
                      total = total + (qty1 \* pr1);
                  }
                  Label t = (Label)GridView1.FooterRow.FindControl("label3");
                  t.Text = total.ToString();
              }
      
          }
      
      B Offline
      B Offline
      Blue_Boy
      wrote on last edited by
      #2

      Are you sure that exists control with name textbox2 in every row of gridview?


      I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post.

      S 1 Reply Last reply
      0
      • B Blue_Boy

        Are you sure that exists control with name textbox2 in every row of gridview?


        I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post.

        S Offline
        S Offline
        strawberrysh
        wrote on last edited by
        #3

        yes it is textbox2.i am sure.

        B 1 Reply Last reply
        0
        • S strawberrysh

          yes it is textbox2.i am sure.

          B Offline
          B Offline
          Blue_Boy
          wrote on last edited by
          #4

          Did you try to specify cell,like this TextBox qty = (TextBox)GridView1.Rows[i].Cells[1].FindControl("textbox2"); Instead index 1 of cell,set cell index which have value of textbox2.


          I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post.

          modified on Thursday, September 4, 2008 7:50 PM

          S 1 Reply Last reply
          0
          • B Blue_Boy

            Did you try to specify cell,like this TextBox qty = (TextBox)GridView1.Rows[i].Cells[1].FindControl("textbox2"); Instead index 1 of cell,set cell index which have value of textbox2.


            I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post.

            modified on Thursday, September 4, 2008 7:50 PM

            S Offline
            S Offline
            strawberrysh
            wrote on last edited by
            #5

            i try the code like you,but i get the same result,(qty is " ") where is my problem?

            B 1 Reply Last reply
            0
            • S strawberrysh

              i try the code like you,but i get the same result,(qty is " ") where is my problem?

              B Offline
              B Offline
              Blue_Boy
              wrote on last edited by
              #6

              Is textbox2 within template item? Does textbox2 have values after Gridview1 is populated with data?


              I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post.

              S 1 Reply Last reply
              0
              • B Blue_Boy

                Is textbox2 within template item? Does textbox2 have values after Gridview1 is populated with data?


                I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post.

                S Offline
                S Offline
                strawberrysh
                wrote on last edited by
                #7

                it is in template item.I couldn't understand your meaning in question 2? I used breakpoint but in qty is " ",and the data I intered in textbox wasn't there.

                B 1 Reply Last reply
                0
                • S strawberrysh

                  it is in template item.I couldn't understand your meaning in question 2? I used breakpoint but in qty is " ",and the data I intered in textbox wasn't there.

                  B Offline
                  B Offline
                  Blue_Boy
                  wrote on last edited by
                  #8

                  I have make a sample based on your code: string[] values ={"zero", "one", "two", "three" ,"four"}; GridView1.DataSource = values; GridView1.DataBind(); /*TextBox1 is within ItemTemplate of Gridview1*/ //add values on textbox1 for (int i = 0; i <= GridView1.Rows.Count - 1; i++) { TextBox qty = (TextBox)GridView1.Rows[i].FindControl("Textbox1"); qty.Text = i.ToString(); } string resultvalues = ""; //read values from textbox1 for (int i = 0; i <= GridView1.Rows.Count - 1; i++) { TextBox qty = (TextBox)GridView1.Rows[i].FindControl("Textbox1"); resultvalues += qty.Text + ";"; } and result of variable resultvalues is:0;1;2;3;4;


                  I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups