gpt4 book ai didi

asp.net - 如何使用 VB 在 asp.net 3.5 中引用动态创建的控件

转载 作者:行者123 更新时间:2023-12-04 06:31:26 24 4
gpt4 key购买 nike

我正在编写一个将控件动态插入到网络表单的程序。根据变量,我添加一个文本框、一组单选按钮或一组复选框。后来,在用户单击提交按钮后,我需要使用控件来确定用户是否提交了正确的答案,但是当我尝试引用控件的 id 时,我得到“txtAnser is not declared. It may be inaccessible”由于它的保护级别。

这是 .aspx 页面(它是母版页的标准内容页面):

<%@ Page Title="" Language="VB" MasterPageFile="~/top.master" AutoEventWireup="false" 
CodeFile="test_page.aspx.vb" Inherits="Default2" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="PageContent" Runat="Server">
<asp:SqlDataSource ID="sdsQuestionPuller" runat="server"
ConnectionString="<%$ ConnectionStrings:SchoolhouseConnectionString6 %>"
SelectCommand="SELECT QuestionText, AnswerType, PossibleAnswers, CorrectAnswers
FROM Questions WHERE (SubjectID = @SubjectID) AND (QuestionOrder = @QuestionOrder)">
<SelectParameters>
<asp:SessionParameter Name="SubjectID" SessionField="SubjectID" />
<asp:SessionParameter Name="QuestionOrder" SessionField="PageNumber" />
</SelectParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="sdsMaxQuestions" runat="server"
ConnectionString="<%$ ConnectionStrings:SchoolhouseConnectionString7 %>"
SelectCommand="SELECT MAX(QuestionOrder) AS LastQuestion FROM Questions GROUP BY SubjectID HAVING (SubjectID = @SubjectID)">
<SelectParameters>
<asp:SessionParameter Name="SubjectID" SessionField="SubjectID" />
</SelectParameters>
</asp:SqlDataSource>
<div>
<asp:Label ID="lblInstructionHolder" runat="server"></asp:Label>
</div>
<div>
<asp:Label ID="lblQuestionHolder" runat="server"></asp:Label>
</div>
<asp:PlaceHolder ID="plhQuestionHolder" runat="server"></asp:PlaceHolder>
<div>
<asp:Button ID="btnSubmit" Text="Submit" runat="server" />
</div>
</asp:Content>

这是后面的代码,我在提交按钮单击事件中遇到问题,我尝试在 select case 语句中引用 txtAnswer 控件:
Imports System.Data

Partial Class Default2
Inherits System.Web.UI.Page

Dim intMaxPage As Integer
'QuestionText, AnswerType, PossibleAnswers, CorrectAnswers
Dim strQuestion As String
Dim strQuestionType As String
Dim astrPossibleAnswers() As String
Dim intNumberOfPossAnswers As Integer
Dim astrCorrectAnswers() As String
Dim intNumberOfCorrectAnswers As Integer

Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init

End Sub

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'Load up the variables(make sure data is retrieved, create a view from the source, retrieve the only row, assing the item(s) from the row to variables)
sdsMaxQuestions.DataBind()
Dim dtvLastPageView As DataView = CType(sdsMaxQuestions.Select(DataSourceSelectArguments.Empty), DataView)
Dim dtrLastPageRow As DataRowView = dtvLastPageView.Item(0)
intMaxPage = CInt(Trim(dtrLastPageRow.Item(0).ToString))

Dim dtvQuestionsView As DataView = CType(sdsQuestionPuller.Select(DataSourceSelectArguments.Empty), DataView)
Dim dtrQuestionsRow As DataRowView = dtvQuestionsView.Item(0)
strQuestion = Trim(dtrQuestionsRow.Item(0).ToString)
strQuestionType = Trim(dtrQuestionsRow.Item(1).ToString)
astrPossibleAnswers = Split(Trim(dtrQuestionsRow.Item(2).ToString), ";")
intNumberOfPossAnswers = astrPossibleAnswers.Count
astrCorrectAnswers = Split(Trim(dtrQuestionsRow.Item(3).ToString), ";")
intNumberOfCorrectAnswers = astrCorrectAnswers.Count

'Finish loading controls
lblQuestionHolder.Text = strQuestion
Select Case strQuestionType
Case "checkbox"
lblInstructionHolder.Text = "Choose the correct answer(s)."
Case "radio"
lblInstructionHolder.Text = "Choose the best answer."
Case "fillintheblank"
lblInstructionHolder.Text = "Please fill in the blank, case doesn't count, spelling does."
Case Else
lblInstructionHolder.Text = "Somethings wrong, contact admin"
End Select

'Generate the controls for answers...
GenerateControls()

End Sub

Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
'Declare the variables, get any existing values, then check to see if the answer is correct, display a message and go to the next page or store the score.
Dim intNumberRight As Integer = 0
Dim intNumberTotal As Integer = 0

If Not (Session("NumberRight") = Nothing) Then
intNumberRight = CInt(Session("NumberRight"))
End If
If Not (Session("NumberTotal") = Nothing) Then
intNumberTotal = CInt(Session("NumberTotal"))
End If

Select Case strQuestionType
Case "checkbox"

Case "radio"

Case "fillintheblank"
'Here is where I am having issues
If txtAnswer Is Not Nothing Then

End If
If txtAnswer.text.ToLower = astrCorrectAnswers(0).ToLower Then

End If
Case Else
MsgBox("Somethings wrong, contact admin")
End Select

If intMaxPage = CType(Session("PageNumber"), Integer) Then
Response.Redirect("user_landing.aspx")
Else
Session("PageNumber") = CType(Session("PageNumber"), Integer) + 1
Response.Redirect("test_page.aspx")
End If
End Sub

Private Sub GenerateControls()
'Make the correct controls depending on the type
Dim conDivHolder As HtmlGenericControl = New HtmlGenericControl("div")

Select Case strQuestionType
Case "checkbox"
For i As Integer = 0 To intNumberOfPossAnswers - 1
Dim chkAnswer As CheckBox = New CheckBox
With chkAnswer
.ID = "chkAnswer" + i.ToString
.Text = astrPossibleAnswers(i)
End With
conDivHolder.Controls.Add(chkAnswer)
Next

Case "radio"
For i As Integer = 0 To intNumberOfPossAnswers - 1
Dim rdoAnswer As RadioButton = New RadioButton
With rdoAnswer
.ID = "rdoAnswer" + i.ToString
.Text = astrPossibleAnswers(i)
.GroupName = "rdoGroup"
End With
conDivHolder.Controls.Add(rdoAnswer)
Next

Case "fillintheblank"
Dim txtAnswer As TextBox = New TextBox
txtAnswer.ID = "txtAnswer"
conDivHolder.Controls.Add(txtAnswer)

Case Else
Dim lblOops As Label = New Label
lblOops.Text = "Oops, contact admin"
conDivHolder.Controls.Add(lblOops)
End Select

plhQuestionHolder.Controls.Add(conDivHolder)
End Sub

End Class

如果有人能指出我需要做什么,我将不胜感激。

谢谢,
西蒙

最佳答案

首先,您在 Page_Load (PostBack 或不)上生成控件,因此无论单击提交按钮之前 txtAnswer 的状态是什么,它现在都消失了 - txtAnswer 已重新生成并具有初始状态。仅当 IsPostBack 为 false 时,才应动态呈现控件。

其次,您可能应该在 Is Not Nothing if 语句 block 中访问 txtAnswer 的 Text 属性。否则你可能会得到一个空引用异常。

第三,要获得对 TextBox 控件的引用,您可以尝试通过 ID 在其占位符中查找控件,如下所示(我在 C# 中执行此操作 - 您可以自己翻译成 VB):

定义函数以递归方式查找控件:

protected Control FindControl(Control parent, string controlID)
{
Control control = parent.FindControlByID("controlID");

if (control != null)
{
return control;
}

foreach(Control child in control.Controls)
{
Control childControl = FindControl(child , controlID);

if (childControl != null)
{
return childControl;
}
}

return null;
}

调用递归查找函数:
Control ctrl = FindControl(plhQuestionHolder, "txtAnswer");

这些代码都没有经过实际测试,所以如果它不能按原样工作,请不要对我大喊大叫。这只是一个指南。

关于asp.net - 如何使用 VB 在 asp.net 3.5 中引用动态创建的控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5368992/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com