日期:2014-05-20  浏览次数:20540 次

[交流:活跃气氛]VS.NET开发中的小技巧
本贴旨在分享开发过程中的技巧,借鉴其它人的优点,少走弯路。为大家提供一些常见问题的解决方案。

例子:

技巧:图片使网站丰富起来,但有的时候不是所有图片都能正常显示的。这个时候会出现一个或多个红叉叉,这个技巧是告诉你如何处理的。
HTML code
<img src="hello.gif" alt="hello" onerror="this.src='error.gif'" />


--------------------------------------------

声明:本贴用于交流,不是接分帖子,禁止灌水。(请斑竹给予灌水、顶贴者严厉打击)
版权声明:本贴仅用于技术交流,任何书籍用到以下例子需注明出处。不尊重他人劳动可耻。

希望高手不吝赐教。

------解决方案--------------------
我也来一个:在DataGrid和GridView中对表头设定背景图片
C# code

在某些情况下,DataGrid或者GridView的默认样式并不能满足日益高涨的用户的要求,很多人追求美观的样式。对表头设定背景也是其中的一个方面,那么有什么好的方法可以达到这一要求呢,我的方法如下:
DataGrid:

private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
    if(e.Item.ItemType == System.Web.UI.WebControls.ListItemType.Header)
    {
        e.Item.Attributes.Add("style", "background-image:url('background.gif')");
    }
}
GridView:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.Header)
    {
        e.Row.Attributes.Add("style", "background-image:url('background.gif')");
    }
}

------解决方案--------------------
我也来一个:在DataGrid和GridView中对表头设定背景图片,不需要后台代码。

HTML code
<asp:GridView runat="server" ID="gvStatList" AllowPaging="false" Width="100%" CssClass="grid"
                AutoGenerateColumns="false" Visible="true" ShowFooter="false">
                <HeaderStyle CssClass="grid-head" />
</asp:GridView>

------解决方案--------------------
1.互斥对象.很多情况下存在互斥对象,我通常这么处理

C# code
bool showObject
{
set{
TextBox1.Visible=value;
TextBox2.Visible=!value;
}
}

------解决方案--------------------
C# code
<%=str%>
后台代码
变量
变量=@<img src='XXX' onclick='fucn(/)'>

            foreach (DataRow dr_Child in dsResult1.Tables[0].Rows)
            {
              
                if (!Convert.ToBoolean(dr_Child["FLAG"]))                
                   {
                    msiChild.sFontColor = "FontRed";
                    msiChild.BM = @"<img  src='/Images/NowPostion1.gif' />" + msiChild.BM;
                 }
                else if (Convert.ToBoolean(dr_Child["FLAG"]))
                {
                    msiChild.BM = @"<img  src='/Images/submit.gif' />" + msiChild.BM;
                }

                str += msiChild.BM;
                      }
            return str;

------解决方案--------------------
Asp.Net2.0中我们可以方便的访问配置文件中,.NetFrameWork2.0新增加了 SystemWebSectionGroup 类。
允许用户以编程方式访问配置文件的 system.web 组。
比如判断web.config内是否为 debug="true",或者判断身份验证形式

SystemWebSectionGroup ws = new SystemWebSectionGroup();
CompilationSection cp = ws.Compilation;
用cp.Debug;就可以得到compilation节内关于"debug"的配置
AuthenticationSection as = ws.Authentication; 
用 as.Mode 可以获取 authentication节中关于"mode"的配置,值为AuthenticationMode 枚举之一
AuthenticationMode的取值如下: 
成员名称 说明 
Forms 将基于 ASP.NET 窗体的身份验证指定为身份验证模式。
None 不指定身份验证。
Passport 将 Microsoft Passport 指定为身份验证模式。
Windows 将 Windows 指定为身份验证模式。在使用 Internet 信息服务 (IIS) 身份验证方法(基本、简要、集成 Windows (NTLM/Kerberos) 或证书)时适用此模式。


附:SystemWebSectionGroup 类的公共属性: