Webform中的一个panel上动态生成的多个控件(包括label和radiobuttonlist),按一下button按钮,读取选项信息输入SQL表。

falcon99 2003-10-19 03:53:46
现在的问题是在按过button按钮后,panel上的控件被清空了?
...全文
74 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
良朋 2003-10-24
  • 打赏
  • 举报
回复
我没时间仔细分析你的代码,只有一些简单的建议
1.把生成控件即你的题目的过程写入一个sub过程, form load 时调用一次。
2.click按钮提交后,清空所有内容,再调用这个过程重新生成各控件。

falcon99 2003-10-22
  • 打赏
  • 举报
回复
to itleon(良朋):

你有什么好的建议,请赐教!
falcon99 2003-10-22
  • 打赏
  • 举报
回复
page_load中调用question_paper过程的语句如下(content_count是一个Int16的值):
运行后panel上正确显示各控件,但上面所说的2个问题存在

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Page.IsPostBack = False Then
.
.
.
content_count在此被赋值
End If

If viewstate("edit") = False Then
If content_count > 7 Then
question_paper(1, 7)
Else
question_paper(1, content_count)
End If
End If
End Sub
falcon99 2003-10-22
  • 打赏
  • 举报
回复
下面是button1过程,问题1,question_paper中的i值没有传递到button1过程中;问题2,按过button1按钮后,panel上的控件被清空了(button1过程中没有清除控件的语句)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim RBLcount As Int16
'检查每一题是否做完
' i = Session("count")
'Response.Write("<script>alert('" & Session("count") & "')</script>")
Response.Write("<script>alert('" & Str(i) & "')</script>")
j = i Mod 7
If j = 0 Then
j = i - 6
Else
j = Int(i / 7) * 7 + 1
End If
Response.Write("<script>alert('" & Str(j) & "')</script>")
For ii = j To i
m = ii Mod 7
If m = 0 Then
m = 7
End If
'Response.Write("<script>alert('" & Str(m) & "')</script>")
'Exit Sub
dyRadioButton = Panel1.FindControl("RadioButtonList" & m)
If dyRadioButton.SelectedIndex = -1 Then
Response.Write("<script>alert('所有题目都必须做!')</script>")
Exit Sub
End If
Next
....

End Sub
良朋 2003-10-20
  • 打赏
  • 举报
回复
你的第二条语句
Panel1.Controls.Clear()
就是用来清空Panel1中的所有控件的。这有什么奇怪,

建议你把动态生成控件写成一个过程,通过按钮调用清空+生成
falcon99 2003-10-20
  • 打赏
  • 举报
回复
各位大虾,我关于生成动态控件的代码如下,从page-load和button过程中调用
Public Sub question_paper(ByVal n As Int16, ByVal m As Int16)
Panel1.Controls.Clear()
'在panel1上写题目和选项,每页7题
myConn.Open()
For i = n To n + m - 1
m = i Mod 7
If m = 0 Then
m = 7
End If
dylabel = New Label()
dylabel.ID = "Label" & (m + 7)
dylabel.Text = Str(i) + ". " + content(i, 0)
ansno = Trim(content(i, 1))
Panel1.Controls.Add(dylabel)
dylabel.Dispose()

Dim selectstr_ans As String = "select ans_name,ans_mark from ans where ans_no ='" & ansno & "' order by ans_mark desc"
Dim da_ans As New SqlDataAdapter(selectstr_ans, myConn)
Dim ds_ans As New DataSet()
Dim dw_a As DataRow

da_ans.Fill(ds_ans, "ans")
'ReDim ans(ds_ans.Tables("ans").Rows.Count)

dyRadioButton = New RadioButtonList()
dyRadioButton.ID = "RadioButtonList" & m
' Response.Write("<script>alert('" & Str(m) & "')</script>")
j = 0
For Each dw_a In ds_ans.Tables("ans").Rows
dyRadioButton.Items.Add(dw_a.Item(0))
content(i, j + 2) = dw_a.Item(0)
ansmark(i, j) = dw_a.Item(1)
j += 1
Next
Panel1.Controls.Add(dyRadioButton)
dyRadioButton.RepeatDirection = RepeatDirection.Horizontal
dyRadioButton.Width = Panel1.Width
dyRadioButton.Height.Pixel(30)
dyRadioButton.SelectedIndex = -1
dyRadioButton.Dispose()
ds_ans.Dispose()

Next
i -= 1
myConn.Close()
myConn.Dispose()

End Sub
良朋 2003-10-19
  • 打赏
  • 举报
回复
你用
panel.add(控件)
生成的控件不会清掉啊,还可以用Addhandle 指向你自定义的事件,非常方便
2002pine 2003-10-19
  • 打赏
  • 举报
回复
动态生成的按钮是无法保持状态的,
你必须在button时再生成一次控件。
或者在page_load
{
生成动态控件,
}

控件上面的数值你可以用viewstate保存。

16,549

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧