将数据库值转换为HTML

时间:2015-05-29 00:26:27

标签: c# asp.net

我正在开发一个用C#.Net 4.0编写的应用程序 - 基本上我有一个存储过程从我的数据库返回我的命令...我将这些命令绑定到GridView -

代码隐藏

protected void Page_Load(object sender, EventArgs e)
{
    // connect to the database
    this.DbConnection();
    // open the connection
    sqlConn.Open();
    // call the stored procedure
    sqlCmd = new SqlCommand("usp_myorders", sqlConn);
    sqlCmd.CommandType = CommandType.StoredProcedure;
    // add parameter 
    sqlCmd.Parameters.Add("@startdate", SqlDbType.DateTime);
    sqlCmd.Parameters["@startdate"].Value = "06/01/2015 18:00:00"; 
    sqlRdr = sqlCmd.ExecuteReader();

    // get the count
    // @TODO: This is a very expensive method -- there must be a better way
    int count = 0;
    if (sqlRdr.HasRows)
    {
        while (sqlRdr.Read())
        {
            ++count;
        }
    }
    // close the connection
    sqlConn.Close();

    // reopen the connection, to create the grid
    sqlConn.Open();
    // bind data to the grid
    grid1.EmptyDataText = "No Record(s) Found...";
    grid1.DataSource = sqlCmd.ExecuteReader();
    grid1.DataBind();
    MakeGridViewPrinterFriendly(grid1);
    // close the connection
    sqlConn.Close();
    //Get the number of rows returned, and then assign it to the Label control
    lblRowCount.Text = count.ToString() + " Record(s) Found...";
    lblRowCount2.Text = count.ToString() + " Record(s) Found...";
}

在HTML方面,我有以下代码

HTML CODE

<asp:Label ID="lblRowCount" runat="server" Text="Label"></asp:Label>
        <!-- Table combined Styles -->
        <asp:GridView ID="grid1" runat="server" class="striped sortable lcpa_ord_nonprintable" AutoGenerateColumns="False" ShowHeaderWhenEmpty="true">
            <Columns>
                <asp:BoundField DataField="fname" HeaderStyle-HorizontalAlign="Left" HeaderText="First Name" SortExpression="fname" ReadOnly="true" />
                <asp:BoundField DataField="lname" HeaderStyle-HorizontalAlign="Left" HeaderText="Last Name" SortExpression="lname" ReadOnly="true" />
                <asp:BoundField DataField="item" HeaderStyle-HorizontalAlign="Center" HeaderText="Order" SortExpression="item" ReadOnly="true" />
                <asp:BoundField DataField="num_seats_pur" HeaderStyle-HorizontalAlign="Center" HeaderText="Quantity" SortExpression="num_seats_pur" ReadOnly="true" />

                <asp:TemplateField HeaderText="Action" HeaderStyle-HorizontalAlign="Center">
                    <ItemTemplate>
                        <a class="btn green small inline" href="#Order11">
                            <i class="fa fa-ticket"></i> View Order</a>
                        <a class="btn green small" href="#" onclick="printContent('Ticket11')">
                            <i class="fa fa-print"></i> Print</a>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView> 
        <asp:Label ID="lblRowCount2" runat="server" Text="Label"></asp:Label>

我遇到困难的部分就在这里..

<a class="btn green small inline" href="#Order11">
    <i class="fa fa-ticket"></i> View Order</a>
        <a class="btn green small" href="#" onclick="printContent('Ticket11')">

我想将href="#Order11"替换为数据库中的实际OrderID,并将printContent('Ticket11')再次替换为数据库中的实际OrderID - 因此它将显示为href="#Order12345"和{{1 }}

请记住,我的表中平均有30到50个订单

我遇到困难的是我无法从数据库(在CodeBehind中)和HTML代码块中获取OrderID

1 个答案:

答案 0 :(得分:3)

有几种方法可以做到这一点。假设您的数据库中的OrderID字段是orderId列。

1)您可以使用这样的模板数据绑定:

<asp:TemplateField HeaderText="Action" HeaderStyle-HorizontalAlign="Center">
                <ItemTemplate>
                    <a class="btn green small inline" href='#Order<%# Eval("orderId")%>'>
                        <i class="fa fa-ticket"></i> View Order</a>
                    <a class="btn green small" href="#" onclick="printContent('Ticket<%# Eval("orderId")%>')">
                        <i class="fa fa-print"></i> Print</a>
                </ItemTemplate>
            </asp:TemplateField>

2)您可以使用服务器控件并在网格的ItemDataBound事件上更新它:

模板文件:

<asp:GridView ID="grid1" runat="server" 
    class="striped sortable lcpa_ord_nonprintable" AutoGenerateColumns="False" 
    ShowHeaderWhenEmpty="true" onrowdatabound="grid1_RowDataBound">
    ...
    <asp:TemplateField HeaderText="Action" HeaderStyle-HorizontalAlign="Center">
        <ItemTemplate>
            <asp:HyperLink runat="server" ID="hlView" CssClass="btn green small inline">
                <i class="fa fa-ticket"></i> View Order
            </asp:HyperLink>
            <asp:LinkButton runat="server" ID="lbPrint" CssClass="btn green small">
                <i class="fa fa-print"></i> Print
            </asp:LinkButton>
        </ItemTemplate>
    </asp:TemplateField>
    ...
</asp:GridView>

代码背后:

    protected void grid1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataRow row = (DataRow)e.Row.DataItem;
                HyperLink hlView = (HyperLink)e.Row.FindControl("hlView");
                LinkButton lbPrint = (LinkButton)e.Row.FindControl("lbPrint");
                if (row != null && lbPrint != null && hlView != null)
                {
                    hlView.NavigateUrl = "#Order" + row["orderId"];
                    lbPrint.OnClientClick = string.Format("printContent('Ticket{0}')", row["orderId"]);
                }
            }
        }

还应更改page_load以提高性能并将数据放入DataTable对象中,以便可以在dataBound事件中以DataRow的形式检索它:

        ...
        sqlCmd.Parameters.Add("@startdate", SqlDbType.DateTime);
        sqlCmd.Parameters["@startdate"].Value = "06/01/2015 18:00:00";
        SqlDataAdapter adapter = new SqlDataAdapter(sqlCmd, sqlConn);
        DataTable tblOrder = new DataTable();
        adapter.Fill(tblOrder);

        // get the count
        // @TODO: This is a very expensive method -- there must be a better way
        int count = tblOrder.Rows.Count;
        ...

        grid1.DataSource = tblOrder;
        grid1.DataBind();
        ...