首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

在GridView中用DataTable,定义模板页,PostBack后TextBox值犹在,但dropDownList值没了,请高手解决.解决方法

2012-01-09 
在GridView中用DataTable,定义模板页,PostBack后TextBox值犹在,但dropDownList值没了,请高手解决......首

在GridView中用DataTable,定义模板页,PostBack后TextBox值犹在,但dropDownList值没了,请高手解决......
首先模板类如下
======
        public   class   MyTemplate   :   ITemplate
        {
                private   SWT.Components.DataTableColumnInfo   _DTColumnInfo;
                private   ListItemCollection   _ListItems;

                public   MyTemplate(SWT.Components.DataTableColumnInfo   toColumnInfo,ListItemCollection   taItems)
                {
                        this._DTColumnInfo   =   toColumnInfo;
                        this._ListItems   =   taItems;
                }

                public   void   InstantiateIn(Control   toContainer)
                {
                        if   (this._DTColumnInfo.ControlTypeName   ==   "TextBox ")   {
                                string   lcID   =   toContainer.ID;
                                lcID   =   toContainer.ClientID;
                                TextBox   loEdtBox   =   new   TextBox();
                                loEdtBox.ID   =   "edt "   +   this._DTColumnInfo.Name;
                                loEdtBox.EnableViewState   =   true;
                                loEdtBox.DataBinding   +=   new   EventHandler(this.OnDataBinding);
                                loEdtBox.Width   =   this._DTColumnInfo.Width;
                                toContainer.Controls.Add(loEdtBox);
                                if   (this._DTColumnInfo.Type   ==   "System.Int32 ")   {
                                        CompareValidator   loCompareInteger   =   new   CompareValidator();
                                        loCompareInteger.ControlToValidate   =   loEdtBox.ID;
                                        loCompareInteger.Operator   =   ValidationCompareOperator.DataTypeCheck;
                                        loCompareInteger.Type   =   ValidationDataType.Integer;


                                        loCompareInteger.ErrorMessage   =   "! ";
                                        toContainer.Controls.Add(loCompareInteger);
                                }
                        }
                        else   if   (this._DTColumnInfo.ControlTypeName   ==   "DropDownList ")
                        {
                                DropDownList   loDDL   =   new   DropDownList();
                                loDDL.ID   =   "ddl "   +   this._DTColumnInfo.Name;
                                loDDL.CssClass   =   "dropdown_default ";
                                loDDL.EnableViewState   =   true;
                                foreach   (ListItem   loItem   in   this._ListItems)   {
                                        loDDL.Items.Add(loItem);
                                }
                               
                                loDDL.DataBinding   +=   new   EventHandler(this.OnDataBinding);
                                toContainer.Controls.Add(loDDL);
                        }
                        else   if   (this._DTColumnInfo.ControlTypeName   ==   "CheckBox ")
                        {
                                CheckBox   loChkBox   =   new   CheckBox();
                                loChkBox.ID   =   "chk "   +   this._DTColumnInfo.Name;
                                loChkBox.EnableViewState   =   true;
                                loChkBox.DataBinding   +=   new   EventHandler(this.OnDataBinding);
                                toContainer.Controls.Add(loChkBox);


                        }
                }

                public   void   OnDataBinding(object   sender,   EventArgs   e)
                {
                        GridViewRow   loContainer   =   (GridViewRow)((Control)sender).NamingContainer;
                        if   (this._DTColumnInfo.ControlTypeName   ==   "TextBox ")
                        {
                                TextBox   loEdtBox   =   (TextBox)sender;
                                loEdtBox.Text   =   ((DataRowView)loContainer.DataItem)[_DTColumnInfo.Name].ToString();
                        }
                        else   if   (this._DTColumnInfo.ControlTypeName   ==   "DropDownList ")
                        {
                                DropDownList   loDDL   =   (DropDownList)sender;
                                ListItem   loSelItem   =   loDDL.Items.FindByValue((((DataRowView)loContainer.DataItem)[_DTColumnInfo.Name].ToString()));
                                if   (loSelItem   !=   null)
                                {
                                        loSelItem.Selected   =   true;
                                }
                        }
                        else   if   (this._DTColumnInfo.ControlTypeName   ==   "CheckBox ")
                        {
                                CheckBox   loChkBox   =   (CheckBox)sender;
                                loChkBox.Checked   =   Convert.ToBoolean(((DataRowView)loContainer.DataItem)[_DTColumnInfo.Name].ToString());
                        }
                }
        }

==========
Page_Load如下:
                protected   void   Page_Load(object   sender,   EventArgs   e)


                {
                        if   (!Page.IsPostBack)   {

                                fillGridView( "add ");//根据动态构造的DataTable(并非来自数据库),构建GridView列
                                addInitial();//对部分控件赋初始值
                                this.grdChapter.EnableViewState   =   true;//grdChapter为GridView控件
                      }
===========
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
问题一   :
如果将动态构建GridView列的“fillGridView()”放在“IsPostBack”里面,单击Button后,GridView中我自己定义的TextBox和DropDownList就没有了。为什么?
问题二:
为什么GridView.DataSource     ==   null?
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

问题三:
我将“fillGridView()”拿到了“IsPostBack”外面,
页面上有一个Button,单击事件中读取Gridview中各单元值,GridView中每一行有edtQuestionNum(TextBox控件)和ddlQuestionType(DropDownList控件)示例代码如下,
                        foreach   (GridViewRow   loCurRow   in   this.grdChapter.Rows)   {
                                if   (loCurRow.RowType   ==   DataControlRowType.DataRow)   {
                                                if   (loCurRow.FindControl( "edtQuestionNum ")   !=   null)
                                                {
                                                        Response.Write(((TextBox)loCurRow.FindControl( "edtQuestionNum ")).Text   +   "|| ");
                                                }
                                                if   (loCurRow.FindControl( "ddlQuestionType ")   !=   null)
                                                {
                                                        Response.Write(((DropDownList)loCurRow.FindControl( "ddlQuestionType ")).SelectValue+   "> > ");
                                                }
                                }                


                        }

为什么每次打印在Page上的TextBox是我填写的值,而DropDownList都是第一条的Value?



[解决办法]
this.grdChapter.EnableViewState = true;

这句话就直接在gridview属性中直接设置就可以了,,这样你第一次所取的数据在你单击button后会自动保存的,,,,


gridview.datasource==null,因为你的页面刷新了,,,当然为null了..

呵呵..
[解决办法]
不要放在
if (!Page.IsPostBack) {}

[解决办法]
每次綁定datagrid那麼dropdownlist邪說奧重新綁定一次的。

热点排行