第一種方法:通過URL連結地址傳遞
send.aspx:
protected void Button1_Click(object sender, EventArgs e)
{
Request.Redirect("Default2.aspx?username=honge");
}
receive.aspx:
string username = Request.QueryString["username"];這樣可以得到參數值。
第二種方法:通過post方式。
send.aspx
<form id="form1" runat="server" action="receive.aspx" method=post>
<div>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
<asp:TextBox ID="username" runat="server"></asp:TextBox>
</div>
</form>
receive.aspx
string username = Ruquest.Form["receive"];
第三種方法:通過session
send.aspx:
protected void Button1_Click(object sender, EventArgs e)
{
Session["username"] = "honge";
Request.Redirect("Default2.aspx");
}
receive.aspx:
string username = Session["username"];這樣可以得到參數值。
第四種方法:通過Application
send.aspx:
protected void Button1_Click(object sender, EventArgs e)
{
Application["username"] = "honge";
Request.Redirect("Default2.aspx");
}
receive.aspx:
string username = Application["username"];這樣可以得到參數值。
第五種方法:通過Server.Transfer
send.aspx:
public string Name
{
get {
return "honge";
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Server.Transfer("Default2.aspx");
}
receive.aspx:
send d = Context.Handler as send ;
if (d != null)
{
Response.Write(d.Name);這樣可以得到參數值。
}
如果在asp.net 2.0中還可以這樣用:通過PreviousPage
PreviousPage d = Context.Handler as PreviousPage ;
if (d != null)
{
Response.Write(d.Name);這樣可以得到參數值。
}
也可以這樣用:
send.aspx:
<asp:Button ID="btnSubmit" runat="server" PostBackUrl="~/reveive.aspx" Text="Submit" />
receive.aspx:
<%@ PreviousPageType VirtualPath="~/Default.aspx" %>
string name = PreviousPage.Name;這樣可以得到參數值。
ivan.....
2016年7月12日 星期二
2016年1月8日 星期五
2015年10月15日 星期四
C# winForm --- Aform 開起其他程式[ Bform.exe ]並傳送參數;
Aform 開起其他程式[ Bform.exe ]並傳送參數;
1. 被開啟的程式[ Bform.exe ]
Program.cs 內容須修改為可接受參數;
static class Program
{
/// <summary>
/// 應用程式的主要進入點。
/// </summary>
static System.Threading.Mutex _mutex;
[STAThread]
static void Main(string[] args)
{
//是否可以打開新進程
bool createNew;
Attribute guid_attr = Attribute.GetCustomAttribute(Assembly.GetExecutingAssembly(), typeof(GuidAttribute));
string guid = ((GuidAttribute)guid_attr).Value;
_mutex = new System.Threading.Mutex(true, guid, out createNew);
if (false == createNew)
{
//發現重複進程
}
_mutex.ReleaseMutex();
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
if (args.Length == 0)
{
Application.Run(new Bform());
}
else
{
Application.Run(new Bform(args[0].ToString()));
}
}
}
/////////////////////////////////
Bform程式需新增
public Bform(string P) ///定義字串參數P;
{
InitializeComponent();
if (P != "") ///判斷有沒有參數;
{
label1.Text = P;
}
else
{
label1.Text = "";
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2. 執行端程式 [ Aform.exe ] 在按鍵中加入;
private void button_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("Bform.exe", "ABC");
}
////System.Diagnostics.Process.Start("程式.exe", "參數");
範例檔案
1. 被開啟的程式[ Bform.exe ]
Program.cs 內容須修改為可接受參數;
static class Program
{
/// <summary>
/// 應用程式的主要進入點。
/// </summary>
static System.Threading.Mutex _mutex;
[STAThread]
static void Main(string[] args)
{
//是否可以打開新進程
bool createNew;
Attribute guid_attr = Attribute.GetCustomAttribute(Assembly.GetExecutingAssembly(), typeof(GuidAttribute));
string guid = ((GuidAttribute)guid_attr).Value;
_mutex = new System.Threading.Mutex(true, guid, out createNew);
if (false == createNew)
{
//發現重複進程
}
_mutex.ReleaseMutex();
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
if (args.Length == 0)
{
Application.Run(new Bform());
}
else
{
Application.Run(new Bform(args[0].ToString()));
}
}
}
/////////////////////////////////
Bform程式需新增
public Bform(string P) ///定義字串參數P;
{
InitializeComponent();
if (P != "") ///判斷有沒有參數;
{
label1.Text = P;
}
else
{
label1.Text = "";
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2. 執行端程式 [ Aform.exe ] 在按鍵中加入;
private void button_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("Bform.exe", "ABC");
}
////System.Diagnostics.Process.Start("程式.exe", "參數");
範例檔案
C# winForm 儲存文字檔[ .txt ]的格式定義為[ Unicode ]
FileStream fs = new FileStream(@"C:\test.txt", System.IO.FileMode.OpenOrCreate);
StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.Unicode);
sw.Write("メールマガジン「.NETプログラミング研究」では");
sw.Flush();
sw.Close();
fs.Close();
StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.Unicode);
sw.Write("メールマガジン「.NETプログラミング研究」では");
sw.Flush();
sw.Close();
fs.Close();
C# winForm 子母視窗互傳參數的方式!!
Form1 開啟 Form2 並傳參數
1. 先在Form2 新增參數名稱及接收的方式
ex:
string _type;
public void _Type
{
get
{
return _type;
}
set
{
_type = value;
}
}
2. 在Form1 寫開啟 Form2 及傳參數
private void btn_Click(object sender, EventArgs e)
{
Form2 fm = new Form2();
fm._Type = "0";
//fm._Table = masterDGV.CurrentRow.Cells[0].Value.ToString();
//fm._Shop = _shopid;
//fm._Date = DateTime.Now.ToString("yyyyMMdd HH:mm:ss");
fm.ShowDialog();
}
///////////////////////////////////////////////////////////////////////////////////////////
Form2 子視窗關閉,將參數回傳 主視窗 Form1
1. 先在Form1 開啟子視窗時定義 Owner = this;
以及接收的參數及方式!
string _strValue;
public void _StrValue
{
get
{
return _strValue;
}
set
{
_strValue = value;
}
}
private void btnShow_Click(object sender, EventArgs e)
{
Form2 fm = new Form2();
fm.Owner = this;
//fm._Table = masterDGV.CurrentRow.Cells[0].Value.ToString();
//fm._Shop = _shopid;
//fm._Date = DateTime.Now.ToString("yyyyMMdd HH:mm:ss");
fm.ShowDialog();
TextBox1.text=_strValue; //關閉表單後返回參數填入TextBox1;
}
2. 在Form2 寫關閉視窗時回傳的方法
private void btnClose_Click(object sender, EventArgs e)
{
Form1 F1 = (Form1)this.Owner;
F1.StrValue = "Form2返回";;
this.Close();
}
1. 先在Form2 新增參數名稱及接收的方式
ex:
string _type;
public void _Type
{
get
{
return _type;
}
set
{
_type = value;
}
}
2. 在Form1 寫開啟 Form2 及傳參數
private void btn_Click(object sender, EventArgs e)
{
Form2 fm = new Form2();
fm._Type = "0";
//fm._Table = masterDGV.CurrentRow.Cells[0].Value.ToString();
//fm._Shop = _shopid;
//fm._Date = DateTime.Now.ToString("yyyyMMdd HH:mm:ss");
fm.ShowDialog();
}
///////////////////////////////////////////////////////////////////////////////////////////
Form2 子視窗關閉,將參數回傳 主視窗 Form1
1. 先在Form1 開啟子視窗時定義 Owner = this;
以及接收的參數及方式!
string _strValue;
public void _StrValue
{
get
{
return _strValue;
}
set
{
_strValue = value;
}
}
private void btnShow_Click(object sender, EventArgs e)
{
Form2 fm = new Form2();
fm.Owner = this;
//fm._Table = masterDGV.CurrentRow.Cells[0].Value.ToString();
//fm._Shop = _shopid;
//fm._Date = DateTime.Now.ToString("yyyyMMdd HH:mm:ss");
fm.ShowDialog();
TextBox1.text=_strValue; //關閉表單後返回參數填入TextBox1;
}
2. 在Form2 寫關閉視窗時回傳的方法
private void btnClose_Click(object sender, EventArgs e)
{
Form1 F1 = (Form1)this.Owner;
F1.StrValue = "Form2返回";;
this.Close();
}
2015年9月8日 星期二
C# 將DataTable寫入文字檔
public void SaveToCSV(DataTable oTable, string FilePath)
{
string data = "";
int i =0;
StreamWriter wr = new StreamWriter(FilePath, false, System.Text.Encoding.Default);
foreach (DataRow row in oTable.Rows)
{
foreach (DataColumn column in oTable.Columns)
{
data += row[column].ToString().Trim() + "," ; // 欄位間加入逗號 ;
}
data = data.TrimEnd(',') ; //取消最後一個逗號 ;
i++;
if (i < dt.Rows.Count)
{
data += "\r\n";
}
wr.Write(data);
data = "";
}
data += "\r\n";
wr.Dispose();
wr.Close();
}
訂閱:
文章 (Atom)