在SharePoint webpart中,我正在显示SpgridView。 SPGRIDVIEW控件位于AJAX UpdatePanel内部,并绑定到代码中的DataTable。

GridView的列主要是作为TemplateField创建的,只有一个边界。我创建了一个继承商品板的类,该类别板为模板字段生成itemTemplate。该课程还处理数据列表/数据列的数据指标。

我在网格外面有一个按钮,该按钮被设计时,以节省对数据浏览和基础数据库中数据的更改。

问题在于,我无法在“ onclick”事件中的GridView中获得对控件的引用,我正在通过GridView中的行进行迭代,并使用以下代码来获取对控件的引用:

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

以上总是无效的,演员表不起作用。谁能为此阐明吗?我认为这是某种观点问题,但我不确定。

我在下面发布了更多代码样本。

谢谢,

亚历克斯

 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

感谢您的回答 - 我最终发现了问题。

我正在重新设置createChildControls方法中的网格列,并在下拉列表的SelectedIndexchanged事件中再次设置它们。

此原因最初的原因是网格需要根据下拉列表中选择的值添加不同的列,但这在createChildControls事件中尚不清楚。

像这样两次设置网格使控件以某种方式从页面控件集合中分离出来,因此当我尝试将它们抛弃时,我总是会毫无空意。

我现在没有两次设置网格列,它效果很好 - 感谢您的帮助!

其他提示

您应该检查DataRow的类型,因为我相信第一行是标题行,您可以使用ROW IS DATAROW进行此操作。

不久前,我们也有类似的问题,除了施放控制的方法与我多次采取的方法相同。

让我知道你是怎么办的。

关于西蒙

我已经看到SpgridView在我的一个自定义网络部分中的后备后表现出奇怪的表现,例如呈现行,但没有为这些行中的单元格渲染内容。

您可以尝试暂时重写它以使用.NET GridView而不是SpgridView,并查看是否有任何区别。如果那也失败,那么您的代码中可能会有一些错误。

许可以下: CC-BY-SA归因
scroll top