如何从Ajax函数调用非静态方法?或者如何在静态Web方法中访问表单控件?

问题描述:

/ *代码* /



CompanyList.InnerHtml + =

< input type = button id = btn value =删除onclick ='delCompany(\+ id +\)'>
;



/ *静态方法来自.cs * /

/*code from inner html*/

CompanyList.InnerHtml += "

<input type=button id=btn value=Delete onclick='delCompany(\"" + id + "\")' >
";

/*static method from .cs*/
[WebMethod]
 public static void Update(int id)
        {
            string query = "select * from company_master where Company_Id="+id;
            SqlCommand cmmd = new SqlCommand(query, con);
            con.Open();
            SqlDataReader reader = cmmd.ExecuteReader();
            while (reader.Read())
            {
               
                CmpBuilderId.Text = reader["Builder_Id"].ToString();
                CmpName.Text = reader["Company_name"].ToString();
                CmpAddress.Text = reader["Address"].ToString();
                CmpPhone.Text = reader["Phone No."].ToString();
                Cmpemail.Text = reader["Email"].ToString();
                Cmpebsite.Text = reader["Website"].ToString();
                CmpAdded.Text = reader["AddedBy"].ToString(); 
                string str  = reader["Added Date"].ToString();
                string str1 = str.Substring(0, str.LastIndexOf(" 1") + 1);
                CmpAddedDate.Text = str1;
                CmpModify.Text = reader["ModifiedBy"].ToString(); 
                string str3 = reader["Modified Date"].ToString();
                string str4 = str3.Substring(0, str.LastIndexOf(" 1") + 1);
                CmpModifyDate.Text = str4;
                Cmpremark.Text = reader["Remark"].ToString();
               
            }
            con.Close();
}



静态功能不接受所有控制ID



客户端代码:




all control id's are not accepted by static function

clientside code:

function delCompany(id) {
            alert(id); 
        $.ajax
              ({
                  type: "POST",
                  url: "companymaster.aspx/Update",
                  data: "{'id':'" + id +"'}",
                  contentType: "application/json;charset=utf-8",
                  dataType: "json",
                  success: function (result) {
                      
                      alert(result.d);
                     
                  },
                  error: function (err) {

                  }
              });
    }





plz帮我解决这个问题



plz help me in this

.ajax
({
类型: POST
url: companymaster.aspx / Update
data: {'id':' + id + '}
contentType: application / json ; charset = utf-8
dataType: json
成功:功能(结果){

alert(result.d);

},
错误: function (错误){

}
});
}
.ajax ({ type: "POST", url: "companymaster.aspx/Update", data: "{'id':'" + id +"'}", contentType: "application/json;charset=utf-8", dataType: "json", success: function (result) { alert(result.d); }, error: function (err) { } }); }





plz帮我这个



plz help me in this


你无法访问表格通过webmethods或ajax控件进行控制,因为客户端浏览器上存在表单元素,并且您的webmethod正在服务器上运行。



要么使用可以为您处理此问题的updatepanel或者让你的webmethod以json格式返回数据,你的js代码将用它来更新客户端上的控件(可能是最好的解决方案),或让你的webmethod为新表单返回html并让html替换已经在的html页面。
You can't access form controls via webmethods or ajax controls as the form elements exist on the client browser and your webmethod is running on the server.

Either use an updatepanel which will handle this for you, or have your webmethod return data in json format that your js code will use to update the controls on the client (best solution probably), or have your webmethod return html for the new form and have that html replace the html that is already on the page.