首先模板类如下
======
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?
======
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?
解决方案 »
- js遍历radiobuttonlist
- 网站后台的统计分析
- 网站首页打开速度慢
- _________碰到页间传值的怪事,期待解答______________
- 欢迎大家加入.net爱好者 群 咱自己的群30081753
- window.parent.frmR.location.replace("右边框架页面.aspx?id="+ctrl);//frmR为右边框架名称///////在VB。NET下怎么写这句
- 网关问题
- 呵呵,又是“/”应用程序中的服务器错误!!!!!!!
- 小问题(讨论)
- aspnet中能不能在客户端实现简单的运算
- 发生类型为 System.OutOfMemoryException 的异常 请问我的iis6怎么配置?
- 请问ass name 和 add key 这2种连接方式有什么区别?
问题一 :
如果将动态构建GridView列的“fillGridView()”放在“IsPostBack”里面,单击Button后,GridView中我自己定义的TextBox和DropDownList就没有了。为什么?
问题二:
为什么GridView.DataSource == null?
if(!ispostback)是第一次加载时执行的,如果第二次加载则不再运行其中的程序.
gridview.datasource==null,因为你的页面刷新了,,,当然为null了..呵呵..
我想在button onclick时间里面读取gridview每一行的DropDownList的值啊!就像下面这个例子
http://blog.csdn.net/net_lover/archive/2006/09/13/1216855.aspx
要是放一个Button click一下,跟我遇到的问题一样,
怎么办啊!高手请帮忙!
if (!Page.IsPostBack) {}
里
不用IsPostBack 我试过,每个控件都会被重新创建初始化,这样还是不能得到填写的值啊