回发后,自定义DataControlField不会保留值

时间:2015-07-25 22:48:08

标签: c# asp.net gridview updatepanel datafield

我有问题。我创建了一些自定义DataControlFields,因为我需要显示的数据不是来自DataSource上的DataGrid

我设法将控制权交给GridView,但我无法解决几个问题。

我的控件不会在回发之间保留其值。我将标记放在我设置为UpdatePanel的{​​{1}}内。然后我配置了我的触发器,不包括Conditional的触发器。我还尝试将GridView设置为UpdateMode。我在这里得到了同样的行为。

这是我的标记:

Always

以下是我的自定义 <asp:UpdatePanel UpdateMode="Conditional" ID="reportchooserUpdatePanel" runat="server"> <ContentTemplate> <Triggers> <asp:PostBackTrigger ControlID="ddlMonth" EventName="Load" /> <asp:PostBackTrigger ControlID="ddlMonth" EventName="SelectedIndexChanged" /> <asp:PostBackTrigger ControlID="ddlYear" EventName="DataBinding" /> <asp:PostBackTrigger ControlID="ddlYear" EventName="SelectedIndexChanged <asp:PostBackTrigger ControlID="GenerateReportsButton" EventName="Click" /> </Triggers> <table class="ms-formtable"> <tr> <td class="ms-formlabel"> <asp:Label ID="MonthYearLabel" runat="server" Text=""></asp:Label> </td> <td class="ms-formbody align-right"> <asp:DropDownList OnSelectedIndexChanged="ddlMonth_SelectedIndexChanged" AutoPostBack="true" runat="server" ID="ddlMonth" OnLoad="ddlMonth_Load"> <asp:ListItem Value="1">Januar</asp:ListItem> <asp:ListItem Value="2">Februar</asp:ListItem> <asp:ListItem Value="3">März</asp:ListItem> <asp:ListItem Value="4">April</asp:ListItem> <asp:ListItem Value="5">Mai</asp:ListItem> <asp:ListItem Value="6">Juni</asp:ListItem> <asp:ListItem Value="7">Juli</asp:ListItem> <asp:ListItem Value="8">August</asp:ListItem> <asp:ListItem Value="9">September</asp:ListItem> <asp:ListItem Value="10">Oktober</asp:ListItem> <asp:ListItem Value="11">November</asp:ListItem> <asp:ListItem Value="12">Dezember</asp:ListItem> </asp:DropDownList> </td> <td class="ms-formbody align-right"> <asp:DropDownList OnSelectedIndexChanged="ddlYear_SelectedIndexChanged" AutoPostBack="true" OnDataBinding="ddlYear_DataBinding" ID="ddlYear" runat="server"></asp:DropDownList> </td> </tr> <tr> <td style="width:100%;" class="ms-formbody" colspan="3"> <asp:GridView AutoGenerateColumns="false" ShowHeaderWhenEmpty="true" CssClass="grid-view" Width="100%" ID="gvProjects" runat="server"> </asp:GridView> </td> <td></td> <td></td> </tr> <tr> <td class="ms-formtoolbar align-right" colspan="3"> <asp:HyperLink Target="_blank" Font-Size="X-Small" ID="hlGembox" NavigateUrl="http://www.gemboxsoftware.com/spreadsheet/free-version" runat="server"></asp:HyperLink> <asp:Button OnClientClick="AddNotification('Please wait...')" ID="GenerateReportsButton" runat="server" Text="" OnClick="GenerateReportsButton_Click" /> </td> <td></td> <td></td> </tr> </table> </ContentTemplate> </asp:UpdatePanel> 之一的代码。除了显示的控件外,它们基本相同:

DataControlFields

最后,这是我将这些添加到我的页面的代码。我在标记中将class TemplateDropDownControl : DataControlField { SPList reportslist = ListItemHelper.GetReportsList(); protected void InitializeDataCell(DataControlFieldCell cell, DataControlRowState rowState) { string ID = Guid.NewGuid().ToString(); DropDownList list = new DropDownList(); list.ID = ID; FillContentTypeDropDown(list); cell.Controls.Add(list); } public override void InitializeCell(DataControlFieldCell cell, DataControlCellType cellType, DataControlRowState rowState, int rowIndex) { //Call the base method. base.InitializeCell(cell, cellType, rowState, rowIndex); this.InitializeDataCell(cell, rowState); } protected override DataControlField CreateField() { return new BoundField(); } public string DataField { get { object value = base.ViewState["DataField"]; if (value != null) { return value.ToString(); } else { return string.Empty; } } set { base.ViewState["DataField"] = value; this.OnFieldChanged(); } } public override void ExtractValuesFromCell(System.Collections.Specialized.IOrderedDictionary dictionary, DataControlFieldCell cell, DataControlRowState rowState, bool includeReadOnly) { DropDownList list = cell.Controls[0] as DropDownList; ListItem selectedValue = list.SelectedItem; if (dictionary.Contains(DataField)) dictionary[DataField] = selectedValue.Value; else dictionary.Add(DataField, selectedValue.Value); } private void FillContentTypeDropDown(DropDownList ddlContentTypes) { if (reportslist == null) return; SPContentTypeCollection cts = reportslist.ContentTypes; ddlContentTypes.Items.Clear(); foreach (SPContentType ct in cts) { ddlContentTypes.Items.Add(new ListItem() { Text = ct.Name, Value = ct.DocumentTemplateUrl + ct.DocumentTemplate.Replace("~site", "") }); } } } 的{​​{1}}属性设置为false:

AutoGenerateColumns

有人知道我在这里做错了吗?

编辑:也许我应该提到我在Sharepoint模式对话框中显示表单。

1 个答案:

答案 0 :(得分:0)

好的我用另一种方式解决了,因为我不知道为什么我的问题不断发生。我现在只使用asp:Table并从代码隐藏生成整个事情。我有一个方法,我呼吁每页回发。请注意,仅从Page_Load调用此方法非常重要。当我从Page_Init调用它时,它无效。

这是我的代码:

    private void BindDataGrid()
    {
        GenerateReportsButton.Enabled = true;
        reportsTable.Rows.Clear();

        TableHeaderRow headerrow = new TableHeaderRow();
        TableHeaderCell pnumberheader = new TableHeaderCell();
        TableHeaderCell templateheader = new TableHeaderCell();
        TableHeaderCell fileFormatHeader = new TableHeaderCell();

        pnumberheader.Text = ResourceHelper.LoadResource(ResourceName.ProjectnumberTableString);
        templateheader.Text = ResourceHelper.LoadResource(ResourceName.TemplateString);
        fileFormatHeader.Text = ResourceHelper.LoadResource(ResourceName.FileFormatString);

        headerrow.Cells.Add(pnumberheader);
        headerrow.Cells.Add(templateheader);
        headerrow.Cells.Add(fileFormatHeader);
        reportsTable.Rows.Add(headerrow);

        if (ddlYear.SelectedItem == null || ddlMonth.SelectedItem == null)
        {
            int index = reportsTable.Rows.Add(new TableRow());
            TableCell cell = new TableCell();
            cell.ColumnSpan = 3;
            cell.Text = ResourceHelper.LoadResource(ResourceName.NoListItemsForMonthYear);
            reportsTable.Rows[index].Cells.Add(cell);
            GenerateReportsButton.Enabled = false;
            return;
        }

        //Get items here

        if (items.Count == 0)
        {
            int index = reportsTable.Rows.Add(new TableRow());
            TableCell cell = new TableCell();
            cell.ColumnSpan = 3;
            cell.Text = ResourceHelper.LoadResource(ResourceName.NoListItemsForMonthYear);
            reportsTable.Rows[index].Cells.Add(cell);
            GenerateReportsButton.Enabled = false;
            return;
        }
        else
            InsertRowIntoProjectTable("Intern", "Intern");

        List<string> processedReports = new List<string>();

        foreach(SPListItem item in items)
        {
            if (item[Variables.projectNumberField].ToString() != "Intern" && !processedReports.Contains(item[Variables.activityProject].ToString()))
            {
                InsertRowIntoProjectTable(item[Variables.activityProject].ToString(), item.ID.ToString());
                processedReports.Add(item[Variables.activityProject].ToString());
            }
        }
    }

然后你可以像这样读取数据:

foreach(TableRow row in reportsTable.Rows)
{
    //Important since foreach also iterates over headerrow
    if (row.Cells[1].Controls.Count > 0 && row.Cells[1].Controls[0] is DropDownList)
    {
         string value1= row.Cells[0].Text;
         string value2= ((DropDownList)row.Cells[1].Controls[0]).SelectedValue;

         //do stuff with the data
    }
 }

如果有人仍然找到上述我特定问题的答案,请随意添加。一旦确认它有效,我会将其标记为已接受。