一些随意的ASP.NET总结

9月 28th, 2009 | Posted by | Filed under 程序设计

本文内容遵从CC版权协议, 可以随意转载, 但必须以超链接形式标明文章原始出处和作者信息及版权声明
网址: http://www.penglixun.com/tech/program/some_asp_net_summary_2009_9.html

关于GridView刷新,当更新了数据库之后想立刻更新GridView,必须把数据源和GridView同时刷新。

 LinqDataSource_List.DataBind();
 GridView_List.DataBind();

 

关于GridView隐藏字段,因为绑定一个数据源的时候,我们经常把主键选出来,但是把它隐藏掉不想显示,可是隐藏了又不能获取值,很悲剧,现在有好东西了,DataKey,专门用来存主键。

GridView_List.DataKeys[row.RowIndex][0]

 

关于LinQ的条件查询,有时候想做一个多种类型查询的功能,下拉菜单选择查询类型,文本框输入关键字,现在有比较好的方法,就是Where参数。

<asp:LinqDataSource ID=”LinqDataSource_List” runat=”server” ContextTypeName=”DataClassesDataContext”
    Select=”new (CName, EName, OwnershipName, LinkmanName, IsReservation, Manufacturer, E_ID)”
    TableName=”View_Device” Where=””>
    <whereparameters>
        <asp:ControlParameter ControlID=”TextBox_Search” Name=”Match” PropertyName=”Text” Type=”String” DefaultValue='””‘ />
    </whereparameters>
</asp:LinqDataSource>

string str = DropDownList_SearchType.SelectedValue;
if (TextBox_Search.Text != “”)
{
    switch (str)
    {
        case “CName”:
            LinqDataSource_List.Where = “CName.Contains(@Match)”;
            break;
        case “EName”:
            LinqDataSource_List.Where = “EName.Contains(@Match)”;
            break;
        case “Manufacturer”:
            LinqDataSource_List.Where = “Manufacturer.Contains(@Match)”;
            break;
        case “OwnershipName”:
            LinqDataSource_List.Where = “OwnershipName.Contains(@Match)”;
            break;
        case “LinkmanName”:
            LinqDataSource_List.Where = “LinkmanName.Contains(@Match)”;
            break;
    }
}
else
{
    LinqDataSource_List.Where = “”;
}

 

关于弹出网页对话框,想点击某个控件触发弹出对话框事件,用JS好了。

string js = “window.showModalDialog(‘DeviceModify.aspx’, new Object(), ‘status:no;help:no’);”;
Button_Add.Attributes.Add(“onclick”, js);

 

关于LinQ的爽,LinQ还不是真正的持久层,已经让我爽死了,从SQL中解脱了,可想而知Hibernate用起来将有多爽。

var res = (from p in db.RBAC_User
               where p.UserName == user.UserName && p.UserPassword == user.UserPassword
               select p.UserPassword).Count();
       
       

关于iframe的src动态修改,没什么难的,加个runat=”Server”,一切都和谐了。

 <iframe id=”iFrame_Main” frameborder=”0″ name=”iFrame_Main” style=”width: 680px;height: 650px” src=”SystemNotice.aspx” runat=”Server”></iframe>

iFrame_Main.Attributes.Add(“src”, “DeviceManager.aspx”);

 

关于MutilView的多View,用来实现某些操作后后某些部分内容变化但是不想重做页面的需求。一个MutilView下多个View,都可以放入控件,然后更改ViewIndex实现视图变化。

 

关于验证码的实现,整个网页返回一个图片数据流,然后附加一个SESSION,通过显示网页数据流和捕捉SESSION,达成验证码。

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Drawing.Drawing2D;

public partial class ValidateCode : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string checkCode = CreateRandomCode(5);
        Session[“CheckCode”] = checkCode;
        CreateImage(checkCode);
    }
    private string CreateRandomCode(int codeCount)
    {
        string allChar = “0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,W,X,Y,Z”;
        string[] allCharArray = allChar.Split(‘,’);
        string randomCode = “”;
        int temp = -1;

        Random rand = new Random();
        for (int i = 0; i < codeCount; i++)
        {
            if (temp != -1)
            {
                rand = new Random(i * temp * ((int)DateTime.Now.Ticks));
            }
            int t = rand.Next(35);
            if (temp == t)
            {
                return CreateRandomCode(codeCount);
            }
            temp = t;
            randomCode += allCharArray[t];
        }
        return randomCode;
    }

    private void CreateImage(string checkCode)
    {
        int iwidth = 79;
        System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth, 20);
        Graphics g = Graphics.FromImage(image);
        Font f = new System.Drawing.Font(“Arial”, 12, System.Drawing.FontStyle.Bold);
        Brush b = new System.Drawing.SolidBrush(Color.Red);
        //g.FillRectangle(new System.Drawing.SolidBrush(Color.Blue),0,0,image.Width, image.Height);
        g.Clear(Color.White);
        g.DrawString(checkCode, f, b, 5, 3);

        Pen pen = new Pen(Color.Blue, (float)0.5);
        Random rand = new Random();
        for (int i = 0; i < 3; i++)
        {
            int x = rand.Next(image.Height);
            int y = rand.Next(image.Height);
            g.DrawLine(pen, 0, x, image.Width, y);
        }

        System.IO.MemoryStream ms = new System.IO.MemoryStream();
        image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
        Response.ClearContent();
        Response.ContentType = “image/Jpeg”;
        Response.BinaryWrite(ms.ToArray());
        g.Dispose();
        image.Dispose();
    }
}

<asp:Image ID=”Image1″ runat=”server”
                    Height=”20px” Width=”79px” ImageUrl=”~/ValidateCode.aspx” />

 

关于PowerDesigner的爽,世界上还有这么好的建模软件。对OOM建模生成代码无爱,但是对PDM建模生成SQL很有爱,非常好,传说中是华人创立的软件,不错,鼓励一下。

目前还没有任何评论.