0

我正在构建一个 Web 应用程序来构建一个文档。该文档具有段落(外部转发器)和子段落(内部转发器)。我正在寻找的是一种向现有文档添加空白小段的方法。

我的标记:

<asp:Repeater ID="ParagraphRepeater" runat="server" 
   OnItemDataBound="ParagraphRepeater_ItemDataBound" >
   <ItemTemplate>
       <asp:TextBox ID="ParagraphTitleTextBox" runat="server" Font-Bold="true" Width="300px" 
           Text='<%# Eval("ParagraphTitle") %>'></asp:TextBox>
       <br />
       <asp:TextBox ID="ParagraphTextTextBox" runat="server" TextMode="MultiLine" Wrap="true"
                        width="1100px" Height="50px" Text='<%# Eval("ParagraphText") %>'></asp:TextBox>
       <asp:Button ID="DeleteParagraphButton" runat="server" Text="Delete" OnClick="DeleteParagraphButton_Click" />
       <asp:Repeater ID="SubParagraphRepeater" runat="server" DataSourceID="SubParagraphSqlDataSource">
           <ItemTemplate>
               <div style="margin-left: 30px">
                   <asp:TextBox ID="SubParagraphTitleTextBox" runat="server" Font-Underline="true" Width="200px"
                                    Text='<%# Eval("SubParagraphTitle") %>'></asp:TextBox>
                   <br />
                   <asp:TextBox ID="SubParagraphTextTextBox" runat="server" TextMode="MultiLine" Wrap="true"
                                    Width="1050px" Height="50px" Text='<%# Eval("SubParagraphText") %>'></asp:TextBox>
                   <asp:Button ID="DeleteSubParagraphButton" runat="server" Text="Delete" 
                                    OnClick="DeleteSubParagraphButton_Click" />
                   <br />
               </div>
           </ItemTemplate>
       </asp:Repeater>
       <br />
       <br />
       <br />
   </ItemTemplate>

我的代码:

protected void MultiView1_ActiveViewChanged(object sender, EventArgs e)
{
    if (MultiView1.GetActiveView() == InputView) 
    {
        BuildParagraphDataTable();
        BuildSubParagraphDataTable();

        if (RevisionsDropDownList.SelectedValue == "0")
        {
            // User is creating a new document
            // Call method to create a datatable for the form row
            SetFormRow();

            // Call method to create a datatable for the paragraph row
            AddParagraph();

            DataTable dt = (DataTable)ViewState["ParagraphTable"];
            ParagraphRepeater.DataSource = dt;
            ParagraphRepeater.DataBind();

        }
        else
        {
            // User is opening an existing document
            // Get the formId and save it to ViewState
            int formId = Convert.ToInt32(RevisionsDropDownList.SelectedValue);
            ViewState["FormId"] = formId.ToString();

            // Bind the Paragraph repeater to its sqlDataSource
            ParagraphRepeater.DataSource = ParagraphSqlDataSource;
            ParagraphRepeater.DataBind();
        }
    }
}
protected void AddParagraph()
{
    int paragraphId;

    DataTable dt = (DataTable)ViewState["ParagraphTable"];
    DataRow dr = dt.NewRow();

    if (ViewState["ParagraphId"] != null)
        paragraphId = Convert.ToInt32(ViewState["ParagraphId"]);
    else
        paragraphId = 0;

    paragraphId--;

    int formId = Convert.ToInt32(ViewState["FormId"]);

    dr["ParagraphId"] = paragraphId;
    dr["FormId"] = formId;
    dr["ParagraphTitle"] = string.Empty;
    dr["ParagraphText"] = string.Empty;
    dr["CreatorId"] = string.Empty;
    dr["RevisorId"] = string.Empty;
    dt.Rows.Add(dr);

    ViewState["ParagraphTable"] = dt;
    ViewState["ParagraphId"] = paragraphId;

}
protected void AddSubParagraph()
{
    DataTable dt = (DataTable)ViewState["SubParagraphTable"];
    DataRow dr = dt.NewRow();

    int paragraphId = Convert.ToInt32(ViewState["ParagraphId"]);

    dr["ParagraphId"] = paragraphId;
    dr["SubParagraphTitle"] = string.Empty;
    dr["SubParagraphText"] = string.Empty;
    dr["CreatorId"] = string.Empty;
    dr["RevisorId"] = string.Empty;
    dt.Rows.Add(dr);

    ViewState["SubParagraphTable"] = dt;
}

protected void ParagraphRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        AddParagraph();
        DataTable parentDataTable = (DataTable)ViewState["ParagraphTable"];
        DataRow lastDTRow = parentDataTable.Rows[parentDataTable.Rows.Count - 1];

        int paragraphId = (int)ViewState["ParagraphId"];
        DataRowView thisParagraphRowView = (DataRowView)e.Item.DataItem;

        paragraphId = (int)thisParagraphRowView.Row["ParagraphId"];

        lastDTRow["ParagraphId"] = thisParagraphRowView.Row["ParagraphId"];
        lastDTRow["FormId"] = thisParagraphRowView.Row["FormId"];
        lastDTRow["ParagraphTitle"] = thisParagraphRowView.Row["ParagraphTitle"];
        lastDTRow["ParagraphText"] = thisParagraphRowView.Row["ParagraphText"];

        ViewState["ParagraphTable"] = parentDataTable;
        ViewState["ParagraphId"] = paragraphId.ToString();

        DataTable childDataTable;
        DataRowView thisSubParagraphRowView;

        Repeater childRepeater = (Repeater)e.Item.FindControl("SubParagraphRepeater");

        foreach (RepeaterItem item in childRepeater.Items)
        {
            if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
            {
                thisSubParagraphRowView = (DataRowView)item.DataItem;
                if (thisSubParagraphRowView != null)
                {
                    AddSubParagraph();
                    childDataTable = (DataTable)ViewState["SubParagraphTable"];
                    lastDTRow = childDataTable.Rows[childDataTable.Rows.Count - 1];

                    lastDTRow["ParagraphId"] = thisSubParagraphRowView.Row["ParagraphId"];
                    lastDTRow["SubParagraphTitle"] = thisSubParagraphRowView.Row["SubParagraphTitle"];
                    lastDTRow["SubParagraphText"] = thisSubParagraphRowView.Row["SubParagraphText"];

                    ViewState["SubParagraphTable"] = childDataTable;
                }
            }
        }
    }
}

当用户打开现有表单时,itemDataBound 代码会填充段落和子段落的数据表。(这些数据表将用于更新数据库。)

当用户创建一个新表单时,调用 setForm() 和 addParagraph() 方法来创建表单 ID 并添加一个空白段落。不添加空白小段 - 用户必须单击按钮才能这样做。

至于数据模型,有一个Form(用户从ddl中选择)。一个表单可以有 1 到多个段落,段落可以有零到多个子段落。

我需要在内部转发器中为外部转发器中的特定行创建一个空白行(光标当前所在的段落,或者如果光标位于子段落中,则为父段落)。我怎么做?我在 Google 上进行了大量挖掘,但找不到解决此问题的条目。谢谢。

4

1 回答 1

0

答案是将“AddSubParagraph”按钮从独立位置移动到外部中继器内部。这样,可以使用按钮的“NamingContainer”属性来定位中继器。然后就是找到内部的中继器Repeater innerRepeater = (Repeater)outerRepeater.FindControl("childRepeater");,并在其中添加一行。我可以添加该行,但在 DataBinding 内部中继器时遇到问题。

我所做的是将每个转发器的数据表保持在视图状态,并在转发器和数据表之间镜像数据。当我需要添加一行时,我将它添加到数据表中,然后将转发器数据绑定到数据表。但我收到一个错误:错误所在的行是childRepeater.Datasource = dt;. 错误是“DataSource 和 DataSourceId 都已定义。删除一个定义。” DataSourceId 在标记中的 childRepeater 上定义,指向 SQLDataSource。所以我需要找到一种方法在检索时动态地将内部转发器绑定到它的 SQLDataSource。

一个障碍:我想我会使用 ItemDataBound 事件来进行数据绑定。但是我已经有很多代码了(见上文)。我可以使用另一个有意义的事件吗?

于 2017-10-12T15:05:27.443 回答