如何重置 ASP.NET 字段中的值?

发布于 2024-08-29 23:13:10 字数 289 浏览 4 评论 0原文

当前表单位于此处。它并不完整,只有几个选项可行。

选择“Image CD”,然后选择任意分辨率,然后单击“添加到订单”。该订单将记录在服务器端,但在客户端,我需要将产品下拉列表重置为“{select}”,以便用户知道他们需要选择另一个产品。这与子选择消失的想法是一致的。

我不知道是否应该使用 ASP 回发或标准表单提交,并且当用户向订单添加项目时,大多数字段都需要重置。

The current form is here. It is not complete, and only a couple options will work.

Select "Image CD" and then any resolution and click "Add to Order." The order will be recorded on the server-side, but on the client-side I need to reset the product drop-down to "{select}" so that the user will know that they need to select another product. This is consistant with the idea that the sub-selections disappear.

I don't know whether I should be using ASP postback or standard form submittal, and most of the fields need to be reset when the user adds an item to the order.

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

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

发布评论

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

评论(3

枫林﹌晚霞¤ 2024-09-05 23:13:10

我会使用 Response.Redirect(Request.RawUrl) 方法从服务器端重置表单数据。

一点解释:这是 PRG 设计模式

用于帮助避免某些重复
表单提交并允许用户代理
更直观地表现
书签和刷新按钮。

此问题的解决方法:
例如,必要的数据可以存储在Session中。这意味着我们通过第一个 POST 获取数据,将其放入存储,执行重定向并取回。

I'd use Response.Redirect(Request.RawUrl) method to reset the form data from the server side.

A little explanation: this is PRG design pattern

Used to help avoid certain duplicate
form submissions and allow user agents
to behave more intuitively with
bookmarks and the refresh button.

A workaround for this question:
necessary data may be stored in Session for example. This means we getting the data with the first POST, putting it to the storage, performing redirect and getting it back.

青芜 2024-09-05 23:13:10

在表单上的页面加载事件中,您需要添加与此类似的内容:

if (IsPostBack)
{
   //Proccess the order here

   ProductOption.SelectedIndex = 0;
}

这将允许您处理订单,但然后重新开始订单表单。

In the pageload event on the form, you need to add something simalar to this:

if (IsPostBack)
{
   //Proccess the order here

   ProductOption.SelectedIndex = 0;
}

That will allow you to process the order, but then start over the order form.

硪扪都還晓 2024-09-05 23:13:10

最简单的方法是在控件类型上分叉的递归函数,

private void ResetControls( Control control )
{
    if ( control == null)
        return;

    var textbox = control As TextBox;
    if ( textbox != null )
        textbox.Text = string.Empty;

    var dropdownlist = control as DropDownList;
    if ( dropdownlist != null )
        dropdownlist.SelectedIndex = 0; // or -1

    ...

    foreach( var childControl in controlControls )
        ResetControls( childControl );
}

您可以通过传递 this 在 Load 事件中调用此函数。 (这是假设您想要重置多个控件或一小部分控件)。

The simplest means would be a recursive function that forks on the type of control

private void ResetControls( Control control )
{
    if ( control == null)
        return;

    var textbox = control As TextBox;
    if ( textbox != null )
        textbox.Text = string.Empty;

    var dropdownlist = control as DropDownList;
    if ( dropdownlist != null )
        dropdownlist.SelectedIndex = 0; // or -1

    ...

    foreach( var childControl in controlControls )
        ResetControls( childControl );
}

You would this call this function in your Load event by passing this. (This is presuming you want to reset more than a single control or small list of controls).

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