how can I correct my code?
-
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(); } }
-
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(); } }
-
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.
yes it is textbox2.i am sure.
-
yes it is textbox2.i am sure.
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
-
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
i try the code like you,but i get the same result,(qty is " ") where is my problem?
-
i try the code like you,but i get the same result,(qty is " ") where is my problem?
-
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.
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.
-
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.
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 variableresultvalues
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.