Click here to Skip to main content
15,922,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have following method on aspx.cs page.. I want to access textbox value inside binddatatable() method. but it is not available inside

C#
[WebMethod]
public static UserDetails[] BindDatatable()
{
      //Page page = HttpContext.Current.Handler as Page;
      //TextBox txtBegin = (TextBox)page.FindControl("txtEmail");
      //string s = txtBegin.Text;
      DataTable dt = new DataTable();
      List<userdetails> details = new List<userdetails>();
      string sSql = "EXECUTE dbo.admin_user_sp_update @in_type = 2";
      dt = EasyDBPoolcl.GetDataTable(sSql);
      foreach (DataRow dtrow in dt.Rows)
      {
            UserDetails user = new UserDetails();
            user.Email = dtrow["flag"].ToString();
            details.Add(user);
      }
      return details.ToArray();
}
public class UserDetails
{
      public string UserId { get; set; }
      public string UserName { get; set; }
      public string Location { get; set; }
      public string Email { get; set; }
}
Posted
Updated 9-Sep-13 0:26am
v2
Comments
Harshil_Raval 9-Sep-13 6:29am    
In webmethod you can not access any server side control. To access textbox value, just pass it in your function.
public static UserDetails[] BindDatatable(string email){//your code}
k@ran 9-Sep-13 6:48am    
i am calling this method from ajax like this.. so how will i pass parameter into it..

url: "../user/register.aspx/BindDatatable"
Harshil_Raval 10-Sep-13 0:54am    
Hi as you said you are using ajax method to call you can do it as
var value = $('#<%= txtEmail.ClientID %>').val();
$.ajax({
type: "POST",
url: "../user/register.aspx/BindDatatable",
contentType: "application/json; charset=utf-8",
data: "{'email': '" + value + "'}",
dataType: "json",
success: function (data) {

},
failure: function (data) {

}
});
// your server side method
public static UserDetails[] BindDatatable(string email){//your code}

Cant you use the textbox value to pass as a query string in the url?

var textBoxValue = $("#textboxId").val(); //or document.getElementbyId("textboxId").value();

url: "../user/register.aspx/BindDatatable/someId?=" + textBoxValue;

---

you will have to change the signature of the static method to take this parameter:

public static UserDetails[] BindDatatable(string someId)
{...}
 
Share this answer
 
 
Share this answer
 
 
Share this answer
 
In pure framework terms, a static method within the web page, can access only those properties which are static ones, and Textbox's text will require an instance method.

So the only way, and the proper design is to have the method accept the values as parameters and have those parameters passed from outside. In this case, they can be passed by reading in the value prior to calling the webmethod and then setting up the method parameter. Many of the above links illustrate how to do this in jQuery
 
Share this answer
 
 
Share this answer
 
Comments
k@ran 9-Sep-13 6:32am    
post is not helpful..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900