Добрый день.
Когда мне нужно выполнить C# код на кнопке, то кнопку рисую в HTML (через редактор содержимого) и при помощи внедренного JavaScript, AJAX-запросом обращаюсь к BackEnd части, например, реализованной в IIS.
Внешне это выглядит примерно так:
try {
var rstObject = {};
rstObject.userLogin = userLogin;
$.ajaxSetup({cache: false});
$.ajax({
type: 'POST',
url: 'http://portal:84/getUserInfo.aspx/getInfo',
data: '{rstObject: ' + JSON.stringify(rstObject) + '}',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
async: false,
success: function(response) {
//response.d /* Работаем с откликом сервера */
},
error: function (jqXHR, exception) {
console.log('error');
}
})
} catch (e) {
console.log(e);
}
На сервере Portal, крутится крошечное web-приложение с обработчиком на C#, и состоит из aspx-части и aspx.cs части:
Например:
getUserInfo.aspx
<%@ Page language="c#" AutoEventWireup="true" CodeFile="getUserInfo.aspx.cs" Inherits="getUserInfo" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type = "text/css">
body
{
font-family:Arial;
font-size:10pt;
}
</style>
</head>
<body>
<div>
Hi!
</div>
</body>
</html>
и getUserInfo.aspx.cs
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.Services;
using System.Web.Script.Services;
public partial class getUserInfo : System.Web.UI.Page
{
[WebMethod]
[ScriptMethod]
public static string getInfo(RSTObject rstObject)
{
string r = "";
string constr = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
string queryString = "select SamAccountName, dep, grp from LDAP_EW where SamAccountName = '" + rstObject.userLogin + "'";
using (SqlCommand query = new SqlCommand(queryString))
{
query.Connection = con;
con.Open();
SqlDataReader reader = query.ExecuteReader();
try
{
while (reader.Read())
{
r = String.Format("{0}", reader[1]) + "#" + String.Format("{0}", reader[2]);
}
}
finally
{
reader.Close();
}
con.Close();
}
}
return r;
}
}
public class RSTObject
{
public string userLogin { get; set; }
}