Ibrahim Shaikh Ibrahim Shaikh - 2 months ago 14
ASP.NET (C#) Question

how to pass parameters from textbox to web service using angular

I am creating a web app in which i am getting my data from

webservice
using
JSON
now i can use json and webservice where i dont need where clause but now i want a particular data(like i just want to see record number (1) in 100 records) how to do it

here is my web service where i declare a string as id

[WebMethod]
public void GetEmployessJSON(string id)
{
SqlConnection con = new SqlConnection(connectionstring);
List<object> list = new List<object>();
SqlCommand cmd = new SqlCommand("select * from authors where AuthorId='"+id.ToString()+"'", con);

con.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
list.Add(new
{
AuthorId = dr["AuthorId"],
Fname = dr["Fname"],
Lname = dr["Lname"]
});
}
con.Close();
var jsonSerialiser = new JavaScriptSerializer();
var json = jsonSerialiser.Serialize(list);

Context.Response.Write("{" + '"' + "info" + '"' + ":" + json + "}");

}


and the id will come from the textbox in aspx page
this is my aspx page

<form id="form1" runat="server">
<asp:TextBox ID="txtid" runat="server"></asp:TextBox>
<div>
<div ng-app="myApp" ng-controller="customersCtrl">
<table class="table">
<tr class="bg-primary">
<th>Author ID</th>
<th>Fname</th>
<th>Lname</th>
<th>Action</th>
</tr>
<tr ng-repeat="x in names">
<td>{{ x.AuthorId }}</td>
<td>{{ x.Fname }}</td>
<td>{{ x.Lname }}</td>
<td><asp:Button ID="btndelete" runat="server" Text="Delete" /></td>
</tr>
</table>
</div>

</div>
<div id="Java Scripts">
<script src="JavaScript.js"></script>
</div>
</form>


this is my js

var app = angular.module('myApp', []);
app.controller('customersCtrl', function ($scope, $http) {
$http.post("testservice.asmx/GetEmployessJSON")
.then(function (response) {
$scope.names = response.data.info;
console.log(response.data.info);
});
});

Answer

You Need To Create another method in JS and pass id(which you used in webservice) in that js file and declare a variable pass the id to variable (var id) and give pass the value of the variable into the textbox {{id}} and on button click event you can do what you wants

Comments