如何为动态创建的超链接字段设置文本属性 [英] How to set text property for dynamically created hyperlinkfield

查看:81
本文介绍了如何为动态创建的超链接字段设置文本属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码的所有方面都能正常运行,但我意识到我的HyperLinkField的URL太长而无法正常显示。如何将text属性设置为单词View。如下所示,我已经将text属性设置为View,但动态创建的URL会将文本写入。

All aspects of my code works and runs properly but I realized the URL for my HyperLinkField is too long to display properly. How do I set the text property to the word "View". As you can see below I've already set the text property to "View" but the dynamically created URL over writes the text.

<div id="first" style="float:left;">
             <asp:GridView ID="GridView3" runat="server" AutoGenerateColumns="False" CellPadding="4" CellSpacing="1" HorizontalAlign="Center">
                    <Columns>
                        <asp:BoundField DataField="AccountNo" HeaderText="AccountNo" SortExpression="AccountNo" />
                        <asp:BoundField DataField="State" HeaderText="State" SortExpression="State" />
                        <asp:BoundField DataField="DocumentType" HeaderText="DocumentType" SortExpression="DocumentType" />
                        <asp:BoundField DataField="Error_MSG" HeaderText="Error_MSG" SortExpression="Error_MSG" />
                        <asp:BoundField DataField="Intent" HeaderText="Intent" SortExpression="Intent" />
                        <asp:BoundField DataField="LoadDate" HeaderText="LoadDate" SortExpression="LoadDate" />
                        <asp:HyperLinkField DataTextField="EncryptedURL" DataNavigateUrlFields="EncryptedURL" Text="View"  HeaderText="EncryptedURL"/>
                    </Columns>
             </asp:GridView>
       </div><pre lang="c#">





这是在页面加载事件中触发的代码





Here's the code behind that fires off in the page load event

using (SqlConnection con = new SqlConnection("Data Source=test;Initial Catalog=test;User ID=te;Password=great"))
{
    lblAcct.Text = Request.QueryString["Account"];
    con.Open();
    SqlCommand cmd = new SqlCommand(" Select * from test.dbo.test where AccountNo = '" + lblAcct.Text + "'", con);
    SqlDataReader dr = cmd.ExecuteReader();
    GridView3.DataSource = dr;
    GridView3.DataBind();
    con.Close();
}





我尝试过:



同样,我已经尝试将text属性设置为View,但由于数据被动态添加到gridview中,因此文本会被覆盖。



有什么想法吗?



What I have tried:

Again, I have already tried setting the text property to "View" but since the data is dynamically added to the gridview the text gets over written.

Any thoughts?

推荐答案

你已经设置了 Text DataTextField 属性。 DataTextField 将使用数据源中的值覆盖 Text 值。



删除 DataTextField 属性,它应该开始使用 Text 属性:

You've set both the Text and the DataTextField properties. The DataTextField will overwrite the Text value with the value from the data source.

Remove the DataTextField property, and it should start using the Text property instead:
<asp:HyperLinkField DataNavigateUrlFields="EncryptedURL" Text="View"  HeaderText="EncryptedURL"/>





然而,你有一个严重的安全漏洞 [ ^ ]。您应立即修复此问题,并查看您编写的任何其他数据库代码以检查并修复漏洞。



你想知道关于SQL注入的一切(但不敢问)特洛伊亨特 [ ^ ]

如何在没有技术术语的情况下解释SQL注入? |信息安全堆栈交换 [ ^ ]

查询参数化备忘单| OWASP [ ^ ]





However, you have a serious security vulnerability[^] in your code. You should fix that immediately, and review any other database code you've written to check for and fix the vulnerability.

Everything you wanted to know about SQL injection (but were afraid to ask) | Troy Hunt[^]
How can I explain SQL injection without technical jargon? | Information Security Stack Exchange[^]
Query Parameterization Cheat Sheet | OWASP[^]

string account = Request.QueryString["Account"];
lblAcct.Text = Server.HtmlEncode(account); // Encode the value to avoid cross-site scripting

using (SqlConnection con = new SqlConnection("Data Source=test;Initial Catalog=test;User ID=te;Password=great"))
using (SqlCommand cmd = new SqlCommand("Select * from test.dbo.test where AccountNo = @AccountNo", con))
{
    cmd.Parameters.AddWithValue("@AccountNo", account); // Use parameters to avoid SQL Injection
    
    con.Open();
    using (SqlDataReader dr = cmd.ExecuteReader())
    {
        GridView3.DataSource = dr;
        GridView3.DataBind();
    }
}


这篇关于如何为动态创建的超链接字段设置文本属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆