我有一个带有模板字段的DetailsView控件,如下所示:
<asp:TemplateField SortExpression="Title">
<ItemTemplate>
<asp:TextBox ID="txtMessageTitle" Text='<%# Bind("Title") %>' runat="server">
</asp:TextBox>
<asp:Button ID="btnUpdateTitle" runat="server" Text="Update"
CommandName="UpdateTitle" CommandArgument='<%# Bind("MessageID") %>' oncommand="btnUpdateCommand"/>
</ItemTemplate>
</asp:TemplateField>
Run Code Online (Sandbox Code Playgroud)
详细信息视图包装在UpdatePanel中。
当用户单击时,btnUpdateButton我希望能够txtMessageTitle在后面的代码中并通过使用按钮的CommandArgument 来检索文本框()值,请更新数据库中的相应MessageTitle。如何从按钮的Command事件检索DetailsView控件内的文本框值?谢谢。
实现自定义异常的优缺点如下:
创建一个枚举,在其描述中表示错误消息:
public class Enums
{
public enum Errors
{
[Description("This is a test exception")]
TestError,
...
}
}
Run Code Online (Sandbox Code Playgroud)
创建自定义异常类:
public class CustomException : ApplicationException
{
protected Enums.Errors _customError;
public CustomException(Enums.Errors customError)
{
this._customError = customError;
}
public override string Message
{
get
{
return this._customError!= Enums.Errors.Base ? this.customError.GetDescription() : base.Message;
}
}
}
Run Code Online (Sandbox Code Playgroud)
该GetDescription方法是枚举扩展方法,它使用反射获取枚举描述.这样,我可以抛出异常,如:
throw new customException(enums.Errors.TestError);
Run Code Online (Sandbox Code Playgroud)
并在catch块中向用户显示如下:
Console.WriteLn(ex.Message);
Run Code Online (Sandbox Code Playgroud)
我见过MVP推荐的这种方法.这种方法对以下方面有什么好处:
WebServiceException班级,AuthenticationException班级等) 谢谢.
我遇到了一个奇怪的问题.每次刷新页面IsPostBack都是假的.
为了确保我的内容或母版页中没有任何内容导致这种情况,我创建了一个空的Web表单并在调试模式下将其启动.仍然,在回发上,我已经IsPostBack设置为假.
我有enableSessionState="true"和<sessionState timeout="30" />web.config.
这让我疯了!
更新:我按F5刷新页面.根据答案,这不应该导致回发.我想知道什么时候使用刷新页面(甚至手动)并防止对db进行一些修改.
这有解决方案吗?
我正在使用Autofac在我的解决方案中实现IoC,但我怀疑我是否做得对.这是场景:
我有一些Manager类都来自BaseManager类.该BaseManager有一个protected User CurrentUser领域.我正在尝试做的是解决CurrentUser使用Autofac.我编写了一个IUserProvider接口并实现了几个类(例如WebUserProvider和WinformsUserProvider).
然后我在下面注册了我的提供者(例如,在Global.asax):
builder.Register(c => new WebUserProvider(...)).As<IUserProvider>();
Run Code Online (Sandbox Code Playgroud)
container在我的类中访问)?我可以使用单例或服务定位器模式,但看起来它是反模式.那么我应该如何解决我的依赖?dependency-injection ioc-container inversion-of-control autofac
我想定义一个派生自 System.Data.DataTable 的类。你可以猜到,
这个类有一个方法来填充数据表。PopulateColumns我希望此方法能够使用任意数量的自定义数据类型动态填充数据表列。(请参阅下面的代码以进行澄清)我尝试使用Dictionary<strin,Type>,而不是一一传递所有参数:
public void Populate(Dictionary<string, Type> dic)
{
foreach (var item in dic)
this.Columns.Add(item.Key, item.Value);
}
Run Code Online (Sandbox Code Playgroud)
并称其为:
var testDt = new TestDataTable();
Dictionary<string, Type> dicCols = new Dictionary<string, Type>();
dicCols.Add("Index", System.Type.GetType("System.Int32"));
dicCols.Add("Title", System.Type.GetType("System.String"));
testDt.Populate(dicCols);
Run Code Online (Sandbox Code Playgroud)
这很好用。但它不能接受具有相同内容的两列(因为列名称是字典中的键)。
我知道我不需要传递两个同名的列。但我只是好奇是否有更好的方法。
是否可以合并以下语句:
if (a != null)
{
if (a.Count > 5)
{
// Do some stuff
}
}
Run Code Online (Sandbox Code Playgroud)
只有1个if语句,并且当第一个条件不满足时,不要检查第二个条件.(比如AndAlsoVB.NET中的关键字).就像是:
if (a != null /* if it is null, don't check the next statement */ &&& a.Count > 5)
{
// Do some stuff
}
Run Code Online (Sandbox Code Playgroud) 我是如何使用c#获取活动目录用户属性列表(不是特定用户ieall属性)egcn,mail等的列表?
我正在定制BlogEngine.Net以支持自定义本地功能(波斯语日历,RTL主题等)
BlogEngine.Net使用mercurial源代码控制,我能够创建项目的分支并将其更改提交给它.但我想将它作为一个单独的项目发布,因为代码中有重大修改以支持波斯语,我不打算向主项目发送拉取请求.AFAIK我有两个选择:
现在使用叉子有什么好处?如果我创建一个fork,我是否能够像自己的项目(选项1)一样在自己的页面中释放我的项目?通过说"它自己的页面"我的意思是我希望有一个像"FarsiBlogengine.CodePlex.Com"这样的地址并制作二进制版本,可以下载源代码版本,有一个讨论论坛等.就像我有的功能一样当我不使用分叉时.
一般来说,使用分叉而不是下载主项目,创建新项目和承诺新项目的优点是什么?
谢谢.
我想检查属性是否是DbSet<T>使用反射的类型.
public class Foo
{
public DbSet<Bar> Bars { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
通过使用反射:
var types = Assembly.GetExecutingAssembly().GetTypes();
foreach (var type in types)
{
if (type.IsSubclassOf(typeof (Foo)) || type.FullName == typeof (Foo).FullName)
{
foreach (
var prop in Type.GetType(type.FullName).
GetProperties(BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Instance))
{
var propType = prop.PropertyType;
bool a = propType.IsAssignableFrom(typeof (DbSet<>));
bool b = typeof (DbSet<>).IsAssignableFrom(propType);
bool c = propType.BaseType.IsAssignableFrom(typeof (DbSet<>));
bool d = typeof (DbSet<>).IsAssignableFrom(propType.BaseType);
bool e = typeof (DbSet<>).IsSubclassOf(propType);
bool f = typeof …Run Code Online (Sandbox Code Playgroud) c# ×5
.net-3.5 ×2
asp.net ×2
.net ×1
asp.net-4.0 ×1
autofac ×1
codeplex ×1
comparison ×1
datatable ×1
detailsview ×1
exception ×1
fork ×1
ispostback ×1
ldap ×1
mercurial ×1
populate ×1
postback ×1
reflection ×1