使用javascript做頁面間傳值

來源:互聯網
上載者:User

大家都知道,Asp.Net中頁面傳值主要可以用Session、Application、Post、Get傳值。但是也可以通過javascript指令碼實現頁面傳值。因為大家經常在網頁上看到,比如叫你選擇日期啊,負責人等,它們有時候就是一個表徵圖,然後,你可以點擊這個表徵圖,彈出一個小頁面,然後你就可以在這個小頁面裡進行選擇了。當然,這也可以用下拉式清單等做的,今天,我給大家介紹一個使用javascript做頁面傳值。

第一個頁面有一項是負責人,然後後面是一個TextBox,TextBox後面是一個小表徵圖,點擊它就可以在一個小頁面裡進行選擇其中一個負責人,然後這個負責人的名字,就顯示在第一個頁面的TextBox中。

    選擇後:

第一個頁面前台代碼:

指令碼代碼

<script language="javascript">

function showmanager()

{

var w=window.open('ManagerSelect.aspx','新的頁面','scrollbars=yes,resizable=no,height=300,width=400');

}

</script>

圖片做成一個連結

<a onmouseover="window.status='選擇負責人';return true;" onmouseout="window.status='';return true;"

href="javascript:showmanager();"><IMG style="Z-INDEX: 103; LEFT: 344px; POSITION: absolute; TOP: 96px" alt="選擇負責人" src="images/tt111.jpg"></a>

第二個頁面

前台:

Javascript代碼

function setUser()

{

window.opener.Form1.TextBox1.value = document.Form1.TextBox1.value;

window.close();

//return true;

}

function closeWin()

{

window.close();

}

DataGrid代碼

<asp:DataGrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 40px; POSITION: absolute; TOP: 16px" runat="server"

Width="328px" Height="128px" PageSize="3" AllowPaging="True" AutoGenerateColumns="False">

<SelectedItemStyle BackColor="Fuchsia"></SelectedItemStyle>

<Columns>

<asp:TemplateColumn HeaderText="使用者名稱">

<ItemTemplate>

<asp:LinkButton id="LinkButton1" Text='<%# DataBinder.Eval(Container, "DataItem.LastName") %>' runat="server" >LinkButton</asp:LinkButton>

</ItemTemplate>

</asp:TemplateColumn>

<asp:BoundColumn DataField="FirstName" HeaderText="姓"></asp:BoundColumn>

<asp:BoundColumn DataField="BirthDate" HeaderText="生日"></asp:BoundColumn>

</Columns>

<PagerStyle Mode="NumericPages"></PagerStyle>

</asp:DataGrid>

後台代碼:

static DataSet ds;

private void Page_Load(object sender, System.EventArgs e)

{

// 在此處放置使用者代碼以初始化頁面

this.BtnConfirm.Attributes.Add("onclick","setUser()");

this.BtnClose.Attributes.Add("onclick", "closeWin()");

if(!this.IsPostBack)

{

this.bindtoDG();

}

}

//資料繫結到DataGrid中

private void bindtoDG()

{

SqlConnection con=new

SqlConnection("server=.;uid=sa;pwd=sa;database=Northwind");

con.Open();

SqlDataAdapter sda=new SqlDataAdapter();

sda.SelectCommand=new SqlCommand("select * from Employees",con);

DataSet ds1=new DataSet();

sda.Fill(ds1);

this.DataGrid1.DataSource=ds1.Tables[0].DefaultView;

ds=ds1;

this.DataBind();

con.Close();

}

//換頁

private void DataGrid1_PageIndexChanged(object source,

System.Web.UI.WebControls.DataGridPageChangedEventArgs e)

{

this.DataGrid1.CurrentPageIndex=e.NewPageIndex;

this.bindtoDG();

}

//選擇一項後讀取其中的資料

private void DataGrid1_ItemCommand(object source,

System.Web.UI.WebControls.DataGridCommandEventArgs e)

{

if((int)e.Item.ItemIndex<0) return;//沒有選擇時返回

this.DataGrid1.SelectedIndex=e.Item.ItemIndex;

//此頁面的TextBox1用來存放名字 用來傳給上一個頁面的TextBox1中

this.TextBox1.Text=ds.Tables[0].Rows[(int)e.Item.ItemIndex+this.DataGr

id1.CurrentPageIndex*this.DataGrid1.PageSize]["LastName"].ToString();

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.