.net之控件传值实例

上篇博客介绍了如何使用利用控件跨界面传值,这次来举例说明:

假设要做一个小程序,在选中日期,输入会议主题,选中参会人员以后,通过控件传到另一个界面,然后输出


c_set.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        议题:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <br />
        <br />
        日期:</div>
    <asp:Calendar ID="Calendar1" runat="server" BackColor="#FFFFCC" 
        BorderColor="#FFCC66" BorderWidth="1px" DayNameFormat="Shortest" 
        Font-Names="Verdana" Font-Size="8pt" ForeColor="#663399" Height="200px" 
        ShowGridLines="True" Width="220px" SelectedDate="10/26/2015 19:49:22">
        <DayHeaderStyle BackColor="#FFCC66" Font-Bold="True" Height="1px" />
        <NextPrevStyle Font-Size="9pt" ForeColor="#FFFFCC" />
        <OtherMonthDayStyle ForeColor="#CC9966" />
        <SelectedDayStyle BackColor="#CCCCFF" Font-Bold="True" />
        <SelectorStyle BackColor="#FFCC66" />
        <TitleStyle BackColor="#990000" Font-Bold="True" Font-Size="9pt" 
            ForeColor="#FFFFCC" />
        <TodayDayStyle BackColor="#FFCC66" ForeColor="White" />
    </asp:Calendar>
    <asp:ListBox ID="ListBox1" runat="server" SelectionMode="Multiple">
        <asp:ListItem>111</asp:ListItem>
        <asp:ListItem>11</asp:ListItem>
        <asp:ListItem>1</asp:ListItem>
    </asp:ListBox>
    <br />
    <br />
    <asp:Button ID="Button1" runat="server" Text="Button" 
        PostBackUrl="~/Default2.aspx" />
    </form>
</body>
</html>

c_read.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        <br />
        <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
        <br />
        <asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>
    
    </div>
    </form>
</body>
</html>

c_read.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(PreviousPage!=null)
            if (PreviousPage.IsCrossPagePostBack)
            {
                TextBox txtbox = (TextBox)PreviousPage.FindControl("TextBox1");
                Label1.Text = txtbox.Text;

                Calendar cal = (Calendar)PreviousPage.FindControl("Calendar1");
                Label2.Text = cal.SelectedDate.ToLongDateString();

                ListBox li = (ListBox)PreviousPage.FindControl("ListBox1");
                Label3.Text = getmember(li);
            }
    }
    protected string getmember(ListBox li)
    {
        string result = "";
        foreach (ListItem listitem in li.Items)
        {
            if (listitem.Selected)
            {
                result += listitem.Text + ",";
            }
        }

        return result;
    }
}
运行效果:

.net之控件传值实例_第1张图片

.net之控件传值实例_第2张图片



你可能感兴趣的:(.net之控件传值实例)