質問

SharePoint?

????SPGridView????????Web???? SPGridView????Ajax?UpdatePanel????????????????????????????

GridView??????????templatefields??????????

?????boundfield??????????ITemplate templatefields????ItemTemplate???????????????????????????DataTable??/?????????????????????????????

???DataTable??????????????GridView????????????????????????????????????????????????

????????????onclick?????GridView????????????????????????????????????GridView??????????????????????????????????????????:

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

??????????????????????????????

?????

?????

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

    }
役に立ちましたか?

解決 2

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!

他のヒント

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

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.

ライセンス: CC-BY-SA帰属
所属していません sharepoint.stackexchange
scroll top