熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

ASP.NET 窗體間傳值實現方法

2013-11-13 10:38:22  來源: .NET編程 

  假設ParentFormaspx 頁面上有TextBox文本框和Open按鈕

  點擊Open按鈕彈出SubFormaspxSubFormaspx頁面上有TextBox文本框和Close按鈕
點擊Close按鈕關閉SubFormaspx頁面並把子頁面SubFormaspx文本框的值顯示到父頁面ParentFormaspx 的文本框上

  父窗體前台代碼
 

 代碼如下         <script type="text/javascript">
        function OpenSubForm(ret) {
            var strPath = "subFormaspx"
            var nHeight =
            var nWidth =
            var feature
            feature = "Height= " + nHeight + "Width=" + nWidth + "top=Left=";
            feature += "dependent=yeslocation=noresizable=yesscrollbars=yesstatus=yestoolbar=no;";
            windowopen(strPath+"?Ret_Form=Form&Ret_Value="+retsubFormfeature)focus();
            return false;
        }
    </script>

  父窗體後台代碼 
 

 代碼如下   private void Page_Load(object sender SystemEventArgs e)
        {
            // ペ?ジを初期化するユ?ザ? コ?ドをここに?啡毪筏蓼?br />             thisButtonAttributesAdd("onClick""return OpenSubForm(TextBox);");
        }

  子窗體後台代碼 
 

 代碼如下  

  
        private void Button_Click(object sender SystemEventArgs e)
        {
            string strScript =stringEmpty;
            string strRetForm = StringEmpty;
            string strRetValue=StringEmpty;
            strRetForm=RequestParams["Ret_Form"];
            strRetValue=RequestParams["Ret_Value"];
            if (strRetForm == stringEmpty)
            {
                strRetForm= "documentforms[]";
            }
            strScript = "<script language=javascript>";
            strScript += "windowopener" + strRetForm;
            strScript += "" + strRetValue + "value=" + thisTextBoxTextTrim() + ";";
            strScript += "windowclose();";
            strScript += "</script>";
            ResponseWrite(strScript);
        }

  
 

  上面是js其實也就是頁面傳值了下面我把一些頁面傳值的代碼發給大家參考

  頁面間傳值的幾種方式

  下面的代碼片斷演示了如何實現這個方法
  源頁面WebFormaspxcs中的部分代碼

 代碼如下   private void Button_Click(object sender SystemEventArgs e)
{
     string url;
     url="WebFormaspx?name=" + TextBoxText + "&email=" + TextBoxText;
     ResponseRedirect(url);
}
 目標頁面WebFormaspxcs中的部分代碼
private void Page_Load(object sender SystemEventArgs e)
{
     LabelText=RequestQueryString["name"];
     LabelText=RequestQueryString["email"];
}

  使用Session變量

  源頁面WebFormaspxcs中的部分代碼

 代碼如下   private void Button_Click(object sender SystemEventArgs e)
{
     //textbox and textbox are webform
     //controls
     Session["name"]=TextBoxText;
     Session["email"]=TextBoxText;
     ServerTransfer("WebFormaspx");
}

  目標頁面WebFormaspxcs中的部分代碼

 代碼如下   private void Page_Load(object sender SystemEventArgs e)
{
     LabelText=Session["name"]ToString();
     LabelText=Session["email"]ToString();
     SessionRemove("name");
     SessionRemove("email");
}
From:http://tw.wingwit.com/Article/program/net/201311/14183.html
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.