使用绑定控件动态创建asp.net gridview模板字段... [英] Dynamically creating asp.net gridview template fields with bound controls...

查看:57
本文介绍了使用绑定控件动态创建asp.net gridview模板字段...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试动态地从C#创建gridview。 gridview代码如下。如何从C#中完全创建它。

I am trying to create a gridview from C# dynamically. The gridview code is below. How can I create this completely from C#.

<asp:GridView ID="ArticlesGridView" runat="server" AutoGenerateColumns="False" DataSourceID="ArticlesDataSource">
    <Columns>
        <asp:BoundField DataField="DateTimeUploaded" HeaderText="Date" SortExpression="DateTimeUploaded" />
        <asp:BoundField DataField="ArticleType" HeaderText="Type" SortExpression="ArticleType" />
        <asp:TemplateField HeaderText="Title" SortExpression="Title">
            <EditItemTemplate>
                <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Title") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:HyperLink ID="HyperLink1" runat="server" Text='<%# Bind("Title") %>' NavigateUrl='<%# Eval("Link") %>' />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

这是我到目前为止所拥有的。我没有运气使绑定的文本框和超链接正常工作。我需要的是"标题"列将标题显示为超链接。超链接源位于名为"链接"的列中。

Here is what I have so far. I am not having any luck getting the bound textbox and hyperlink to work correctly. What I need is for the 'Title' column to display the title as a hyperlink. The hyperlink source is in a column named 'Link'.

SqlDataSource ArticlesDataSource = new SqlDataSource();
            ArticlesDataSource.ID = "ArticlesDataSource"; 
            ArticlesDataSource.ConnectionString = @"Data Source=MAHONEY;Initial Catalog=SQL2K5;Integrated Security=True";

            ArticlesDataSource.SelectCommand = "SELECT * FROM MKTG";

            SqlConnection ArticlesConnection = new SqlConnection(ArticlesDataSource.ConnectionString);
            SqlCommand ArticlesCommand = new SqlCommand("SELECT * FROM MKTG", ArticlesConnection);

            GridView ArticlesGridView = new GridView(); 
            ArticlesGridView.ID = "ArticlesGridView"; 
            ArticlesGridView.AutoGenerateColumns = false; 
            ArticlesGridView.AutoGenerateColumns = true; 
            this.Controls.Add(ArticlesGridView);
            ArticlesGridView.DataSource = ArticlesDataSource; 

            TextBox txtLink = new TextBox(); 
            txtLink.ID = "txtLink";
            txtLink.Text = @"<%# Bind(" + '\u0022' + "Title" + '\u0022' + ") %>"; 
            ArticlesGridView.Controls.Add(txtLink);

            HyperLink hypLink = new HyperLink();

            string sTitle = "--"; //This needs to be set to the 'Title' column value which is a Text field... (nvarchar(150))
            string sLink = "<%# Eval(" + '\u0022' + "Link" + '\u0022' + ") %>"; //This needs to be set to the 'Link' column value which is a Text field... (nvarchar(150))

            hypLink.Text = sTitle;
            hypLink.ID = "hypLink";



            //I TRIED THIS, BUT IT SENDS TO AN ERROR PAGE SAYING "HTTP Error 400 - Bad Request.".
            //Also, the url it's trying to reach ends in this, "/%3C%#%20Eval%28%22Link%22%29%20%%3E"
            hypLink.NavigateUrl = sLink;

            ArticlesGridView.Controls.Add(hypLink);
                                
            TemplateField tField = new TemplateField(); tField.HeaderText = "Title"; tField.SortExpression = "Title";
                
            ArticlesConnection.Open();
            ArticlesGridView.AllowPaging = false;
                
            ArticlesGridView.DataBind();

            TableCell cell = new TableCell();

            ArticlesGridView.Rows[0].Cells.Add(cell);
            ArticlesGridView.Rows[1].Cells[1].Controls.Add(hypLink);




JCassity



JCassity

推荐答案

超链接有一个特定的列类型;这将使最后一栏更容易。  您只需在数据源中指定确定URL和文本的字段名称(或者您可以指定静态值;两者都有字段)。
There is a specific column type for hyperlinks; that will make the last column much easier.  You can simply specify the name of the field in the datasource that determines the URL and text (or you can specify a static value; there are fields for both).


这篇关于使用绑定控件动态创建asp.net gridview模板字段...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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