65.9K
CodeProject 正在变化。 阅读更多。
Home

如何将值从用户 Web 控件传递到父 ASPX 页面

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.53/5 (6投票s)

2008年2月23日

CPOL
viewsIcon

87110

如何在没有 JavaScript 的情况下,在用户控件和 ASPX 页面之间传递值。

引言

我正在开发一个用户控件,试图将值从弹出式用户控件传递到父页面。使用 JavaScript 传递值很容易,但很难找到一种不使用 JavaScript 将值传递到父页面的方法。我找到了一种方法,并希望其他开发人员也能了解一下。

背景

在用户控件和 ASPX 页面之间传递值,无需 JavaScript 的帮助。

Using the Code

  1. 创建一个 Web 项目
  2. 将用户控件添加到项目
  3. 接下来,查看代码块
  4. 添加这些代码行
    private DataSet data;
    // declare a delegate
    public delegate void U_ListCommandEventHandler
    		(object sender, U_ListCommandEventArgs e);
    //Event
        public event U_ListCommandEventHandler UGridViewChanged;
    //selected index property
      public int SelectedIndex
        {
            get { return UGridView.SelectedIndex; }
            set
            {
                if (!Page.IsPostBack)
                {
                    GVData_Bind();
                }
                if (value < UGridView.Rows.Count)
                {
                    UGridView.SelectedIndex = value;
                    data = U_DataSet
    		(UGridView.Rows[UGridView.SelectedIndex].Cells[1].Text);
                    OnUGridViewChanged(new U_ListCommandEventArgs(data));
                }
            }
        }
    //Method to load data in to dataset
     private DataSet GVData_Bind()
        {
            SqlConnection Con = new SqlConnection(Cn);
            SqlDataAdapter adapter = new SqlDataAdapter();
            adapter.SelectCommand = new SqlCommand("SELECT * FROM Person", Con);
            data = new DataSet();
            adapter.Fill(data, "U_Table");
            return data;
        }
     protected void Page_Load(object sender, EventArgs e)
        {
          // load data to the grid view
        if (!IsPostBack)
            {
            data= GVData_Bind();
            UGridView.DataSource = data;
            UGridView.DataBind();
        }
    }
    //Method to return DataSet base on id
      private DataSet U_DataSet(string id)
        {
            SqlConnection Con = new SqlConnection(Cn);
            SqlDataAdapter adapter = new SqlDataAdapter();
            adapter.SelectCommand = new SqlCommand
    		("SELECT * FROM Person where Id='" + id + "'", Con);
            data = new DataSet();
            adapter.Fill(data, "U_Table");
            return data;
        }
    //method for selected index
     protected void UGridView_SelectedIndexChanged(object sender, EventArgs e)
        {
            data = U_DataSet(UGridView.Rows[UGridView.SelectedIndex].Cells[1].Text);
            OnUGridViewChanged(new U_ListCommandEventArgs(data));
    
        }
    //virtual method
      protected virtual void OnUGridViewChanged(U_ListCommandEventArgs e)
        {
            if (UGridViewChanged != null) UGridViewChanged(this, e);
        }
    // Add this class to the project
    public class U_ListCommandEventArgs
    {
        private DataSet _U_DataSet;
    
    	public U_ListCommandEventArgs(DataSet U_DataSet)
    	{
            _U_DataSet = U_DataSet;
    
    	}
        public DataSet U_DataSet { get { return _U_DataSet; } }
    
    }
    //also Add this
    public class U_ListCommandEventHandler
    {
    	public U_ListCommandEventHandler()
    	{
    		
    	}
    }
  5. 将新构建的用户控件拖放到 ASPX 页面上。
  6. 添加或更新 OnInit() 方法
    protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            GridViewUsercontrol1.UGridViewChanged+=
    		new GridViewUsercontrol.U_ListCommandEventHandler
    		(GridViewUsercontrol1_UGridViewChanged);        
        }
    //since we are getting dataset back from user control 
    //we can bind it to the gridview.
    private void GridViewUsercontrol1_UGridViewChanged
    		(object sender, U_ListCommandEventArgs e)
        {
            GridView1.DataSource = e.U_DataSet;
            GridView1.DataBind();
        }

历史

  • 2008 年 2 月 23 日:初始发布
© . All rights reserved.