0

我可以在我的 ASPX 页面中创建一个自定义链接,如下所示:

<asp:GridView 

    ID="GridView1" 
    CssClass="rounded-corner" 
    runat="server" 
    AutoGenerateColumns="false"
    EnableModelValidation="True" 
    AllowSorting="True"
    onsorting="GridView1_Sorting" 
    EnableViewState="true"
    AllowPaging="True"   
    PageSize="10"
    OnPageIndexChanging="GridView1_PageIndexChanging" >

        <Columns>
            <asp:BoundField DataField="DATE_ENTERED" HeaderText="Date Submitted" dataformatstring="{0:MMMM d, yyyy}" htmlencode="false" />
            <asp:BoundField DataField="CLIENT_CLAIM_NUM" HeaderText="Your Claim #" />
            <asp:BoundField DataField="PRIMARY_INSURED" HeaderText="Insured" />
            <asp:BoundField DataField="PRIMARY_CLAIMANT" HeaderText="Claimant" />
            <asp:BoundField DataField="LOB_ID" HeaderText="Type" />

            <asp:TemplateField HeaderText="Branch">
                <ItemTemplate>
                   <asp:HyperLink ID="BRANCH_NAME" runat="server" NavigateUrl='<%# "mailto:"+Eval("OWNERS_EMAIL") %>' Text='<%# Eval("BRANCH_NAME") %>' ToolTip='<%# 
                   "<span style=\"text-decoration:underline;\">"+Eval("BRANCH_NAME").ToString().Trim() + "</span><br />" + 
                   Eval("OWNERS_FIRST_NAME").ToString().Trim() + " " +
                   Eval("OWNERS_LAST_NAME").ToString().Trim() + "<br />" + 
                   Eval("OWNERS_EMAIL").ToString().Trim() + "<br />" + 
                   Eval("OWNERS_OFFICE_PHONE").ToString().Trim() + "<br />" + 
                   Eval("OWNERS_FAX_PHONE").ToString().Trim()
                   %> '> 
                   </asp:HyperLink>
                </ItemTemplate>
            </asp:TemplateField>

        </Columns>

</asp:GridView>

相反,是否有一种方法可以直接在我的代码中执行此操作;通过创建一种“计算字段?”

后面的代码如下:

DbProviderFactory dbf = DbProviderFactories.GetFactory();
using ( IDbConnection con = dbf.CreateConnection() )
{
    string sSQL;
    // 12/04/2006 Paul.  Only include active users. 
    sSQL = "select top 20                "         + ControlChars.CrLf
         + " DATE_ENTERED, CLIENT_CLAIM_NUM, PRIMARY_INSURED, PRIMARY_CLAIMANT, LOB_ID, BRANCH_NAME," + ControlChars.CrLf
         + " OWNERS_FIRST_NAME, OWNERS_LAST_NAME, OWNERS_NAME, OWNERS_EMAIL, OWNERS_OFFICE_PHONE, OWNERS_FAX_PHONE" + ControlChars.CrLf
         + "  from vwFILES_DetailViewWithOwners" + ControlChars.CrLf
         + " where 1 = 1                   " + ControlChars.CrLf;
    using ( IDbCommand cmd = con.CreateCommand() )
    {
        cmd.CommandText = sSQL;

        using ( DbDataAdapter da = dbf.CreateDataAdapter() )
        {
            ((IDbDataAdapter)da).SelectCommand = cmd;
            using ( DataTable dt = new DataTable() )
            {
                da.Fill(dt);
                vwMain = dt.DefaultView;
                GridView1.DataSource = vwMain;
                if ( !IsPostBack )
                {

                    GridView1.DataBind();
                }
            }
        }
    }
}

我可以简单地在我的代码中创建新字段,这样我就可以这样称呼它:

<asp:TemplateField HeaderText="Branch">
    <ItemTemplate>
       <asp:HyperLink ID="BRANCH_NAME" 
            runat="server" 
            NavigateUrl='<%# "mailto:"+Eval("OWNERS_EMAIL") %>' 
            Text='<%# Eval("BRANCH_NAME") %>' 
            ToolTip='<%# Eval("MY_CALCULATED_FIELD") %>'> 
       </asp:HyperLink>
    </ItemTemplate>
</asp:TemplateField>

谢谢你的建议。

4

1 回答 1

2

您可以在 C# 代码中创建一个函数来格式化数据。MSDN上的这篇文章谈到了它:http: //msdn.microsoft.com/en-us/library/bb288032.aspx#aspnett12ustmpfldsvb_topic5

总而言之,您可以像这样更改您的项目模板:

<ItemTemplate>
    <asp:HyperLink ID="BRANCH_NAME" runat="server" 
          NavigateUrl='<%# "mailto:"+Eval("OWNERS_EMAIL") %>' 
          Text='<%# Eval("BRANCH_NAME") %>' 
          ToolTip='<%# GetBranchAddress(CType(Container.DataItem, System.Data.DataRowView).Row) %> '> 
    </asp:HyperLink>
</ItemTemplate>

在你的 c# 中,创建一个这样的函数:

protected string GetBranchAddress(DataRow row)
{
    return "<span style=\"text-decoration:underline;\">"+ 
               row["BRANCH_NAME"].ToString().Trim() + "</span><br />" + 
               row["OWNERS_FIRST_NAME"].ToString().Trim() + " " +
               row["OWNERS_LAST_NAME"].ToString().Trim() + "<br />" + 
               row["OWNERS_EMAIL"].ToString().Trim() + "<br />" + 
               row["OWNERS_OFFICE_PHONE"].ToString().Trim() + "<br />" + 
               row["OWNERS_FAX_PHONE"].ToString().Trim();

}

这种方法也很好,因为您可以在 c# 函数中设置断点并添加错误处理等。

于 2013-02-08T21:04:23.957 回答