Tech
Forums
Jobs
Books
Events
Live
Interviews
More
Learn
Training
Career
Members
Videos
News
Blogs
Login
Sign Up
Ask Question
1
Reply
Jquery how to pass GridView as a parameter to a WebMethod
Chriz L
Sep 15 2016 2:16 AM
644
Reply
Hello,
I'm trying to pass a GridView as parameter from Javascript to a WebMethod. Here's my code:
<script src=
"http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"
type=
"text/javascript"
></script>
<script type =
"text/javascript"
>
function bindGrid() {
var pageurl =
'<%=ResolveUrl("Default.aspx/bindGrid1") %>'
;
var gv = $(
"#<%=gvDisplay.ClientID%>"
).val();
var idno = $(
"#<%=txtIDNO.ClientID%>"
).val();
var name = $(
"#<%=txtName.ClientID%>"
).val();
var tel = $(
"#<%=txtTel.ClientID%>"
).val();
var parameter={
"gv"
:gv,
"idno"
:idno,
"name"
:name,
"tel"
:tel}
$.ajax({
type:
"POST"
,
url: pageurl,
data: JSON.stringify(parameter),
contentType:
"application/json; charset=utf-8"
,
dataType:
"json"
,
success: OnSuccess,
failure: function(response) {
alert(response.d);
}
});
}
function OnSuccess(response) {
alert(response.d);
}
</script>
and the WebMethod:
[WebMethod]
public
static
string
bindGrid1(GridView gv, String idno, String name, String tel)
{
string
message =
""
;
DataSet ds;
SqlDataAdapter SqlAda;
SqlConnection con = getConnection();
SqlCommand cmd =
new
SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText =
"sproc"
;
cmd.Parameters.Add(
"@IDNO"
, SqlDbType.VarChar).Value = idno;
cmd.Parameters.Add(
"@Name"
, SqlDbType.VarChar).Value = name;
cmd.Parameters.Add(
"@Tel"
, SqlDbType.VarChar).Value = tel;
cmd.Connection = con;
try
{
con.Open();
gv.EmptyDataText =
"No data."
;
SqlAda =
new
SqlDataAdapter(cmd);
ds =
new
DataSet();
SqlAda.Fill(ds);
gv.DataSource = ds;
gv.DataBind();
message =
"success"
;
}
catch
(Exception ex)
{
appObj.myMessageBox(ex.Message);
message =
"error"
;
}
finally
{
con.Close();
con.Dispose();
}
return
message;
}
I know I can't pass it as a string, but I can't find a way to pass it.
Any kind of help would be appreciated.
Thank you in advance.
Upload Source Code
Select only zip and rar file.
Post
Reset
Cancel
Answers (
1
)
Next Recommended Forum
how to convert object to json and json to object?
how to get html value into c# page