【发布时间】:2013-10-10 10:28:22
【问题描述】:
我有一个gridview 和它外面的2 个按钮。单击按钮时,gridview 的所有行都应变为可编辑的。我正在使用 ITemplate 类来完成这项任务,但无法完成。
下面是我获取可编辑网格视图的代码:
public class GridViewTemplate : ITemplate
{
private DataControlRowType templateType;
private string columnName;
private string columnNameBinding;
private string controlType;
public GridViewTemplate(DataControlRowType type, string colname, string colNameBinding, string ctlType)
{
templateType = type;
columnName = colname;
columnNameBinding = colNameBinding;
controlType = ctlType;
}
public void InstantiateIn(System.Web.UI.Control container)
{
switch (templateType)
{
case DataControlRowType.Header:
Literal lc = new Literal();
lc.Text = columnName;
container.Controls.Add(lc);
break;
case DataControlRowType.DataRow:
if (controlType == "Label")
{
Label lb = new Label();
lb.ID = "lblName";
lb.DataBinding += new EventHandler(this.OnDataBinding);
container.Controls.Add(lb);
}
else if (controlType == "TextBox")
{
TextBox tb = new TextBox();
tb.ID = "txtWeightage" + columnNameBinding;
tb.DataBinding += new EventHandler(this.OnDataBinding);
container.Controls.Add(tb);
}
default:
break;
}
}
public void OnDataBinding(object sender, EventArgs e)
{
object bound_value_obj = null;
Control ctrl = (Control)sender;
IDataItemContainer data_item_container = (IDataItemContainer)ctrl.NamingContainer;
bound_value_obj = DataBinder.Eval(data_item_container.DataItem, columnName);
switch (templateType)
{
case DataControlRowType.Header:
Label lb = (Label)sender;
lb.Text = bound_value_obj.ToString();
break;
case DataControlRowType.DataRow:
TextBox txtbox = (TextBox)sender;
txtbox.Text = bound_value_obj.ToString();
break;
}
}
}
【问题讨论】:
-
请参阅此MSDN Article 寻求帮助。
-
谁能帮我在我的按钮点击和其他相关的gridview事件中实现这段代码...
标签: c# asp.net gridview itemplate