存储用于回发的动态插入控件的数量

发布于 2024-09-07 03:31:09 字数 315 浏览 4 评论 0原文

我需要创建以下功能,并寻找存储多个回发信息的最佳方法:

假设我有一个文本框,它与一个问题相关,即“我访问过的地方”。一旦 TextBox 将在第一次加载时加载。在其下方,有一个“添加另一个地点”按钮。这将回发,然后将在第一个文本框下方添加另一个文本框。这可以进行 x 次。

我的问题是,考虑到此加载需要在 Init 事件中完成,并且此时 ViewState 尚未加载,如何存储/记住用户已添加到页面的控件数量?

编辑*:

我忘了提及,当用户保存时,会进行一些验证,因此如果验证失败,需要显示所有文本框及其发布的数据。

I need to create the following functionality, and looking for the best way to store info on multiple postbacks:

Say I have a TextBox, that is related to a question, i.e. "Places I've visited". Once TextBox will be loaded on first load. Under that, there is a button to "Add another place". This will postback, which will then add another TextBox underneath the first. This can be done x number of times.

The question I have is, how do I store/remember the number of controls that have been added to the page by the user, considering that this load needs to be done in the Init event, and ViewState is not loaded at this point?

EDIT*:

I forgot to mention, that when the user saves, there would be some validation, so if validation fails, need to show all the TextBoxes, with their posted data.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

若无相欠,怎会相见 2024-09-14 03:31:09

如果您只允许表单上存在有限数量的文本框,则可以在 Page_Init 期间创建该数量的文本框并将其可见性设置为 false,以便它们不会在浏览器中呈现。在按钮的单击事件中,您可以找到第一个不可见的文本框并将可见性更改为 true。像这样的事情

protected void Page_Init(object sender, EventArgs e)
{
    for (int i = 0; i < 20; i++)
    {
        this.Form.Controls.Add(new TextBox() { Visible = false });
    }
}

protected void addTextboxButton_Click(object sender, EventArgs e)
{
    TextBox tb = this.Form.Controls.OfType<TextBox>().FirstOrDefault(box => box.Visible == false);
    if (tb != null) tb.Visible = true;
}

使用这种方法,每次单击按钮时文本框都会变得一一可见,并且回发值会保留。

显然,您需要投入更多的工作,例如定义一些文字控件来创建换行符和文本框提示,以及当用户达到您设置的任何有限限制时显示一条消息。

If you were going to only allow a finite number of textboxes on your form, you could create that number of textboxes during Page_Init and set their visibility to false so they would not be rendered in the browser. On the button's click event, you could find the first invisible textbox and change the visibility to true. Something like this

protected void Page_Init(object sender, EventArgs e)
{
    for (int i = 0; i < 20; i++)
    {
        this.Form.Controls.Add(new TextBox() { Visible = false });
    }
}

protected void addTextboxButton_Click(object sender, EventArgs e)
{
    TextBox tb = this.Form.Controls.OfType<TextBox>().FirstOrDefault(box => box.Visible == false);
    if (tb != null) tb.Visible = true;
}

Using this approach, the textboxes become visible one by one on each button click, and the postback values stick.

Obviously, you'd want to put some more work into it, such as perhaps definining some literal controls to create line breaks and prompts for the textboxes, as well as displaying a message when the user hit whatever finite limit you set.

左耳近心 2024-09-14 03:31:09

选项1:您可以使用Session来存储文本框的数量...

选项2:您甚至可以在页面的Load事件中添加控件,其中您将获得ViewState信息。

以下是可以帮助您的链接...

真正了解动态控件

真正理解ViewState

Option 1 : You can use Session to store the number of Textboxes...

Option 2 : You can even add controls in the Load event of the page, wherein you will have the ViewState information.

Here are the links that could help you...

TRULY understanding Dynamic Controls

Truly Understanding ViewState

对岸观火 2024-09-14 03:31:09

经过一番思考并考虑给出的答案后,提出了这个解决方案。

在控件占位符中,有一个隐藏的输入字段,它将存储添加到页面的控件数量。然后,在 Init 上,我可以使用以下代码(仅测试):

protected override void OnInit(EventArgs e)
{
    int i = 0;
    i = Int32.Parse(hdnTestCount.Value);

    if(Request.Params[hdnTestCount.UniqueID] != null)
    {
        i = Int32.Parse(Request.Params[hdnTestCount.UnitueID]);
    }

    for (int j = 1; j <= i; j++)
    {
         TextBox txtBox = new TextBox();
         txtBox.ID = "Test" + j.ToString();
         plhTest.Controls.Add(txtBox);
    }
}

protected void btnAdd_OnClick(object sender, EventArgs e)
{
    int i = 0;
    i = Int32.Parse(hdnTestCount.Value) + 1;

    TextBox txtBox = new TextBox();
    txtBox.ID = "Test" + i.ToString();
    plhTest.Controls.Add(txtBox);
    hdnTestCount.Value = i.ToString();
}

当然,唯一的问题是用户可以在隐藏字段中操纵该值。唯一的其他选择是使用 Session,我不想使用它,因为它会一直存在,如果以这种方式刷新页面,表单将自行重置,这就是应该发生的情况。

After some thinking, and considering the answers given, came up with this solution.

In the controls placeholder, have a hidden input field which will store the number of controls added to the page. Then, on Init, I can have the following code (testing only):

protected override void OnInit(EventArgs e)
{
    int i = 0;
    i = Int32.Parse(hdnTestCount.Value);

    if(Request.Params[hdnTestCount.UniqueID] != null)
    {
        i = Int32.Parse(Request.Params[hdnTestCount.UnitueID]);
    }

    for (int j = 1; j <= i; j++)
    {
         TextBox txtBox = new TextBox();
         txtBox.ID = "Test" + j.ToString();
         plhTest.Controls.Add(txtBox);
    }
}

protected void btnAdd_OnClick(object sender, EventArgs e)
{
    int i = 0;
    i = Int32.Parse(hdnTestCount.Value) + 1;

    TextBox txtBox = new TextBox();
    txtBox.ID = "Test" + i.ToString();
    plhTest.Controls.Add(txtBox);
    hdnTestCount.Value = i.ToString();
}

Of course the only issue with this, is that the value could be manipulated by the user in the hidden field. The only other option would be to use Session, which I do not want to use as it sticks around, whereby if the page is refreshed this way, the form will reset itself, which is what should happen.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文