How to set a dropdownlist item as selected in ASP.NET ?

from: http://stackoverflow.com/questions/10974992/how-to-set-a-dropdownlist-item-as-selected-in-asp-net

dropdownlist.Items.FindByValue(value).Selected = true;

廣告

取得檔案的唯一檢查碼Checksum MD5 SHA(轉貼)

取得檔案的唯一檢查碼Checksum MD5 SHA(轉貼)

最近因為在思考如何驗證取得的檔案與來源端相同,比對檔案大小是個方法,但不是很好,畢竟它也可能同大小,但內容不一樣,於是就想到Checksum這個名詞,大概找了一下網路上的資料,發現方法各有不同,所以算出來的結果也會不同,想來想去,反正來源端的checksum的運算方式只要同目的端,這樣就可以達成驗證的目標了.

這次用的方法其實也很簡單,不論是MD5或SHA1都不過兩三行的程式即可達成,這次就用VS2008的C#,來用WinForm做Demo,這個功能會用到加密的功能,及讀檔的部份,所以用到以下幾個namespace.

using System.IO;

using System.Security.Cryptography;

SHA Code :

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                using (FileStream fs = File.OpenRead(openFileDialog1.FileName))
                {
                    SHA256Managed sha = new SHA256Managed();
                    txtResult.Text = Convert.ToBase64String(sha.ComputeHash(fs));
                    txtPath.Text = openFileDialog1.FileName;
                }
            }

MD5 Code :
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                using (FileStream fs = File.OpenRead(openFileDialog1.FileName))
                {
                    MD5 m = MD5.Create();
                    txtPath.Text = openFileDialog1.FileName;
                    txtResult.Text = Convert.ToBase64String(m.ComputeHash(fs));
                }
            }

看一下MD5及SHA的程式碼幾乎一樣,但各會依據其加密方式取回checksum,如果檔案內容有所不同,則checksum就會不同,記得看過一篇討論,如果日期時間相同,就會產生相同的checksum,但從這程式碼中就可以發現,加密的來源是檔案內容,並不是檔案屬性,這也才是所要驗證的目的.

c# read binary data from sqlserver and output data-stream

From: http://dbtutorials.com/advanced/retrieving-binary-sql-cs.aspx

c# read binary data from sqlserver

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;

public partial class ShowImage : System.Web.UI.Page
{
	protected void Page_Load(object sender, EventArgs e)
	{
		try
		{
			int PictureID = Convert.ToInt32(Request.QueryString["ID"]);

			using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
			{
				const string SQL = "SELECT [MIME], [BinaryData] FROM [BinaryTable] WHERE [ID] = @ID";
				SqlCommand myCommand = new SqlCommand(SQL, myConnection);
				myCommand.Parameters.AddWithValue("@ID", PictureID);

				myConnection.Open();
				SqlDataReader myReader = myCommand.ExecuteReader();

				if (myReader.Read())
				{
					Response.ContentType = myReader["MIME"].ToString();
					Response.BinaryWrite((byte[]) myReader["BinaryData"]);
				}

				myReader.Close();
				myConnection.Close();
			}
		}
		catch (Exception ex)
		{
			Response.Write(ex.ToString());
		}
	}
}

C# 全型和半型字體相互轉換

參考來源:MSDN Strings.StrConv 方法

1. 加入參考 Microsoft.VisualBasic

2. 指示詞 using Microsoft.VisualBasic

3. 轉形 Strings.StrConv(Text1.text, VbStrConv.Narrow, 0).ToLower().Trim();

VbStrConv.Narrow :全形轉半形

VbStrConv.Wide:半形轉全形

範列程式

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.VisualBasic;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("半型「12345abcde」轉全型「" + Strings.StrConv("12345abcde", VbStrConv.Wide,0)+"」");
            Console.WriteLine("全型「12345abcde」轉半型「" + Strings.StrConv("12345abcde", VbStrConv.Narrow, 0) + "」");
            Console.ReadLine();
        }
    }
}

自訂類別 繼承System.Web.UI.Page 且 Override Page_load 或是 onload 讓你的網頁更加有彈性

資料來源:How to Execute Page_Load() in Page’s Base Class?

public class FactsheetBase : System.Web.UI.Page 
{ 

    public FactsheetBase()
    {
        this.Load += new EventHandler(this.Page_Load);
    }

    public MyPageData Data { get; set; }  
    protected void Page_Load(object sender, EventArgs e) 
    { 
        // get data that's common to all implementors of FactsheetBase 
        // and store the values in FactsheetBase's properties 
        this.Data = ExtractPageData(Request.QueryString["data"]);             
    } 
}
public class FactsheetBase : System.Web.UI.Page 
{ 

    public FactsheetBase()
    {
    }

    public MyPageData Data { get; set; }  
    protected override void OnLoad(EventArgs e)
    {
        //your code
        // get data that's common to all implementors of FactsheetBase 
        // and store the values in FactsheetBase's properties 
        this.Data = ExtractPageData(Request.QueryString["data"]);             

        base.OnLoad(e);
    }
}

如果在加上自已的一些function,像是是否已經登入、載入cookie值、資料庫連線設定、自訂資料庫取得資料的自訂Function、判斷是否有取的特定get的值,再做一些指定的工作。將一些必要的函式功能通通可以寫在這裏,就不用在每個Page各寫一次,造成維護上的麻煩。

如何在C#中使用字串來取得同名的變數值

如何在C#中使用字串來取得同名的變數值

using System;

namespace Test
{
	class MainClass
	{
		public int myInteger = 7;
		
		public void Test()
		{
                       // Grabs the integer by the variables name
			int test = (int)this.GetType().GetField("myInteger").GetValue(this);
			
			Console.WriteLine(test);
		}
		
		public static void Main(string[] args)
		{
			MainClass mc = new MainClass();
			
			mc.Test();
		}
	}
}