添加ValidationControls动态地的UpdatePanel? [ASP.NET] [英] Add ValidationControls dynamically to updatepanel? [ASP.NET]

查看:140
本文介绍了添加ValidationControls动态地的UpdatePanel? [ASP.NET]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,我已经创建了我的动态UpdatePanel的。

问题是,我想添加一个验证器,即RequriedFieldValidator,到我创建的服务器端的每一个元素。
这上面工作正常。
但是,当我点击提交按钮,它什么都不做。我可以在htmlShell code,一个span元素是存在显示错误看,但它有的风格=知名度=隐藏。

我要如何解决这个问题呢?我真的不知道我在做它从一开始就不是(非常编程新)

MARKUP:

 < ASP:面板ID =UpdatepanelWrapper的CssClass =Updatepanelwrapper=服务器>
    < ASP:的UpdatePanel ID =UpdatePanel1=服务器>
        <&的ContentTemplate GT;
            < ASP:占位符=服务器ID =WholeWrapper>                < ASP:占位符=服务器ID =QuestionWrapper>
                    < ASP:占位符=服务器ID =LabelQuestion>< ASP:占位符>
                    < ASP:占位符=服务器ID =问题>< / ASP:占位符>
                < / ASP:占位符>
                < / ASP:占位符>
            < / ASP:占位符>
        < /&的ContentTemplate GT;
        <&触发器GT;
            < ASP:AsyncPostBackTrigger控件ID =btnAdd事件名称=点击/>
            < ASP:AsyncPostBackTrigger控件ID =btnDelete事件名称=点击/>
        < /触发器>    < / ASP:的UpdatePanel>
< ASP:面板ID =ButtonPanel的CssClass =ButtonPanel=服务器>
        < ASP:ImageButton的ID =btnDelete=服务器的ImageUrl =〜/图片/ Deleteicon.png的CssClass =DeleteButton的OnClientClick =btnDelete_Click/>
        < ASP:ImageButton的ID =btnAdd=服务器的ImageUrl =〜/图片/ Addicon.png的CssClass =Add按钮的OnClientClick =btnAddQuestion_Click/>
    < / ASP:面板>
< / ASP:面板>

服务器端的code:(覆盖方法OnInit是最重要的一个)

 公共部分类_Default:页
{
    静态INT CountOfQuestions = 1;    私人的RequiredFieldValidator [] ArrRequiredFieldQuestion;
    私人面板[] ArrWholePanel;
    私人面板[] ArrQuestionPanel;    私人标签[] ArrQuestionLabel;
    私人文本框[] ArrQuestionBox;    保护无效的Page_Load(对象发件人,EventArgs的发送)
    {    }
    私人无效LoadControls()
    {
        控制myControl = GetPostBackControl(this.Page);        如果(myControl!= NULL)
        {
            如果(myControl.ID.ToString()==btnAdd)//安德拉直到btnAddQuestion OM连接按钮斯卡användas
            {
                CountOfQuestions ++;
            }
            如果(myControl.ID.ToString()==btnDelete)
            {
                CountOfQuestions--;
            }        }
    }    保护覆盖无效的OnInit(EventArgs的发送)
    {
        base.OnInit(E);
        如果(!的IsPostBack)
        {
            CountOfQuestions = 1;
        }
        LoadControls();        ArrRequiredFieldQuestion =新的RequiredFieldValidator [CountOfQuestions]
        ArrWholePanel =新面板[CountOfQuestions]
        ArrQuestionPanel =新面板[CountOfQuestions]
        ArrQuestionLabel =新的标签[CountOfQuestions]
        ArrQuestionBox =新的TextBox [CountOfQuestions]
        的for(int i = 0; I< CountOfQuestions;我++)
        {
            的RequiredFieldValidator ReqFieldQuestion =新的RequiredFieldValidator();            占位符WholeWrapper = UpdatePanel1.FindControl(WholeWrapper)作为占位符;            面板WholePanel =新面板();
            面板QuestionPanel =新面板();            面板CorrectAnswerPanel =新面板();            标签QuestionLabel =新的Label();            文本框问题=新的TextBox();            QuestionLabel.Text =写下你的问题;            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 =验证+ i.ToString();
            ReqFieldQuestion.ControlToValidate =tbxQuestion+ i.ToString();
            ReqFieldQuestion.ValidationGroup =QuestionGroup;
            ReqFieldQuestion.ErrorMessage =错误;
            ReqFieldQuestion.Attributes.Add(RUNAT,服务器);            QuestionPanel.CssClass =QuestionEntry;            QuestionPanel.Controls.Add(QuestionLabel);
            QuestionPanel.Controls.Add(问题);
            QuestionPanel.Controls.Add(ReqFieldQuestion);            WholeWrapper.Controls.Add(WholePanel);
            WholePanel.Controls.Add(QuestionPanel);
            ArrRequiredFieldQuestion [I] = ReqFieldQuestion;            ArrQuestionPanel [I] = QuestionPanel;            ArrQuestionLabel [I] = QuestionLabel;
            ArrQuestionBox [I] =问题;
        }
    }    保护无效btnAddQuestion_Click(对象发件人,EventArgs的发送)
    {
        // Handeld由pre_init和LoadControls
    }    保护无效btnDelete_Click(对象发件人,EventArgs的发送)
    {
        // Handeld由pre_init和LoadControls
    }
    公共静态控制GetPostBackControl(页翻动书页)
    {
        控制postbackControlInstance = NULL;
        如果(postbackControlInstance == NULL)
        {
            的for(int i = 0; I< thePage.Request.Form.Count;我++)
            {
                如果((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 [Ⅰ]。长度 - 2));
                    返回postbackControlInstance;
                }
            }
        }
        返回postbackControlInstance;
    }


解决方案

  

但是,当我点击提交按钮,它什么都不做。


由于我没有看到一个提交按钮,我带你指的是添加和删除按钮猜测。如果是这种情况,问题是,所有的 RequiredFieldValidators 已经在的ValidationGroup 属性设置为 QuestionGroup 的,但这不以任何的按钮设置

修改按钮是这样的:

 < ASP:ImageButton的ID =btnAdd=服务器的ValidationGroup =QuestionGroup... />

I have a problem with my dynamic updatepanel that I've created.

The problem is that i want to add a validator,i.e RequriedFieldValidator, to every element that i'm creating on the serverside. This above works fine. But when i'm clicking the submit button it does nothing. I can see in the htmlShellcode that a span element is there showing the error, but it has the "style="visibility=hidden".

How do i solve this issue? i'm not really sure i'm doing it right from the beginning either(very new to programming).

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>

SERVERSIDE CODE: (Override method OnInit is the important one)

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.

Since I don't see a Submit button, I'm taking a guess that you mean the Add and Remove buttons. If this is the case, the problem is that all your RequiredFieldValidators have the ValidationGroup property set to QuestionGroup but this is not set in any of your buttons.

Modify the Buttons like this:

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

这篇关于添加ValidationControls动态地的UpdatePanel? [ASP.NET]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆