2012-03-31 71 views
0

我的问题的快速总结:我想显示用户填写的字段数。填写计数字段

我是学习ASP的新手,我看过并没有找到解决方案。我生成了一个简单的示例页面,与我需要帮助的内容相比很简单,但是同样的想法。

当用户转到第一页时,他显然会得到三个文本框。 当他提交表格时,他会显示他提交的信息,填写的字段数量。我正在尝试遍历每个字段,如果数字大于0,则将其添加到名为tt的计数器。

通过2显示我的循环,而不是给我tt的价值。我尝试使用response.write进行循环,但没有奏效。

<html> 
<body> 

<% 
sub pass1 
%> 
Pass 1 <P> 
<form action="count_p.asp" method = "post"> 
<input type="text" name="t1"><BR> 
<input type="text" name="t2" ><BR> 
<input type="text" name="t3"><BR> 
<input type="hidden" name="token" value="2"> 
<input type="submit" value="submit query"> 




<% 
end sub 

sub pass2 
    response.write "<P>Pass 2 tokenvalue="+cstr(tokenvalue) 

t1=request.form("t1") 
t2=request.form("t2") 
t3=request.form("t3") 



response.write "<P>t4=" + t1 
response.write "<P>t4=" +t2 
response.write "<P>t4=" +t3 
%> 

tt=0 
for i=1 to 3 
    if t + cstr(i) > 0 then 
    tt=tt+1 
    end if 
then 


response.write "<P>Fields filled = " + tt 



<% 
end sub 



tokenvalue=request.form("token") 
select case tokenvalue 
case "" 
    call pass1 
case "2" 
    call pass2 
case "3" 
    call pass3 

end select 
%> 


</body> 
</head> 
+0

是你的问题在传统的ASP或ASP.net?代码看起来像ASP。 – 2012-03-31 02:08:18

+0

看起来可能是使用VB的MVC2? – TGH 2012-03-31 02:13:38

+0

.asp,很抱歉 – user1084561 2012-03-31 02:19:03

回答

0

您不能使用动态变量名称。他们不受支持。试试这个:

'Here we are splitting all the form values 
'into an array. Your values will come in 
'looking something like this: 
' 
' t1=4&t2=323&t3=3 
' 
'after we split them, you'll have 3 sets 
'of values that look like: 
' 
' aFormNamesAndValues(0) = "t1=4" 
' aFormNamesAndValues(1) = "t2=323" 
' aFormNamesAndValues(2) = "t3=3" 

aFormNamesAndValues = Split(Request.Form,"&") 

tt=0 
for i=0 to 2 
    'Ok, splitting once again, this time on the 
    'equals character. Now we will have an array 
    'with 2 values, the name of the form field 
    'and the value it holds, we can check each 
    'value and perform some logic on it: 
    aNameAndAValue = Split(aFormNamesAndValues(i),"=") 

    if aNameAndAValue(0) = "t" & (i+1) then 
     if aNameAndAValue(1) > 0 then 
      tt=tt+1 
     end if 
    end if 
then 
0

可以使用Eval方法本 - 通常它是令人难以接受的,但在这种情况下,它是有效的用法:

tt=0 
For i=1 to 3 
    curValue = Eval("t" & i) 
    If IsNumeric(curValue) Then 
     If CLng(curValue)>0 Then 
      tt = tt + 1 
     End If 
    End If 
Next 

正如你也可以看到,您需要将值转换使用CLng编号以进行适当的比较。