C# 无法将PageIndex添加到GridViewTemplate:ITemplate javascript函数
我已经在代码隐藏中动态创建了GridView。现在,在实际的数据绑定事件中,我有一个JavaScript函数,用于保存编辑的单元格及其值。如果没有寻呼机,这一切都可以正常工作。现在,当我将寻呼机添加到GW时,我需要将页面索引添加到JS函数中,以便稍后计算实际更改的行/单元格C# 无法将PageIndex添加到GridViewTemplate:ITemplate javascript函数,c#,javascript,.net,gridview,itemplate,C#,Javascript,.net,Gridview,Itemplate,我已经在代码隐藏中动态创建了GridView。现在,在实际的数据绑定事件中,我有一个JavaScript函数,用于保存编辑的单元格及其值。如果没有寻呼机,这一切都可以正常工作。现在,当我将寻呼机添加到GW时,我需要将页面索引添加到JS函数中,以便稍后计算实际更改的行/单元格 // Databind an edit box in the grid void edt_DataBinding(object sender, EventArgs e) { DropDownList
// Databind an edit box in the grid
void edt_DataBinding(object sender, EventArgs e)
{
DropDownList txtData = (DropDownList)sender;
GridViewRow container = (GridViewRow)txtData.NamingContainer;
object dataValue = DataBinder.Eval(container.DataItem, _columnName);
txtData.Attributes.Add("onchange", "sav(" + container.RowIndex.ToString() + "," + _columnName + ",this.value)");
if (dataValue != DBNull.Value)
{
txtData.SelectedItem.Text = dataValue.ToString();
txtData.BackColor = Color.LightGreen;
}
}
我试图将“grdMain.PageIndex”添加到JS中,但出现了以下错误:“无法访问外部类型的非静态成员…”
txtData.Attributes.Add("onchange", "sav(" + grdMain.PageIndex + container.RowIndex.ToString() + "," + _columnName + ",this.value)");
有什么聪明的解决办法吗
更多代码:
public class GridViewTemplate : ITemplate
{
private ListItemType _templateType;
private string _columnName;
private string _col;
private string _dataType;
private bool _isLabel;
private bool _isCategory;
private string _types;
private IQueryable _role;
public GridViewTemplate(ListItemType type, string colname, string col, string DataType, bool isLabel, bool isCategory, string types, IQueryable role)
{
_templateType = type;
_columnName = colname;
_dataType = DataType;
_col = col;
_isLabel = isLabel;
_isCategory = isCategory;
_types = types;
_role = role;
}
void ITemplate.InstantiateIn(System.Web.UI.Control container)
{
Label lbl = new Label();
switch (_types)
{....
}
}
void lbl_DataBinding(object sender, EventArgs e)
{
Label lbl = (Label)sender;
GridViewRow container = (GridViewRow)lbl.NamingContainer;
object dataValue = DataBinder.Eval(container.DataItem, _columnName);
if (dataValue != DBNull.Value)
{
lbl.Text = dataValue.ToString();
}
}
// Databind an edit box in the grid
void edt_DataBinding(object sender, EventArgs e)
{....
}
}
按钮点击:
.....
TemplateField tfUsers = new TemplateField();
grdMain.Columns.Add(tfUsers);
tfUsers.ItemTemplate = new GridViewTemplate(ListItemType.Item, "Name", "0", "String", true, false, "resource", lame);
tfUsers.HeaderTemplate = new GridViewTemplate(ListItemType.Header, "Resource", "0", "String", true, false, "resource", lame);
_dtEntry.Columns.Add("Name");
......
我不知道你的全部代码,所以我在这里猜测 你试过ItemDataBound吗 像这样的
protected void grdMain_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
txtData.Attributes.Add("onchange", "sav(" + grdMain.PageIndex + container.RowIndex.ToString() + "," + _columnName + ",this.value)");
}
}
什么是grdMain?您在哪里访问它?看起来像是页面加载?grdMain是GridView ID,没有我在btn上单击创建表。