Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

In a SharePoint webpart I am displaying an SPGridView. The SPGridView control is inside an Ajax updatepanel and is bound to a datatable in the code.

The columns for the gridview are created mostly as templatefields, there is only one boundfield. I have created a class that inherits ITemplate that produces the itemtemplate for the templatefields. This class also handles the databinding of the controls to columns/data in the datatable.

I have a button outside the grid which when clicked is designed to save the changes made to the data in the gridview to the datatable and underlying database.

The problem is that I cannot get a reference to the controls within the gridview in the buttons 'onclick' event, I am iterating through the rows in the gridview and using the following code to obtain a reference to the control:

foreach (SPGridViewRow row in grid.Rows)
        {
            TextBox txtExamMark = (TextBox)row.FindControl("txtExamMark");

The above is always null and the cast does not work. Can anyone shed any light on this? I assume it is some kind of viewstate issue but im not sure.

I have posted some more code samples below.

Thanks,

Alex

 public class CustomSPGridViewTemplate : ITemplate
{
    string columnName;
    string controlType;

    int controlWidth;

    ListItemCollection itemColl;

    string txtMode;

    public CustomSPGridViewTemplate(string ctype,string colname,ListItemCollection itemcol,string mode,int cWidth)
    {
        columnName = colname;
        controlType = ctype;
        itemColl = itemcol;
        controlWidth = cWidth;
        txtMode = mode;
    }

    public void InstantiateIn(System.Web.UI.Control container)
    {
        if (controlType == "TextBox")
        {
            TextBox txtBox = new TextBox();
            txtBox.ID = "txt" + columnName;
            txtBox.Visible = true;
            txtBox.Width = Unit.Pixel(controlWidth);
            txtBox.DataBinding += new EventHandler(txtBox_DataBinding);
            if (txtMode == "MultiLine")
            {
                txtBox.TextMode = TextBoxMode.MultiLine;
            }
            container.Controls.Add(txtBox);
        }
        if (controlType == "PupilImage")
        {
            Image pupilImage = new Image();
            pupilImage.ID = "pupilImage";
            pupilImage.Visible = true;
            pupilImage.DataBinding += new EventHandler(pupilImage_DataBinding);
            container.Controls.Add(pupilImage);
        }
        if (controlType == "DropDown")
        {
            DropDownList dropDown = new DropDownList();
            dropDown.ID = "ddl" + columnName;
            dropDown.Visible = true;             
            dropDown.DataBinding += new EventHandler(dropDown_DataBinding);
            container.Controls.Add(dropDown);
        }
    }

    void txtBox_DataBinding(object sender, EventArgs e)
    {
        TextBox txtBox = (TextBox)sender;
        SPGridViewRow container = (SPGridViewRow)txtBox.NamingContainer;
        txtBox.Text = DataBinder.Eval(container.DataItem, columnName).ToString();

    }
share|improve this question
    
am doing the same think, can i get your webpart code please ? –  user8738 Jun 7 '12 at 13:50

3 Answers 3

You should check the type of DataRow as the first row I believe will be the Header row you can do this using row is DataRow.

We had a similar problem a while back, apart from that the approach to cast the control is the same approach I have taken on many occasions.

let me know how you get on.

Regards Simon

share|improve this answer
    
Thanks Simon - I saw this mentioned elsewhere too, I didnt have a header row in my Grid but it was a useful thing to note :-) –  Anonymous Jan 14 '10 at 15:07
up vote 2 down vote accepted

Thanks for the responses - I found the problem in the end.

I was re-setting up the grid columns in the CreateChildControls method and also setting them up again in the SelectedIndexChanged event of a DropDownList.

The reason for this originally is the grid needs to have different columns added to it depending on the value picked in the dropdown but this is not known in the CreateChildControls event.

Setting up the grid like this twice caused the controls to somehow dissapear from the pages controls collection so when I tried to cast them back I always got null.

I am now not setting up the grid columns twice and it works great - thanks for your help!

share|improve this answer
    
Cheers for posting the answer - I'll have to remember that one. –  Anonymous Jan 15 '10 at 7:50

I have seen the SPGridView behave strangely after postbacks in one of my custom web parts, e.g. rendering rows but not rendering content for cells in those rows.

You could try rewriting it temporarily to just use the .NET GridView, instead of the SPGridView, and see if that makes any difference. If that one fails as well, then there is probably some bug somewhere in your code.

share|improve this answer
    
Thanks Jaap - I did try rewriting it to use the standard .NET GridView and had the same issue. I found the problem in the end - see below for the solution. Cheers –  Anonymous Jan 14 '10 at 15:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.