无法将 PageIndex 添加到 GridViewTemplate:ITemplate javascript 函数
本文关键字:ITemplate javascript 函数 GridViewTemplate PageIndex 添加 | 更新日期: 2023-09-27 18:31:35
我在代码隐藏中动态创建了 GridView。现在,在实际的数据绑定事件中,我有一个JavaScript函数,用于保存编辑的单元格及其值。这一切都在没有寻呼机的情况下工作正常,现在当我将寻呼机添加到 GW 时,我需要将页面索引添加到 JS 函数中,以便我以后可以计算出它实际上是哪一行/单元格被更改了。
// 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)");
}
}