📄 ajaxmanager.cs
字号:
}
/* else if (CallBackAttemptCount == RegisterCount)
{
// If you're getting this error, it might be because you're calling
// back with the wrong control Id or you forget to register your page
// by calling Ajax.Manager.Register. You need to register your page
// during both GET and POST requests so don't hide your calls to
// Ajax.Manager.Register inside a "if (!IsPostBack)" block.
error = "Call back method not found.";
WriteResult(resp, val, error);
resp.End();
} */
}
}
static MethodInfo FindTargetMethod(Control control)
{
Type type = control.GetType();
string methodName = CallBackMethod;
MethodInfo methodInfo = type.GetMethod(methodName);
if (methodInfo != null)
{
object[] methodAttributes = methodInfo.GetCustomAttributes(typeof(AjaxMethodAttribute), true);
if (methodAttributes.Length > 0)
{
return methodInfo;
}
}
return null;
}
static object[] ConvertParameters(
MethodInfo methodInfo,
HttpRequest req)
{
object[] parameters = new object[methodInfo.GetParameters().Length];
int i = 0;
foreach (ParameterInfo paramInfo in methodInfo.GetParameters())
{
object param = null;
string paramValue = req.Form["Ajax_CallBackArgument" + i];
if (paramValue != null)
{
if (paramInfo.ParameterType.IsArray)
{
Type type = paramInfo.ParameterType.GetElementType();
string[] splits = paramValue.Split(',');
Array array = Array.CreateInstance(type, splits.Length);
for (int index = 0; index < splits.Length; index++)
{
array.SetValue(Convert.ChangeType(splits[index], type), index);
}
param = array;
}
else
{
param = Convert.ChangeType(paramValue, paramInfo.ParameterType);
}
}
parameters[i] = param;
++i;
}
return parameters;
}
static object InvokeMethod(
Control control,
MethodInfo methodInfo,
object[] parameters)
{
object val = null;
try
{
val = methodInfo.Invoke(control, parameters);
}
catch (TargetInvocationException ex)
{
// TargetInvocationExceptions should have the actual
// exception the method threw in its InnerException
// property.
if (ex.InnerException != null)
{
throw ex.InnerException;
}
else
{
throw ex;
}
}
return val;
}
public static void WriteResult(
HttpResponse resp,
object val,
string error)
{
resp.ContentType = "application/x-javascript";
resp.Cache.SetCacheability(HttpCacheability.NoCache);
StringBuilder sb = new StringBuilder();
try
{
WriteValueAndError(sb, val, error);
}
catch (Exception ex)
{
// If an exception was thrown while formatting the
// result value, we need to discard whatever was
// written and start over with nothing but the error
// message.
sb.Length = 0;
WriteValueAndError(sb, null, ex.Message);
}
resp.Write(sb.ToString());
}
static void WriteValueAndError(StringBuilder sb, object val, string error)
{
sb.Append("{\"value\":");
WriteValue(sb, val);
sb.Append(",\"error\":");
WriteValue(sb, error);
sb.Append("}");
}
static void WriteValue(StringBuilder sb, object val)
{
if (val == null || val == System.DBNull.Value)
{
sb.Append("null");
}
else if (val is string || val is Guid)
{
WriteString(sb, val.ToString());
}
else if (val is bool)
{
sb.Append(val.ToString().ToLower());
}
else if (val is double ||
val is float ||
val is long ||
val is int ||
val is short ||
val is byte ||
val is decimal)
{
sb.Append(val);
}
else if (val is DateTime)
{
sb.Append("new Date(\"");
sb.Append(((DateTime)val).ToString("MMMM, d yyyy HH:mm:ss", new CultureInfo("en-US", false).DateTimeFormat));
sb.Append("\")");
}
else if (val is DataSet)
{
WriteDataSet(sb, val as DataSet);
}
else if (val is DataTable)
{
WriteDataTable(sb, val as DataTable);
}
else if (val is DataRow)
{
WriteDataRow(sb, val as DataRow);
}
else if (val is IEnumerable)
{
WriteEnumerable(sb, val as IEnumerable);
}
else
{
WriteSerializable(sb, val);
}
}
static void WriteString(StringBuilder sb, string s)
{
sb.Append("\"");
foreach (char c in s)
{
switch (c)
{
case '\"':
sb.Append("\\\"");
break;
case '\\':
sb.Append("\\\\");
break;
case '\b':
sb.Append("\\b");
break;
case '\f':
sb.Append("\\f");
break;
case '\n':
sb.Append("\\n");
break;
case '\r':
sb.Append("\\r");
break;
case '\t':
sb.Append("\\t");
break;
default:
int i = (int)c;
if (i < 32 || i > 127)
{
sb.AppendFormat("\\u{0:X04}", i);
}
else
{
sb.Append(c);
}
break;
}
}
sb.Append("\"");
}
static void WriteDataSet(StringBuilder sb, DataSet ds)
{
sb.Append("{\"Tables\":{");
foreach (DataTable table in ds.Tables)
{
sb.AppendFormat("\"{0}\":", table.TableName);
WriteDataTable(sb, table);
sb.Append(",");
}
// Remove the trailing comma.
if (ds.Tables.Count > 0)
{
--sb.Length;
}
sb.Append("}}");
}
static void WriteDataTable(StringBuilder sb, DataTable table)
{
sb.Append("{\"Rows\":[");
foreach (DataRow row in table.Rows)
{
WriteDataRow(sb, row);
sb.Append(",");
}
// Remove the trailing comma.
if (table.Rows.Count > 0)
{
--sb.Length;
}
sb.Append("]}");
}
static void WriteDataRow(StringBuilder sb, DataRow row)
{
sb.Append("{");
foreach (DataColumn column in row.Table.Columns)
{
sb.AppendFormat("\"{0}\":", column.ColumnName);
WriteValue(sb, row[column]);
sb.Append(",");
}
// Remove the trailing comma.
if (row.Table.Columns.Count > 0)
{
--sb.Length;
}
sb.Append("}");
}
static void WriteEnumerable(StringBuilder sb, IEnumerable e)
{
bool hasItems = false;
sb.Append("[");
foreach (object val in e)
{
WriteValue(sb, val);
sb.Append(",");
hasItems = true;
}
// Remove the trailing comma.
if (hasItems)
{
--sb.Length;
}
sb.Append("]");
}
static void WriteSerializable(StringBuilder sb, object o)
{
MemberInfo[] members = o.GetType().GetMembers(BindingFlags.Instance | BindingFlags.Public);
sb.Append("{");
bool hasMembers = false;
foreach (MemberInfo member in members)
{
bool hasValue = false;
object val = null;
if ((member.MemberType & MemberTypes.Field) == MemberTypes.Field)
{
FieldInfo field = (FieldInfo)member;
val = field.GetValue(o);
hasValue = true;
}
else if ((member.MemberType & MemberTypes.Property) == MemberTypes.Property)
{
PropertyInfo property = (PropertyInfo)member;
if (property.CanRead && property.GetIndexParameters().Length == 0)
{
val = property.GetValue(o, null);
hasValue = true;
}
}
if (hasValue)
{
sb.Append("\"");
sb.Append(member.Name);
sb.Append("\":");
WriteValue(sb, val);
sb.Append(",");
hasMembers = true;
}
}
if (hasMembers)
{
--sb.Length;
}
sb.Append("}");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -