请问asp.net中如何在后台代码里提交表单

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了请问asp.net中如何在后台代码里提交表单相关的知识,希望对你有一定的参考价值。

我的代码如下:
Default.aspx内容:
<form id="form1" name="form1" method="post" action="">
<div>
<input id="txtName" name="txtName" type="text" style="width:180px" />
<input name="btnTijiao" type="submit" value="TiJiao" />
</div>
<form id="xixi" runat="server">
<table>
<tr>
<td><asp:Button ID="btnSubmitForm1" runat="server" Text="提交表单form1" OnClick="btnSubmitForm1_Click" /></td>
</tr>
</table>
</form>
Default.aspx.cs内容
protected void Page_Load(object sender, EventArgs e)

if (!IsPostBack)

string testName = Request.Params["txtName"];
Response.Write(testName);


protected void btnSubmitForm1_Click(object sender, EventArgs e)



我想要在这个btnSubmitForm1_Click函数中提交表单form1,然后Response.Write一下form1中id=txtName的Text控件中的值。
我知道单击form1中TiJiao按钮可以做到这一点,但我不知道怎样才能做到单击“提交表单form1”也能做到这一点

参考技术A 参考参考我的代码 链接Access数据库的:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.htmlControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data.OleDb;
using System.Web.SessionState;

public partial class _Default : System.Web.UI.Page

protected void Page_Load(object sender, EventArgs e)

if (!Page.IsPostBack)

//////Session["adm"] = TextBox1.Text;///////new add ???????
TextBox1.Text = "";
TextBox2.Text = "";
TextBox3.Text = "";
TextBox4.Text = "";



protected void Button1_Click(object sender, EventArgs e)

//////////////////////////////////////////////////输入判断
string strname,pwd1,pwd2,email;
strname = TextBox1.Text;
pwd1 = TextBox2.Text;
pwd2 = TextBox4.Text;
email = TextBox3.Text;
if (strname=="")

Response.Write("<script>alert('用户名输入不能为空')</script>");
return;

if (pwd1 == "")

Response.Write("<script>alert('密码输入不能为空')</script>");
return;

if (pwd2 == "")

Response.Write("<script>alert('您还未输入密码确认')</script>");
return;

if (email == "")

Response.Write("<script>alert('请输入电子邮件')</script>");
return;

if (pwd1!=pwd2)

Response.Write("<script>alert('两次密码输入不一致')</script>");
return;

/////////////////////////////////////////////////////////////////////数据库链接.注册用户
OleDbConnection objconn = new OleDbConnection();
objconn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:/mysite1/AcDatebase/db1.mdb";
objconn.Open();
string objsql,username,password,useremail;
username=TextBox1.Text.Trim();
password=TextBox2.Text.Trim();
useremail=TextBox3.Text.Trim();
////////////////////////////////////////判断用户是否存在
string jack;
jack = "select * from denglu where name='" + username + "'";
//执行SQL语句
OleDbCommand Cmd = new OleDbCommand(jack, objconn);
//执行Datareader
OleDbDataReader dr = Cmd.ExecuteReader();
//作出判断
if (dr.Read())

Response.Write("<script>alert('用户名已经存在!');window.location='denglu.aspx';</script>");
TextBox1.Text = "";
TextBox2.Text = "";
TextBox3.Text = "";
TextBox4.Text = "";
//用户存在...do something..

else

/////////////用户不存在,添加新用户
string objsqlscore;
objsql = "insert into denglu(name,pwd,email)values('" + username + "','" + password + "','" + useremail + "')";
objsqlscore = "insert into score(name)values('" + username + "')";
OleDbCommand objcmd = new OleDbCommand(objsql, objconn);
OleDbCommand objcmdscore = new OleDbCommand(objsqlscore, objconn);
objcmd.ExecuteNonQuery();
objcmdscore.ExecuteNonQuery();
objconn.Close();
Session["adm"] = TextBox1.Text;
Response.Write("<script>alert('注册成功!系统将会分配给您100的答题积分。请重新登陆。。。'); window.location='denglu.aspx';</script>");
////Response.Redirect("gerenxinxi.aspx");存在,
/////////////<script>alert('注册成功!')</script>无法显示
////// 添加 Session
///////////
//Session["adm"] = TextBox1.Text;
//Response.Redirect("gerenxinxi.aspx");


protected void Button2_Click(object sender, EventArgs e)

///////////////////////////////////////////////////////////////////////////////////////////用户登陆
OleDbConnection objconn = new OleDbConnection();
objconn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:/mysite1/AcDatebase/db1.mdb";
objconn.Open();
string objsql, username, password, useremail;
username = TextBox1.Text.Trim();
password = TextBox2.Text.Trim();
useremail = TextBox3.Text.Trim();
////////////////////////////////////////判断用户是否存在
string jack;
jack = "select name from denglu where name='" + username + "'";
//执行SQL语句
OleDbCommand Cmd1 = new OleDbCommand(jack, objconn);
//执行Datareader
OleDbDataReader dr1 = Cmd1.ExecuteReader();
//作出判断
if (!dr1.Read())

///////////////////////////////用户不存在
Response.Write("<script>alert('该用户名不存在!请先注册...');window.location='denglu.aspx';</script>");
TextBox1.Text = "";
TextBox2.Text = "";
TextBox3.Text = "";
TextBox4.Text = "";
////用户不存在...do something..
return;

else

/////
///判断密码是否正确。。。。。。待补
string jackpwd;
jackpwd = "select name,pwd,email from denglu where name='" + username + "'and pwd='" + password + "'and email='" +useremail+ "'";
//执行SQL语句
OleDbCommand Cmd2 = new OleDbCommand(jackpwd, objconn);
//执行Datareader
OleDbDataReader dr2 = Cmd2.ExecuteReader();
if (dr2.Read())

////////////用户存在
/////添加 Session
//Session("adm") = TextBox1.Text;
Session["adm"] = TextBox1.Text;
Response.Redirect("kuangjia.aspx");

else

Response.Write("<script>alert('您输入的密码或者邮箱地址错误,请重新输入...');window.location='denglu.aspx';</script>");
TextBox1.Text = "";
TextBox2.Text = "";
TextBox3.Text = "";
TextBox4.Text = "";
Session["adm"] = "";


//
//添加登陆程序。。。
//

protected void Button3_Click(object sender, EventArgs e)

/////////////////////////////////////////////////////////////////////////////////////取消操作
//Response.Redirect("denglu.aspx");
TextBox1.Text = "";
TextBox2.Text = "";
TextBox3.Text = "";
TextBox4.Text = "";
本回答被提问者采纳

以上是关于请问asp.net中如何在后台代码里提交表单的主要内容,如果未能解决你的问题,请参考以下文章

如何多次提交表单(asp.net core razor)

asp.net提交表单到数据库中的中文数据是一些问号

asp.net中form表单多个按钮submit提交到后台的实例

如何使“div”按钮提交其所在的表单?

ASP.NET MVC 网站开发总结——Ajax异步提交表单之检查验证码

asp.net jquery ajax post 后台页面获取不到值 怎么解决?