Darren Wood
Darren Wood

Reputation: 1528

HiddenField has correct Value but displaying the wrong value in C# ASP.NET

I have the following code in a button click method that goes through the rows of a GridView. ServiceFormQuestionTypeIDHF is a HiddenField.

The first time through the foreach statement the value of sfqtIdINT is 10 and the value of sfqtIdSTR is "10" yet the if statement is true. Now the value I am expecting for the HiddenField.ToInt() is 1.

I can confirm that the rest of the foreach statement and the rest of the if statements display the wrong value in sfqtIdSTR and sfqtIdINT but the behavior of the if statement is the value I am expecting from that row in the GridView. In other words the HiddenField has the right value but displays the wrong value.

I can see no reason for this. Can anyone provide a reason for this?

 foreach (TableRow tr in FormGV.Rows)
 {
     HiddenField ServiceFormQuestionTypeIDHF = (HiddenField) tr.FindControl("ServiceFormQuestionTypeIDHF");
     int sfqtIdINT = ServiceFormQuestionTypeIDHF.Value.ToInt();
     string sfqtIdSTR = ServiceFormQuestionTypeIDHF.Value;                       
     if ((ServiceFormQuestionTypeIDHF.Value.ToInt() == 1 )||
                        (ServiceFormQuestionTypeIDHF.Value.ToInt() == 2))
     { .......
       .......
     }

Upvotes: 0

Views: 78

Answers (1)

Naveen Gogineni
Naveen Gogineni

Reputation: 306

Try using GridViewRow instead of TableRow in foreach.

Upvotes: 1

Related Questions