Jake
Jake

Reputation: 159

Unable to add PageIndex to the GridViewTemplate : ITemplate javascript function

I've got dynamically created GridView in code behind. Now during actual DataBinding event I've got a JavaScript function in place that is saving edited cell and its value. This all works fine without a pager, now when I add pager to GW I need to add to JS function the Page Index so I can calculate later which row/cell it actually was that got changed.

// 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;
        }
    }

I tried to add "grdMain.PageIndex" to the JS but the following error occurs: "Cannot access the non-static member of outer type....

txtData.Attributes.Add("onchange", "sav(" + grdMain.PageIndex + container.RowIndex.ToString() + "," + _columnName + ",this.value)");

Any intelligent workaround?

More code:

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)
    {....
    }
 }

Button Click:

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

Upvotes: 0

Views: 369

Answers (1)

Nick Kahn
Nick Kahn

Reputation: 20078

i dont know your full code so i am guessing here...

have you tried in ItemDataBound?

something like this...

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

   }

}

Upvotes: 1

Related Questions