Extjs /如何将数据从表单保存到数据库
本文关键字:表单 保存 数据库 数据 Extjs | 更新日期: 2023-09-27 18:07:47
我试图通过表单添加一个新用户到我的数据库我准备的形式,但我的问题是如何保存数据从这个形式到我的数据库?我使用c#和extjs4.0.1
我知道我需要在一个按钮上使用提交并给它一个url我这样做了,但是当我点击它什么也没有发生!没有错误!我得到了成功提示框!但是当我检查数据库时,没有插入任何内容!就像我的updateuser函数从来没有被调用过?!!
这是我的表单脚本:buttons: [{
text: 'Save user',
handler: function () {
if (this.up('form').getForm().isValid())
this.up('form').getForm().submit
({// this would submit the form to the configured url
url: 'update.ashx',
success: function (form, action) {
Ext.MessageBox.show({
title: 'Success !',
msg: 'Changes saved successfully<br />',
icon: Ext.MessageBox.INFO
})
},
});
this.up('form').getForm().reset();
}
,这是我的更新。ashx脚本:
public class update : IHttpHandler
{
SqlConnection dbConn = new SqlConnection("....");
public void ProcessRequest(HttpContext context)
{ }
public bool IsReusable
{
get { return false; }
}
public void updateUser(string firstname, string lastname, string email)
{
string str = "insert into User (Name , FName , Email ) values ( '" + firstname + "' , '" + lastname + "' , '" + email + "' )";
SqlCommand sqlCommand = new SqlCommand(str);
sqlCommand.Connection = dbConn;
sqlCommand.CommandType = CommandType.Text;
SqlDataAdapter sda = new SqlDataAdapter(sqlCommand);
dbConn.Open();
if (sqlCommand.Connection.State == ConnectionState.Open)
{
sqlCommand.ExecuteNonQuery();
dbConn.Close();
}
}
}
你有什么想法吗?至少让我知道我走的路对不对……由于
首先"success: function (form, action)"这个函数调用并不意味着更新用户是成功的,只是表示提交是成功的。为了成功,你需要编码一个json,其中success为true,然后action.result.success将决定success是否为true…检查API .....
第二,我不太会c#…但似乎你甚至不处理请求 编辑:要避免重复问题,请向处理程序发送额外的参数…并且只有在接收到请求
中的更新操作时才执行更新方法。url: 'update.ashx',
params: {action: 'update'},
success: function (form, action) {
表单submit将表单值发送到context对象中包含的HttpRequest对象中的处理程序。要检索表单值,您可以检查http://msdn.microsoft.com/en-us/library/system.web.ihttphandler.processrequest.aspx示例