我的问题是,如果我在(用户名,Countryid,Isactive)中提供值,并且我想以Json格式显示值 [英] My Question Is If I Give Values In (Username , Countryid , Isactive ) And I Want To Display The Values In Json Format
本文介绍了我的问题是,如果我在(用户名,Countryid,Isactive)中提供值,并且我想以Json格式显示值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的问题是如何以json格式输出.............
[WebMethod]
public string Inserttblval( int CountryId, string UserName, int IsActive)
{
int rowsInserted;
SqlConnection conn = new SqlConnection(你的connectionstring);
string sql = string .Format( @ INSERT INTO [TestDB]。[dbo]。[Table_1]([UserName],[CountryId],[IsActive])VALUES({0},'{1 }',{2}),UserName,CountryId,IsActive);
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = sql;
cmd.CommandType = CommandType.Text;
conn.Open();
rowsInserted = cmd.ExecuteNonQuery();
conn.Close();
cmd = null ;
return string .Format( 谢谢,{0}插入的行数!,rowsInserted);
}
aspx页面
< head runat = server >
< title > < / title >
< script 类型 = text / javascript < span class =code-attribute> sr c = http://ajax.googleapis.com/ajax/libs/jquery/1.4.2 /jquery.min.js\"> < / script >
< script type = text / javascript src = http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery- ui.min.js > < / script >
< script type = text / javascript >
$(功能(){
$([id * = Button1])。click(function(){
var obj = {};
obj.countryid = $ .trim($([id * = TextBox2])。val());
obj.username = $ .trim($([id * = TextBox1])。val());
obj.isactive = $ .trim($([id * = TextBox3])。val());
$ .ajax({
类型:POST,
url:Default.aspx / GetValues,
数据:JSON.stringify(obj),
contentType:application / json; charset = utf-8,
dataType:json,
success:function(r){
alert(rd);
document。 getElementById('Label1')。value = rd;
}
});
返回false;
});
});
< / script >
< / head >
< 正文 >
< 表格 id = form1 runat = server >
< div >
< table < span class =code-attribute> align = center class = auto-style1 >
< tr >
< td > 用户名< / td >
< td >
< asp:TextBox ID = TextBox1 runat = 服务器 >
< / td >
< td > < / td >
< / tr >
< tr > 温泉n>
< td > CountryId < / td >
< td >
< asp:TextBox ID = TextBox2 runat = server >
< / td >
< td > < / td >
< / tr >
< < span class =code-leadattribute> tr >
< td class = auto-style2 > IsActive < / td >
< td class = auto-style2 >
< asp:TextBox ID = TextBox3 runat = server >
< / td >
< td class = auto-style2 > < / td >
< / tr >
< span class =code-keyword>< tr >
< td > < / td >
< span class =code-keyword>< td >
< asp:按钮 ID = Button1 runat = server < span class =code-attribute> 文本 = 按钮 / > ;
< / td >
< td > < / td >
< / tr >
< tr >
< td > 结果:< / td >
< td >
< asp:Label ID = Label1 runat = server 文本 = >
&升t; / td >
< td > < ; / td >
< / tr >
< / table >
< span class =code-keyword>< / div >
< / form >
< / body >
cs代码
使用 System.Web.Services;
public partial class 默认:System.Web.UI.Page
{
protected void Page_Load(< span class =code-keyword> object sender,EventArgs e)
{
}
[WebMethod]
public static string GetValues( int countryid, string username, int isactive)
{
WebService ws = new WebService();
return ws.Inserttblval(countryid,username,isactive);
}
}
解决方案
(function() {
([id * = Button1])。click(function(){
var obj = {};
obj.countryid =
.trim(
My Question is how to get output in json format .............
[WebMethod]
public string Inserttblval(int CountryId, string UserName, int IsActive)
{
int rowsInserted;
SqlConnection conn = new SqlConnection(your connectionstring);
string sql = string.Format(@"INSERT INTO [TestDB].[dbo].[Table_1] ([UserName],[CountryId],[IsActive]) VALUES ({0},'{1}',{2})", UserName, CountryId, IsActive);
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = sql;
cmd.CommandType = CommandType.Text;
conn.Open();
rowsInserted = cmd.ExecuteNonQuery();
conn.Close();
cmd = null;
return string.Format("Thank you, {0} number of rows inserted!", rowsInserted);
}
aspx page
<head runat="server">
<title></title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js"></script>
<script type="text/javascript">
$(function () {
$("[id*=Button1]").click(function () {
var obj = {};
obj.countryid= $.trim($("[id*=TextBox2]").val());
obj.username= $.trim($("[id*=TextBox1]").val());
obj.isactive= $.trim($("[id*=TextBox3]").val());
$.ajax({
type: "POST",
url: "Default.aspx/GetValues",
data: JSON.stringify(obj),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (r) {
alert(r.d);
document.getElementById('Label1').value = r.d;
}
});
return false;
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table align="center" class="auto-style1">
<tr>
<td>UserName</td>
<td>
<asp:TextBox ID="TextBox1" runat="server">
</td>
<td> </td>
</tr>
<tr>
<td>CountryId </td>
<td>
<asp:TextBox ID="TextBox2" runat="server">
</td>
<td> </td>
</tr>
<tr>
<td class="auto-style2">IsActive</td>
<td class="auto-style2">
<asp:TextBox ID="TextBox3" runat="server">
</td>
<td class="auto-style2"></td>
</tr>
<tr>
<td> </td>
<td>
<asp:Button ID="Button1" runat="server" Text="Button" />
</td>
<td> </td>
</tr>
<tr>
<td>Result:</td>
<td>
<asp:Label ID="Label1" runat="server" Text="">
</td>
<td> </td>
</tr>
</table>
</div>
</form>
</body>
cs Code
using System.Web.Services;
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[WebMethod]
public static string GetValues(int countryid,string username,int isactive)
{
WebService ws = new WebService();
return ws.Inserttblval(countryid, username, isactive);
}
}
解决方案
(function () {
("[id*=Button1]").click(function () { var obj = {}; obj.countryid=
.trim(
这篇关于我的问题是,如果我在(用户名,Countryid,Isactive)中提供值,并且我想以Json格式显示值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文