与下拉和按钮,触发自己的事件服务器控件 [英] Server Control with dropdown and button, and trigger own event

查看:99
本文介绍了与下拉和按钮,触发自己的事件服务器控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含一个按钮和一个下拉列表中选择C#服务器控件
我想下拉的selectedIndex RESPONSE.WRITE,同时按一下按钮的页面
,我不想做的网页级别(CSS)的事件处理程序,但我想code将其控制内,作为编译DLL

我的流程是:
构建按钮和下拉,其控制类负载
覆盖的CreateChildControls来添加按钮到服务器控件的click事件绑定到它
重写RenderControl的下拉列表添加到一个表,然后呈现按钮

终于让我找到了按钮事件可以点击,但它只是不能得到下拉列表中选择项目,当我选择第二个

这里是code:

 公共类ServerControl1:WebControl的,作INamingContainer
    {
            公共ServerControl1()
            {
                _oBtn =新按钮();
                _oBtn.ID =BTN;
                _oBtn.Text =点击我;
                _oBtn.Click + =新的EventHandler(_oBtn_Click);
                _ddl =新的DropDownList();
                _ddl.ID =DDL
                _ddl.Items.add(新的ListItem(XXXXXXXX,XXXXXXXX))
                _ddl.Items.add(新的ListItem(YYYYYYY,YYYYYYY))
            }            保护覆盖无效的CreateChildControls()
            {
                this.Controls.Add(_oBtn);
                base.CreateChildControls();
            }             公共覆盖无效RenderControl(HtmlTextWriter的作家)
            {
                的AddAttributesToRender(作家);
                表m_oTable =新表();
                的TableRow m_oRow;
                TableCell的m_oCell;
                m_oCell =新的TableCell();
                m_oCell.Controls.Add(_ddl);
                m_oRow.Cells.Add(m_oCell);
                m_oTable.Rows.Add(m_oRow);
                m_oTable.RenderControl(作家);
                _oBtn.RenderControl(作家);
            }            保护无效_oBtn_Click(对象发件人,EventArgs的发送)
            {
                如果(_ddl.SelectedIndex!= 0)
                {
                    Page.Response.Redirect(URL +&放大器; F0 =+ _ddl.SelectedIndex);
                }
                其他
                {
                    Page.Response.Write(nonononon);
                }
            }
    }


解决方案

为了向动态添加控件preserve状态(ViewState中)(按钮,DropDownList的),你必须确保它们被添加到控制树的层次结构。

   - >页
      - > WebControl的
            - >按键
            - >下拉列表

初​​始化一个WebControl的子控件的正确方法是在Init事件。

  ///<总结>
    ///控件初始化
    ///< /总结>
    ///< PARAM NAME =E>< /参数>
    保护覆盖无效的OnInit(EventArgs的发送)
    {
         base.OnInit(E);
         _oBtn =新按钮();
         _ddl =新的DropDownList();
         m_oTable =新表();
         m_oRow =新的TableRow();
         m_oCell =新的TableCell();         _oBtn.ID =BTN;
         _oBtn.Text =点击我;
         _oBtn.Click + =新的EventHandler(_oBtn_Click);
         _ddl.ID =DDL
         _ddl.Items.Add(新的ListItem(XXXXXXXX,XXXXXXXX));
         _ddl.Items.Add(新的ListItem(YYYYYYY,YYYYYYY));
         _ddl.EnableViewState = TRUE;
         _ddl.AutoPostBack = FALSE;    }

如果IsPostaback比对照的Load事件之前,子控件的状态从ViewState中恢复(例如:当前按钮文本和选择的索引设置)。

下一步是在的CreateChildControls 方法控件树层次

添加此子控件

 保护覆盖无效的CreateChildControls()
        {            m_oCell.Controls.Add(_ddl);
            m_oRow.Cells.Add(m_oCell);
            m_oTable.Rows.Add(m_oRow);
            this.Controls.Add(_oBtn);
            this.Controls.Add(m_oTable);
            base.CreateChildControls();
        }

和渲染控制。您的必须避免的初始化或在这一点上添加控件:

 公共覆盖无效RenderControl(HtmlTextWriter的作家)
        {
            m_oTable.RenderControl(作家);
            _oBtn.RenderControl(作家);
            _txt.RenderControl(作家);        }

i have a C# server control which contains one button and one dropdown i want to response.write the dropdown selectedindex to the page while the button click and i dont want to make the event handler on page level (aspx), but i want to code it inside the control and compile as dll

my flow is : construct the button and dropdown, which the control class is load override CreateChildControls to add the button into the server control and bind the click event to it override the RenderControl to add the dropdown to a table, and then render the button

finally i found that the button event can be click, but it just cant get the dropdown selected item, when i select the second one

here is the code :

    public class ServerControl1  : WebControl, INamingContainer
    {       
            public ServerControl1()
            {
                _oBtn = new Button();
                _oBtn.ID = "btn";
                _oBtn.Text = "Click Me";
                _oBtn.Click += new EventHandler(_oBtn_Click);
                _ddl = new DropDownList();
                _ddl.ID = "ddl";
                _ddl.Items.add(new ListItem("xxxxxxxx", "xxxxxxxx"))
                _ddl.Items.add(new ListItem("yyyyyyy", "yyyyyyy"))
            }

            protected override void CreateChildControls()
            {
                this.Controls.Add(_oBtn);
                base.CreateChildControls();
            }

             public override void RenderControl(HtmlTextWriter writer)
            {
                AddAttributesToRender(writer);
                Table m_oTable = new Table();
                TableRow m_oRow;
                TableCell m_oCell;
                m_oCell = new TableCell();
                m_oCell.Controls.Add(_ddl);
                m_oRow.Cells.Add(m_oCell);
                m_oTable.Rows.Add(m_oRow);
                m_oTable.RenderControl(writer);
                _oBtn.RenderControl(writer);
            }

            protected void _oBtn_Click(object sender, EventArgs e)
            {
                if (_ddl.SelectedIndex != 0)
                {
                    Page.Response.Redirect(Url + "&f0=" + _ddl.SelectedIndex);
                }
                else
                {
                    Page.Response.Write("nonononon");
                }
            }
    }

解决方案

In order to preserve the state (ViewState) of the dynamically added controls (button, dropdownlist), you have to make sure they are added to the Control Tree hierarchy.

 -> Page  
     -> WebControl
           -> Button
           -> DropdownList

The proper way to initialize the Child controls in a WebControl is in the Init event.

    /// <summary>
    /// Initialization of controls
    /// </summary>
    /// <param name="e"></param>
    protected override void OnInit(EventArgs e)
    {        
         base.OnInit(e);
         _oBtn = new Button();
         _ddl = new DropDownList();       
         m_oTable = new Table();
         m_oRow = new TableRow();
         m_oCell = new TableCell();

         _oBtn.ID = "btn";
         _oBtn.Text = "Click Me";
         _oBtn.Click += new EventHandler(_oBtn_Click);


         _ddl.ID = "ddl";            
         _ddl.Items.Add(new ListItem("xxxxxxxx", "xxxxxxxx"));
         _ddl.Items.Add(new ListItem("yyyyyyy", "yyyyyyy"));
         _ddl.EnableViewState = true;
         _ddl.AutoPostBack = false;        

    }

If IsPostaback than before the Load event of the control, the state of the Child controls is restored from the ViewState (ex: current button text and selected index are set).

Next step is to add this child controls in the Control Tree hierarchy in the CreateChildControls method

protected override void CreateChildControls()
        {

            m_oCell.Controls.Add(_ddl);
            m_oRow.Cells.Add(m_oCell);
            m_oTable.Rows.Add(m_oRow);
            this.Controls.Add(_oBtn);             
            this.Controls.Add(m_oTable);
            base.CreateChildControls();           
        }

and to render the control. You have to avoid initializing or adding controls at this point on:

 public override void RenderControl(HtmlTextWriter writer)
        {
            m_oTable.RenderControl(writer);
            _oBtn.RenderControl(writer);
            _txt.RenderControl(writer);

        }

这篇关于与下拉和按钮,触发自己的事件服务器控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆