2013-03-14 64 views
2

我遇到了我創建的動態更新面板的問題。動態添加ValidationControls到updatepanel? [ASP.NET]

問題是,我想添加一個驗證程序,即RequriedFieldValidator,到我在服務器端創建的每個元素。 這上面的工作正常。 但是當我點擊提交按鈕時,它什麼都不做。我可以在一個span元素是存在顯示錯誤的htmlShellcode看到,但它具有「風格=」知名度=隱藏」。

我怎麼解決這個問題呢?我真的不知道我在做它從一開始是(很新的編程)

MARKUP:

<asp:Panel ID="UpdatepanelWrapper" CssClass="Updatepanelwrapper" runat="server"> 
    <asp:UpdatePanel ID="UpdatePanel1" runat="server"> 
     <ContentTemplate> 
      <asp:PlaceHolder runat="server" ID="WholeWrapper"> 

       <asp:PlaceHolder runat="server" ID="QuestionWrapper"> 
        <asp:PlaceHolder runat="server" ID="LabelQuestion"><asp:PlaceHolder> 
        <asp:PlaceHolder runat="server" ID="Question"></asp:PlaceHolder>       
       </asp:PlaceHolder>      
       </asp:PlaceHolder> 
      </asp:PlaceHolder> 
     </ContentTemplate> 
     <Triggers> 
      <asp:AsyncPostBackTrigger ControlID="btnAdd" EventName="Click" /> 
      <asp:AsyncPostBackTrigger ControlID="btnDelete" EventName="Click" /> 
     </Triggers> 

    </asp:UpdatePanel> 
<asp:Panel ID="ButtonPanel" CssClass="ButtonPanel" runat="server"> 
     <asp:ImageButton ID="btnDelete" runat="server" ImageUrl="~/Images/Deleteicon.png" CssClass="DeleteButton" OnClientClick="btnDelete_Click" /> 
     <asp:ImageButton ID="btnAdd" runat="server" ImageUrl="~/Images/Addicon.png" CssClass="AddButton" OnClientClick="btnAddQuestion_Click" /> 
    </asp:Panel> 
</asp:Panel> 

服務器端代碼:(覆蓋方法OnInit是最重要的一個)

public partial class _Default : Page 
{ 
    static int CountOfQuestions = 1; 

    private RequiredFieldValidator[] ArrRequiredFieldQuestion; 
    private Panel[] ArrWholePanel; 
    private Panel[] ArrQuestionPanel; 

    private Label[] ArrQuestionLabel; 
    private TextBox[] ArrQuestionBox; 

    protected void Page_Load(object sender, EventArgs e) 
    { 

    } 
    private void LoadControls() 
    { 
     Control myControl = GetPostBackControl(this.Page); 

     if (myControl != null) 
     { 
      if (myControl.ID.ToString() == "btnAdd") //Ändra till btnAddQuestion om en "button" ska användas 
      { 
       CountOfQuestions++; 
      } 
      if (myControl.ID.ToString() == "btnDelete") 
      { 
       CountOfQuestions--; 
      } 

     } 
    } 

    protected override void OnInit(EventArgs e) 
    { 
     base.OnInit(e); 
     if (!IsPostBack) 
     { 
      CountOfQuestions = 1; 
     } 
     LoadControls(); 

     ArrRequiredFieldQuestion = new RequiredFieldValidator[CountOfQuestions]; 
     ArrWholePanel = new Panel[CountOfQuestions]; 
     ArrQuestionPanel = new Panel[CountOfQuestions]; 
     ArrQuestionLabel = new Label[CountOfQuestions]; 
     ArrQuestionBox = new TextBox[CountOfQuestions]; 
     for (int i = 0; i < CountOfQuestions; i++) 
     { 
      RequiredFieldValidator ReqFieldQuestion = new RequiredFieldValidator(); 

      PlaceHolder WholeWrapper = UpdatePanel1.FindControl("WholeWrapper") as PlaceHolder; 

      Panel WholePanel = new Panel(); 
      Panel QuestionPanel = new Panel(); 

      Panel CorrectAnswerPanel = new Panel(); 

      Label QuestionLabel = new Label(); 

      TextBox Question = new TextBox(); 



      QuestionLabel.Text = "Write your question"; 

      Question.TextMode = TextBoxMode.MultiLine; 
      Question.Width = 550; 
      Question.Height = 50; 


      WholePanel.ID = "WholePanel" + i.ToString(); 
      QuestionPanel.ID = "QuestionPanel" + i.ToString(); 
      Question.ID = "tbxQuestion" + i.ToString(); 


      ReqFieldQuestion.ID = "Validate" + i.ToString(); 
      ReqFieldQuestion.ControlToValidate = "tbxQuestion" + i.ToString(); 
      ReqFieldQuestion.ValidationGroup = "QuestionGroup"; 
      ReqFieldQuestion.ErrorMessage = "Error"; 
      ReqFieldQuestion.Attributes.Add("runat", "server"); 

      QuestionPanel.CssClass = "QuestionEntry"; 

      QuestionPanel.Controls.Add(QuestionLabel); 
      QuestionPanel.Controls.Add(Question); 
      QuestionPanel.Controls.Add(ReqFieldQuestion); 

      WholeWrapper.Controls.Add(WholePanel); 
      WholePanel.Controls.Add(QuestionPanel); 


      ArrRequiredFieldQuestion[i] = ReqFieldQuestion; 

      ArrQuestionPanel[i] = QuestionPanel; 

      ArrQuestionLabel[i] = QuestionLabel; 
      ArrQuestionBox[i] = Question; 
     } 
    } 

    protected void btnAddQuestion_Click(object sender, EventArgs e) 
    { 
     //Handeld by Pre_init and LoadControls 
    } 

    protected void btnDelete_Click(object sender, EventArgs e) 
    { 
     //Handeld by Pre_init and LoadControls 
    } 


    public static Control GetPostBackControl(Page thePage) 
    {    
     Control postbackControlInstance = null; 
     if (postbackControlInstance == null) 
     { 
      for (int i = 0; i < thePage.Request.Form.Count; i++) 
      { 
       if ((thePage.Request.Form.Keys[i].EndsWith(".x")) || (thePage.Request.Form.Keys[i].EndsWith(".y"))) 
       { 
        postbackControlInstance = thePage.FindControl(thePage.Request.Form.Keys[i].Substring(0, thePage.Request.Form.Keys[i].Length - 2)); 
        return postbackControlInstance; 
       } 
      } 
     } 
     return postbackControlInstance; 
    } 
+0

_but - 我沒有看到一個提交按鈕 – Blachshma 2013-03-14 11:29:47

回答

2

但是,當我點擊提交按鈕它什麼都不做。

由於我沒有看到提交按鈕,我猜測你是指添加和刪除按鈕。如果是這樣的話,問題是,你所有的RequiredFieldValidatorsValidationGroup屬性設置爲QuestionGroup但這不是在你的任何按鍵的設置。

修改按鈕是這樣的:當我點擊提交button_

<asp:ImageButton ID="btnAdd" runat="server" ValidationGroup="QuestionGroup" ... /> 
+2

哦,媽的。我想我徹底通過發佈的代碼。 我有一個submitbutton,只是我錯過了爲它添加標記碼。 但ValidationGroup的解決方案解決了這個問題。 這是尷尬..我應該知道,我顯然錯過了添加它。 – 2013-03-14 11:37:46

+1

適合所有人:) – Blachshma 2013-03-14 11:38:47