c# - 将 ValidationControls 动态添加到 updatepanel? [ASP.NET]

标签 c# asp.net validation updatepanel requiredfieldvalidator

我创建的动态更新面板有问题。

问题是我想向我在服务器端创建的每个元素添加一个验证器,即 RequriedFieldValidator。 上面这个效果很好。 但是当我点击提交按钮时它什么也没做。我可以在 htmlShellcode 中看到一个 span 元素显示了错误,但它有“style =”visibility=hidden”。

我该如何解决这个问题?我也不确定我是否从一开始就做对了(对于编程来说非常陌生)。

标记:

 <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;
    }

最佳答案

But when i'm clicking the submit button it does nothing.

由于我没有看到“提交”按钮,因此我猜测您指的是“添加”和“删除”按钮。如果是这种情况,问题在于您的所有 RequiredFieldValidators 都将 ValidationGroup 属性设置为 QuestionGroup,但在您的任何一个中都没有设置该属性。按钮。

像这样修改按钮:

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

关于c# - 将 ValidationControls 动态添加到 updatepanel? [ASP.NET],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15407793/

相关文章:

c# - 如何使用 EventHandlers 在 ASP.NET 页面中动态创建/删除控件?

c# - 将字符串属性序列化为属性,即使字符串为空

c# - 如何更改 .wav 格式音频文件比特率

ASP.NET MVC 3 成员(member)提供程序

perl - Moose 属性初始化

c# - Lambda/LINQ 选择最小值

.net - 如何向用户发送电子邮件确认链接

javascript - 从javascript中的下拉列表中获取选定的索引

javascript - 验证不以特殊字符开头或结尾的文本框

javascript - 每当以 Bootstrap 的形式出现无效的弹出消息时,我们如何才能停止发送数据?