从后面的代码提交表单 [英] Submit a form from code behind

查看:74
本文介绍了从后面的代码提交表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在c#/asp.net应用程序中实现功能.

I'm having trouble implementing a functionality on my c#/asp.net app.

我有一个带有RadioButtonList和一个提交按钮的表单.RadioButtonList是在 Page_Load()上从我从数据库中检索到的对象列表中生成的.如果列表中只有1个对象,我想自动提交表单.

I have a form with a RadioButtonList and a submit button. The RadioButtonList is generated on Page_Load() from a list of objects I retrieve from the database. I would like to automatically submit the form if there is only 1 object in the list.

我可以访问我的Form对象,提交按钮等...但是我似乎找不到解决方案(最后,我正在寻找一种 form.Submit())方法.

I have access to my Form object, to the submit button etc... but I can't seem to find a solution (in the end I'm looking for a kind of form.Submit() ) method.

有人知道我该怎么做吗?

Does anyone have an idea of how I could do this ?

提前谢谢!

编辑>>这是代码:

.aspx: http://pastebin.com/0E6T7dqH

.aspx.cs: http://pastebin.com/54payZJP

.aspx.cs : http://pastebin.com/54payZJP

EDIT2 >>>

EDIT2 >>>

起初似乎没有办法做我想做的事,最终我将会话变量与response.redirect()一起使用

As it seems there is no way to do what I wanted to do at first, I ended up using a session variable with a response.redirect()

来源: http://dotnetslackers.com/Community/blogs/haissam/archive/2007/11/26/ways-to-pass-data-between-webforms.aspx

推荐答案

我实际上不得不做一次类似的事情.这是您可以做到的方式.

I actually had to do something similar, once. Here is a way you can do it.

Asp.Net按钮具有一个名为 PostBackUrl 的属性,它完全符合您的期望-它控制了单击按钮后表单的发布位置.

Asp.Net buttons have a property called PostBackUrl, and it does exactly what you would expect - it controls where the form will post if you click the button.

您还可以使用 RegisterStartupScript 函数来在页面.

You can also use the RegisterStartupScript function to render javascript on the page.

现在,有了这两点,您就可以实现自己的目标.

Now, with these two pieces, you can achieve your goal.

if(!IsPostBack)
{
    if(results == 1)
    {
        button.PostBackUrl = "next page's url"
        //Register script to click the button using RegisterStartupScript
    }
}

现在,向您展示了这一点,我会警告您,这可能无法获得最佳的用户体验.当我这样做时,它是针对一个非常具体的案例,没有其他解决方案.该页面实际上将回发给用户,并且他们将在javascript单击按钮生效之前看到该页面.另外,当您设置按钮的PostBackUrl时,这意味着单击该按钮时,您的整个表单将被发布到指定的页面.当前页面后面的代码根本不会触发,因此,如果您进行任何验证,它将无法运行.

Now, having shown you this, I will warn you it may not make for the best user experience. When I did it, it was for a very specific case that had no other solution. The page will actually post back to the user, and they will see the page for a moment before the javascript to click the button takes effect. Additionally, when you set a button's PostBackUrl, that means that when it is clicked, your entire form will be posted to the page specified. The code behind for the current page will not fire at all, so if you have any validation, it won't run.

让用户单击按钮来提交表单没有什么错,即使他们只有一个选择.根据我的经验,用户喜欢感觉自己在系统上处于控制之中.当页面只是在没有输入的情况下执行操作时,他们就不会喜欢它.

There's nothing wrong with letting the user click the button to submit the form even if they only have one choice. In my experience, users like to feel like they are in control on the system; they don't like it when pages just do things without their input.

此外,使用 Response.Redirect 将下一页需要的信息放入会话或什至是数据库表中,并没有任何错误.这是一种相当普遍的做法,并且在大多数情况下都可以可靠地工作.

Also, there is not really anything wrong with putting the information the next page needs into the session or even a database table, and using Response.Redirect. It's a fairly common practice and works reliably in most scenarios.

这篇关于从后面的代码提交表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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