2012-04-19 31 views
0

我在代碼後面動態創建了GridView。現在,在實際的DataBinding事件中,我已經有了一個保存編輯單元格及其值的JavaScript函數。這一切工作正常,沒有尋呼機,現在當我將尋呼機添加到GW我需要添加到JS功能的頁面索引,所以我可以計算後來哪個行/單元它實際上是被改變。無法將PageIndex添加到GridViewTemplate:ITemplate javascript函數

// 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"); 
    ...... 
+0

是什麼grdMain?和你訪問的地方?看起來像page_load? – 2012-04-19 18:44:15

+0

grdMain是GridView ID,並且我沒有在btn點擊創建表。 – Jake 2012-04-19 22:17:03

回答

1

我不知道您的完整代碼,所以我在這裏猜測...

中有你的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)"); 

    } 

}