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

DataGrid中的按鈕反選事件

2013-11-13 10:14:55  來源: .NET編程 

  DataGrid中想實現這樣的效果
根據某一字段列的值動態改變按鈕的文本比如
點擊按鈕列自動更新某列原為的值為並將按鈕列的文本改為
再按下自動更新某列原為的值為並將按鈕列的文本改為

  最終通過NamingContainer實現! 方法如下




<asp:DataGrid id=DataGrid runat=server AutoGenerateColumns=False>
                <Columns>
                    <asp:BoundColumn DataField=HonoreeID HeaderText=ID></asp:BoundColumn>
                    <asp:BoundColumn DataField=status HeaderText=狀態>
                        <HeaderStyle Width=px></HeaderStyle>
                    </asp:BoundColumn>
                    <asp:TemplateColumn HeaderText=狀態是否為>
                        <HeaderStyle HorizontalAlign=Center Width=%></HeaderStyle>
                        <ItemStyle HorizontalAlign=Center></ItemStyle>
                        <ItemTemplate>
                            <asp:Label id=lb runat=server Visible=false Text=<%# ((DataBinderEval(Container DataItemstatus {}))==)?:<font color=red>否</font> %>>
                            </asp:Label>
                            <asp:Button ID=changeState Runat=server Text=<%# ((DataBinderEval(Container DataItemstatus {}))==)?轉為:轉為 %>>
                            </asp:Button>
                        </ItemTemplate>
                    </asp:TemplateColumn>
                </Columns>
            </asp:DataGrid>


後台



protected SystemWebUIWebControlsDataGrid DataGrid;
   
        public int  KeyID 
        {
            get
            {
                object o =ViewState [KeyID];
                if(o!=null)
                {
                    return intParse(ViewState [KeyID]ToString());
                }
                else
                {
                    return ;
                }
            }
            set
            {
                ViewState [KeyID]        = value;
            }
        }
        public int  RowState 
        {
            get
            {    return intParse(ViewState [RowState]ToString());
               
            }
            set
            {
                ViewState [RowState]        = value;
            }
        }
        private void Page_Load(object sender SystemEventArgs e)
        {
            if(IsPostBack)
            {return ;
            }
            getData();
        }


        private void getData()
        {
            //SqlConnection con = new SqlConnection(ConfigurationSettingsAppSettings[DsnPubs]);
   
            SqlConnection con = new SqlConnection(SystemConfigurationConfigurationSettingsAppSettings[Mblog]);
            SqlCommand  cmd;
            conOpen();
            cmd = new SqlCommand(select * from dboHonoree con);
            DataGridDataSource = cmdExecuteReader();
            DataGridDataBind();
            conClose();
        }

        private bool UpdateData(int IDint OldState)
        {
            SqlConnection con = new SqlConnection(SystemConfigurationConfigurationSettingsAppSettings[Mblog]);
            SqlCommand  cmd;
            conOpen();
            try
            {
                string strSql=Update Honoree set Status={} where HonoreeID={};
                strSql=stringFormat(strSql(OldState==?:)ToString()IDToString());
                cmd = new SqlCommand(strSql con);
                cmdExecuteNonQuery();
                cmdDispose();
                return true;
            }
            catch
            {
                return  false;
            }
            finally
            {
                conClose();
                conDispose();
               
            }
            return  false;
        }


        Web 窗體設計器生成的代碼#region Web 窗體設計器生成的代碼
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: 該調用是 ASPNET Web 窗體設計器所必需的
            //
            InitializeComponent();
            baseOnInit(e);
        }
       
        /**//// <summary>
        /// 設計器支持所需的方法  不要使用代碼編輯器修改
        /// 此方法的內容
        /// </summary>
        private void InitializeComponent()
        {   
            thisDataGridItemCreated += new SystemWebUIWebControlsDataGridItemEventHandler(thisDataGrid_ItemCreated);
            thisDataGridItemDataBound += new SystemWebUIWebControlsDataGridItemEventHandler(thisDataGrid_ItemDataBound);
            thisLoad += new SystemEventHandler(thisPage_Load);

        }
        #endregion

        private void DataGrid_ItemCreated(object sender SystemWebUIWebControlsDataGridItemEventArgs e)
        {
            if(eItemItemType==ListItemTypeItem  || eItemItemType    ==    ListItemTypeAlternatingItem)
            {
                Button b=(Button)eItemFindControl(changeState);
                if(b!=null)
                {
                   
                    bClick+=new EventHandler(b_Click);
                }
            }
       
        }

        private void DataGrid_ItemDataBound(object sender SystemWebUIWebControlsDataGridItemEventArgs e)
        {
           
        }

        private void b_Click(object sender EventArgs e)
        {
            Button but = (Button)sender;
            DataGrid dg = (DataGrid)butNamingContainerNamingContainer;
            //此處是關鍵!!即找到包含按鈕的命名容器的上層命名容器
            if(dg == null) return;
            DataGridItem di =(DataGridItem)butNamingContainer;
            TableCell key= (TableCell)diCells[];
            TableCell state= (TableCell)diCells[];

            KeyID=(key==null)?:intParse(keyText);
            RowState=(state==null)?:intParse(stateText);
            ResponseWrite(UpdateData(thisKeyIDthisRowState)ToString());
           
            getData();
        }

From:http://tw.wingwit.com/Article/program/net/201311/13037.html
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.