문제

편집 : 누군가 내가 아래에서 일어나려고하는 일을 할 수있는 더 현명한 방법을 제안 할 수 있다면, 그것은 또한 매우 감사 할 것입니다.

나는 게시물 방법에서 수량 (제품)을 가져 오는 다중 지식 양식을 작성하고 해당 숫자에 의존하는 양식 시퀀스를 표시합니다. 사용자가 다음 페이지로 이동하면 양식은이 정보를 수집하여 표시해야합니다 (확인을 위해) 은이 정보를 URL을 제공 할 서비스로 보냅니다.

말할 것도없이, 나는이 일을하는 데 문제가 있습니다. 다음은 내 (익명) 코드의 관련 부분입니다.

public partial class foo : System.Web.UI.Page
{
    Int quantityParam    = 3;
    ArrayList Users  = new ArrayList();
    //the information for each user is held in a hashtable the array list will be an array list of the user hashtables


protected void Page_Init(object sender, EventArgs e)
{
    if(null != Request["quantity1"])
       { 
             this.quantityParam = Request["quantity1"];
       }
}
protected void Page_Load(object sender, EventArgs e)
{
    int quantity = this.quantityParam;
    if(quantity < 1){ mviewThankYou.SetActiveView(View4Error);} 
    else 
    { //create a form for each user
        mviewThankYou.SetActiveView(View1EnterUsers);
        for(int user = 0;user < quantity; user++)
        {
            createUserForm(user);       
        }
    }   
}
protected void BtnNext1_Click(object sender, EventArgs e)
{
    if(Page.IsValid)
    {
        for(int i = 0; i < quantity; i++)
        {
            String ctrlName = "txtUser" + i.ToString();
            String ctrlEmail = "txtEmail" + i.ToString();
            TextBox name = (TextBox)FindControl(ctrlName);
            TextBox email = (TextBox)FindControl(ctrlEmail);

            /*BONUS QUESTION: How can I add the Hashtables to the Users Array without them being destroyed when I leave the function scope?

            this is where the failure occurs: 
            System.NullReferenceException: Object reference not set to an instance of an object. on: "tempUser.Add("name",name.Text); 
            */

            Hashtable tempUser = new Hashtable();
            tempUser.Add("name",name.Text);
            tempUser.Add("email",email.Text);
            this.Users.Add(tempUser);
        }
        for(int i = 0; i < quantity; i++)
        {
            v2Content.Text +="<table><tr><td>Name: </td><td>"+
            ((Hashtable)Users[i])["name"]+
            "</td></tr><tr><td>Email:</td><td>"+
            ((Hashtable)Users[i])["email"]+
            "</td></tr></table>";
        }
        mviewThankYou.SetActiveView(View2Confirm);
    }
}
private void createUserForm(int userNum){
    DataTable objDT = new DataTable();
        int rows = 2;
        int cols = 2;

    //create the title row..
    TableRow title = new TableRow();
    TableCell titleCell = new TableCell();
    formTable.Rows.Add(title);
    Label lblUser = new Label();
    lblUser.Text = "<b>User "+ (userNum+1) + "</b>";
    lblUser.ID = "lblTitle"+ userNum;
    titleCell.Controls.Add(lblUser);
    title.Cells.Add(titleCell);

    for(int i = 0; i < rows; i++)
    {
        TableRow tRow = new TableRow();     
        formTable.Rows.Add(tRow);
        for(int j = 0; j < cols; j++)
        {
            TableCell tCell = new TableCell();
            if(j == 0){
                Label lblTitle = new Label();
                if(i == 0){
                    lblTitle.Text = "User Name:";
                    lblTitle.ID = "lblUser" + userNum;
                }
                else{
                    lblTitle.Text = "User Email:";
                    lblTitle.ID = "lblEmail" + userNum;
                }
                tCell.Controls.Add(lblTitle);
            } else {
                TextBox txt = new TextBox();
                if(i==0){
                    txt.ID = "txtUser" + userNum;
                }
                else{
                    txt.ID = "txtEmail" + userNum;
                }
                RequiredFieldValidator val = new RequiredFieldValidator();
                val.ID = txt.ID + "Validator";
                val.ControlToValidate = txt.UniqueID;
                val.ErrorMessage = "(required)";

                tCell.Controls.Add(txt);
                tCell.Controls.Add(val);
            }
            tRow.Cells.Add(tCell);
        }//for(j)
    }//for(i)

    //create a blank row...
    TableRow blank = new TableRow();
    TableCell blankCell = new TableCell();
    formTable.Rows.Add(blank);
    Label blankLabel = new Label();
    blankLabel.Text = " ";
    blankLabel.ID = "blank" + userNum;
    blankCell.Controls.Add(blankLabel);
    blank.Cells.Add(blankCell);         

}//CreateUserForm(int)

(아마추어 코드)의 끔찍한 금액에 대해 죄송합니다. 실패하면 FindControl ()가 작동하지 않는다는 것이 생각하지만 이유를 알 수 없습니다.

어떤 도움을 줄 수 있다면, 나는 매우 크게 될 것입니다.

편집 : 오류를 표시하면 도움이 될 수 있습니다.

오류 (112 행) 예외 세부 사항 : System.NullReferenceException : 객체 참조는 객체의 인스턴스로 설정되지 않습니다.

소스 오류 :

라인 111 : Hashtable Tempuser = New Hashtable ();
112 행 : tempuser.add ( "name", name.text);
113 행 : tempuser.add ( "이메일", email.text);
114 행 : this.users.add (Tempuser);

도움이 되었습니까?

해결책 2

나는 그것을 알아:

FindControl ()은 호출되는 통제의 어린이를 직접 검색하는 역할을합니다.

내가 그것을 호출 할 때, 그것은 (자동으로) page.findControl () 필드 내부의 테이블 생성과 테이블 컨트롤을 중첩했습니다.

tableId.findControl ()을 호출했을 때 컨트롤을 찾았습니다.

도움을 주셔서 감사합니다, 그레고리, 그리고 모든 의견에 감사드립니다.

-매트

다른 팁

Page_load에서 매번 양식을 다시로드하고 있다는 사실에 문제가 발생합니다. 동적 텍스트 상자를 한 번만로드하면 확인을 위해 필요할 때 찾을 수 있습니다. Page_load가 재건되는 한 답을 찾지 못하고 아무것도 찾지 못할 위험이 있습니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top