如何将HyperLinkField NavigateURL设置为SQL列值C#ASP.Net Web窗体

时间:2017-08-22 15:07:52

标签: c# asp.net sql-server gridview hyperlink

我正在尝试构建gridview,并且一些列需要使用HyperLinkField来显示来自一个字段的文本,同时链接到另一个字段中的文本。在这种情况下,我尝试用于NavigateURL的文本是pdf的名称。我目前用于获取HyperLinkField的NavigateURL文本的方法只是从查询中的字段中获取第一个值,因此将显示正确的文本,但NavigateURL对于除第一个值之外的所有内容都不正确。 / p>

这是aspx页面中的gridview。

<asp:GridView ID="PaperProp" RowStyle-BackColor="#f4f4f4" HeaderStyle-BackColor="#CFDBE5" runat="server" AutoGenerateColumns="False" >
                        <columns>
                        </columns>
                        </asp:GridView> 

以下是我用来构建gridview的代码背后的代码

string PaperPropQuery = "SELECT bidd_number, bidd_name, bidd_desc, prebid_req, open_dt, buyer, bidd_status, Addendum FROM vPaper_Proposals ORDER BY CONVERT(DateTime, open_dt,101) DESC";
        SqlCommand cmd2 = new SqlCommand(PaperPropQuery, new SqlConnection(ConStr));

        cmd2.Connection.Open();
        var cmdr2 = cmd2.ExecuteReader();

            if (cmdr2.Read())
            {
                LinkField = new HyperLinkField();

                LinkField.HeaderText = "RFP No.";
                LinkField.DataTextField = "bidd_number";
                LinkField.HeaderStyle.CssClass = "GVpadding";
                LinkField.ItemStyle.CssClass = "GVpadding";
                LinkField.NavigateUrl = "./bids/" + cmdr2["bidd_name"].ToString();
                LinkField.Target = "_blank";

                PaperProp.Columns.Add(LinkField);

                TextField = new BoundField();

                TextField.HeaderText = "Description";
                TextField.DataField = "bidd_desc";
                TextField.HeaderStyle.CssClass = "GVpadding";
                TextField.ItemStyle.CssClass = "GVpadding";

                PaperProp.Columns.Add(TextField);

                LinkField = new HyperLinkField();

                LinkField.HeaderText = "PreBid";
                LinkField.DataTextField = "prebid_req";
                LinkField.HeaderStyle.CssClass = "GVpadding";
                LinkField.ItemStyle.CssClass = "GVpadding";

                PaperProp.Columns.Add(LinkField);

                TextField = new BoundField();

                TextField.HeaderText = "Open Date";
                TextField.DataField = "open_dt";
                TextField.HeaderStyle.CssClass = "GVpadding";
                TextField.ItemStyle.CssClass = "GVpadding";

                PaperProp.Columns.Add(TextField);

                LinkField = new HyperLinkField();

                LinkField.HeaderText = "Buyer";
                LinkField.DataTextField = "buyer";
                LinkField.HeaderStyle.CssClass = "GVpadding";
                LinkField.ItemStyle.CssClass = "GVpadding";
                LinkField.NavigateUrl = "";

                PaperProp.Columns.Add(LinkField);

                TextField = new BoundField();

                TextField.HeaderText = "Status";
                TextField.DataField = "bidd_status";
                TextField.HeaderStyle.CssClass = "GVpadding";
                TextField.ItemStyle.CssClass = "GVpadding";

                PaperProp.Columns.Add(TextField);

                LinkField = new HyperLinkField();

                LinkField.HeaderText = "Addendum";
                LinkField.DataTextField = "Addendum";
                LinkField.HeaderStyle.CssClass = "GVpadding";
                LinkField.ItemStyle.CssClass = "GVpadding";

                PaperProp.Columns.Add(LinkField);

                cmdr2.Close();

                PaperProp.DataSource = cmd2.ExecuteReader();
                PaperProp.DataBind();

                cmd2.Connection.Close();
                cmd2.Connection.Dispose();
            }
            else
            {
                PaperProplbl.Text = "None at this time.";
            }

我编码不多,而且我对C#很陌生,所以我知道这可能是查询数据库和构建网格视图的一种可怕方式,因为我对任何一个开放关于如何改进代码的建议,但我的主要问题是如何获得

LinkField.NavigateUrl =“./ bids /”+ cmdr2 [“bidd_name”]。ToString();

包含字段中每行的bidd_name而不是第一行?

1 个答案:

答案 0 :(得分:0)

找到我自己的答案。您可以使用DataNavigateUrlFields和DataNavigateUrlFormatString来创建表示查询数据的URL。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>SpringBoot</groupId>
    <artifactId>SpringWebAngularJS</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>SpringWebAngularJS</name>
    <description>Spring Boot Web AngularJs Example</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.4.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-couchbase</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
        </dependency>

        <dependency>
            <groupId>com.github.ulisesbocchio</groupId>
            <artifactId>jasypt-spring-boot-starter</artifactId>
            <version>1.10</version>
        </dependency>

        <!-- <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>bootstrap</artifactId>
            <version>3.3.7</version>
        </dependency>
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>jquery</artifactId>
            <version>3.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>angularjs</artifactId>
            <version>1.6.4</version>
        </dependency> -->

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>