동적으로 컨트롤을 웹 양식에 삽입하는 프로그램을 작성하고 있습니다. 변수에 따라 텍스트 상자, 라디오 단추 집합 또는 체크 상자 집합을 추가합니다. 나중에 사용자가 제출 단추를 클릭 한 후 컨트롤을 사용하여 사용자가 올바른 대답을 제출하는지 확인해야하지만 컨트롤의 ID를 참조하려고하면 "txtAnser가 선언되지 않습니다. 액세스 할 수 없습니다. 때문에 그것은 보호 수준의 여기 VB에서 asp.net 3.5에서 동적으로 생성 된 컨트롤을 참조하는 방법
가 .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
누구든지 내가해야 할 일을 지적 할 수 있으면 감사하게 생각합니다.
덕분에, 사이먼
ㅎ ... 멋지다. :) – Kon
감사합니다. 훌륭한 게시물이었고 도움이되었습니다. – Simon