0

我在后面的代码中动态创建了 GridView。现在,在实际的 DataBinding 事件中,我有一个 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");
    ......
4

1 回答 1

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)");

   }

}
于 2012-04-19T23:52:14.570 回答