是否可以将控件置于另一个控件中?

时间:2011-03-08 14:22:25

标签: c# asp.net gridview user-controls

我问这是因为我从Exchange Server访问邮件。邮件正文包含HTML。为了显示邮件的正文,我使用FreeTextBox控件。但我有很多邮件,我首先在gridview中显示。但问题是正文中的HTML也显示在gridview单元格中。是否可以在Gridview的每个记录中放置一个FreeTextBox ???

欢迎任何帮助

这是我用来绑定/ etc

的代码
    private DataTable dt;
    private ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
    private void Page_Load(object sender, System.EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            dt = new DataTable();
            MakeDataTable();
            ConnectToExchange();
            DoorloopMailbox();
            BindGrid();
        }
        else
        {
            dt = (DataTable)ViewState["DataTable"];
        }
        ViewState["DataTable"] = dt;
        //service.TraceEnabled = true; --om te traceren.

    }

    private void ConnectToExchange()
    {
        service.Credentials = new NetworkCredential("user", "pass", "domain.com");
        service.Url = new Uri("Uri");
    }

    private void DoorloopMailbox()
    {
        FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox,
            new ItemView(100));
        foreach (Item item in findResults.Items)
        {
            item.Load();
            DataRow dr = dt.NewRow();
            dr["OutlookID"] = item.Id.UniqueId;
            //dr["Sender"] = item.
            dr["Subject"] = item.Subject;
            string processedInput;
            string omschrijving = item.Body;
            if (omschrijving != null)
            {
                processedInput = Regex.Replace(omschrijving, @"<(.|\n)*?>", string.Empty);
            }
            else
            {
                processedInput = "geen omschrijving";
            }
            dr["Body"] = processedInput;
            dr["Sent"] = item.DateTimeSent;
            dt.Rows.Add(dr);
        }
    }

    private void BindGrid()
    {
        gvAanvragen.DataSource = dt;
        gvAanvragen.DataBind();
    }

    private void MakeDataTable()
    {
        dt.Columns.Add("OutlookID");
        dt.Columns.Add("Sender");
        dt.Columns.Add("Subject");
        dt.Columns.Add("Body");
        dt.Columns.Add("Sent");
    }

    protected void GvMeldingen_SelectedIndexChanged(object sender, EventArgs e)
    {
        Response.Redirect("Detailscherm.aspx?melder=" + Server.UrlEncode(gvMeldingen.SelectedRow.Cells[3].Text)
            + "&datum=" + gvMeldingen.SelectedRow.Cells[6].Text
            + "&onderwerp=" + Server.UrlEncode(gvMeldingen.SelectedRow.Cells[4].Text)
            + "&id=" + Server.UrlEncode(gvMeldingen.SelectedRow.Cells[1].Text)
            + "&omschrijving=" + Server.UrlEncode(gvMeldingen.SelectedRow.Cells[5].Text));
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
    }

    protected void GvAanvragen_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvAanvragen.PageIndex = e.NewPageIndex;
        BindGrid();
    }

这是设计师视图中的代码:

<asp:GridView ID="gvAanvragen" 
            OnPageIndexChanging="GvAanvragen_PageIndexChanging" runat="server" AllowPaging="True" 
            AllowSorting="True" BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" 
            BorderWidth="1px" CellPadding="4" ForeColor="Black" GridLines="Vertical" 
            PageSize="5">
            <RowStyle BackColor="#F7F7DE" />
            <FooterStyle BackColor="#CCCC99" />
            <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
            <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
            <AlternatingRowStyle BackColor="White" />
        </asp:GridView>

3 个答案:

答案 0 :(得分:3)

是的,你可以这样做。在gridview中使用TemplateField,然后将FreeTextBox控件放在ItemTemplate或EditTemplate中(可能只是编辑模板)。

有点像这样:

        <asp:TemplateField HeaderText="HTML Content">
            <ItemTemplate>
                <asp:Label ID="lblText" runat="server" Text='<% Eval("") %>' />
            </ItemTemplate>
            <EditItemTemplate>
                <FTB:FreeTextBox id="ftbTest" runat="server" Text='<% Bind("") %>' />
            </EditItemTemplate>
        </asp:TemplateField>

修改

你甚至需要使用FreeTextBox吗?也许你只需要在你的BoundField上设置HtmlEncode="false"(如果你使用的话)。默认情况下,出于安全原因,GridView会在显示之前对所有文本进行HtmlEncode编码。

编辑2 注意我设置AutoGenerateColumns="false"并添加了三列。我省略了消息ID,但您可以通过添加另一个已经存在的BoundField来重新添加它。

<asp:GridView ID="gvAanvragen" 
        OnPageIndexChanging="GvAanvragen_PageIndexChanging" runat="server" AllowPaging="True" 
        AllowSorting="True" BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" 
        BorderWidth="1px" CellPadding="4" ForeColor="Black" GridLines="Vertical" 
        PageSize="5" AutoGenerateColumns="false">
        <RowStyle BackColor="#F7F7DE" />
        <FooterStyle BackColor="#CCCC99" />
        <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
        <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
        <AlternatingRowStyle BackColor="White" />
        <Columns>
            <asp:BoundField HeaderText="Subject" DataField="Subject" />
            <asp:BoundField HeaderText="Body" DataField="Body" HtmlEncode="false" />
            <asp:BoundField HeaderText="Sent" DataField="Sent" />
        </Columns>
    </asp:GridView>

答案 1 :(得分:1)

是的,您可以使用模板字段在网格视图中显示您想要的任何内容:http://msdn.microsoft.com/en-us/library/bb288032.aspx

答案 2 :(得分:0)

是的,您应该能够在gridview中为网格中的每一行添加一个控件。在我们进一步提供帮助之前,您需要提供有关如何构建gridview的更多信息。我们能看到一些例子吗?