做一个考试系统,客户要求要有填空题,实现的过程中可谓费尽周折,在这里说一说实现的过程。
1、出题的时候,将需要填空的部分用固定长度的下划线表示,保存到数据库中,字段名字为stnr
2、取出stnr,用分割函数以固定长度的下划线为分隔符,将除填空部分以外的字符串搞到一个数组里,然后循环取出这个数组里每一个值并上<input type=text id=>赋给字符串str,再用table.innerhtml=str,这样就将考题输出到表格里了,填空的部分被替换成文本框
———————xx.aspx.vb—————–
private sub create()
dim str as string
str = dv.item(0).item(“stnr”).tostring.trim
dim a() as string
a = split(str, “_____”, -1, comparemethod.text)
dim i as int16
dim str1 as string
for i = 0 to a.length – 2
if dvdab.count > 0 then
dim strdab as string
strdab = dvdab.item(0).item(“da”).tostring.trim
dim s() as string
s = split(strdab, “|”, -1, comparemethod.text)
str1 = str1 & a(i) & “<input type=text id=text” & i & ” style=border-top-style: none; border-bottom: green thin solid; border-right-style: none; border-left-style: none; height: 22px; width: 109px; background-color: transparent runat=server value=” & s(i) & “>”
else
str1 = str1 & a(i) & “<input type=text id=text” & i & ” style=border-top-style: none; border-bottom: green thin solid; border-right-style: none; border-left-style: none; height: 22px; width: 109px; background-color: transparent runat=server>”
end if
next
textcount记录了填空的个数,传递给js执行循环
textcount.value = a.length – 1
td2.innerhtml = str1 & a(a.length – 1)
end sub
3、接下来就是取这些文本框的值了,刚开始的时候尝试request.from()取不到值,用page.findcontrols也找不到这些文本框,之前我让他们都runat=server的,很是奇怪。最后想到了用js,js循环取值后将所有文本框中的值加个分隔符并到一个hidden控建里,然后由asp.net来跟标准答案作比较,评分
——————xx.aspx—————-
function aa(){
var count=document.all("textcount").value;
var hiddentext = "";
for(i=0;i<count;i++){
hiddentext+=“|”+document.all(“text“+i).value;
}
document.all(“hiddentext“).value=hiddentext;
}
————————xx.aspx.vb——————————
public sub point()
dim i() as string
dim a as int16
da = da.remove(0, 1)
i = split(da, ",", -1, comparemethod.text)
dim ii() as string
ii = split(dvtk.item(0).item("stda2"), ",", -1, comparemethod.text)
dim fs2 as single
for a = 0 to i.length - 1
if i(a) = ii(a) then
fs2 = dvscsj.item(0).item("xtfs") / ii.length
else
fs2 = 0
end if
fs += fs2
next
end sub
5、再补充一点,提交之后,文本框中的内容会被清空,但是数据已经却提交到服务器,我就重新执行一次create(),至此,问题全部解决。
代码质量比较差,欢迎指教!
