aspx文件中对GridView进行硬编码

aspx文件中对GridView进行硬编码

本文介绍了如何在我的.aspx文件中对GridView进行硬编码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个带有静态数据的GridView,并没有链接到任何数据库或数据源,我想直接在我的aspx文件中对其进行硬编码。

我是全新的ASP.NET,不知道自己在做什么,无论出于何种原因,我在网上找不到任何有关如何操作的信息。



我试图创建一个标题为Hello World和两个数据项Hello和World的单列表。我在尝试,但运行时在页面上没有显示出来:

 < asp:GridView ID = GridViewrunat =server> 
< Columns>
< asp:TemplateField HeaderText =Hello World>
< ItemTemplate>
< asp: Label ID =lblHellorunat =serverText =Hello/>
< / ItemTemplate>
< ItemTemplate>
< asp:Label ID =lblWorld runat =serverText =World/>
< / ItemTemplate>
< / asp:TemplateField>
< / Columns>
< / asp :GridView>


决方案

您希望分配IEnumerable,DataSet或DataTable来在GridView中显示数据。

.stack.imgur.com / 3X4VT.pngalt =

 < asp:GridView ID =GridViewrunat =serverAutoGenerateColumns =False> 
<列>
< asp:TemplateField HeaderText =Hello World>
< ItemTemplate>
< asp:Label ID =lblHellorunat =server
Text ='<%#Eval(Text1)%>'/>
< asp:Label ID =lblWorldrunat =server
Text ='<%#Eval(Text2)%>'/>
< / ItemTemplate>
< / asp:TemplateField>
< /列>
< / asp:GridView>

public class Item
{
public string Text1 {get;组; }
public string Text2 {get;组; }


protected void Page_Load(object sender,EventArgs e)
{
GridView.DataSource = new List< Item>
{
new Item {Text1 =Hello,Text2 =World}
};
GridView.DataBind();
}



更新:



 < script runat =server> 
protected void Page_Load(object sender,EventArgs e)
{
GridView.DataSource =
new Dictionary< string,string> { { 你好,世界 } };
GridView.DataBind();
}
< / script>
< asp:GridView ID =GridViewrunat =serverAutoGenerateColumns =False>
<列>
< asp:TemplateField HeaderText =Hello World>
< ItemTemplate>
< asp:Label ID =lblHellorunat =server
Text ='<%#Eval(Key)%>'/>
< asp:Label ID =lblWorldrunat =server
Text ='<%#Eval(Value)%>'/>
< / ItemTemplate>
< / asp:TemplateField>
< /列>
< / asp:GridView>


I want to have a GridView with static data in it, not linked to any database or data source, and I would like to hard-code it directly in my aspx file.

I'm brand new to ASP.NET and have no idea what I'm doing, and for whatever reason I can't find anything online about how to do this.

I'm trying to create a one-column table with a heading of "Hello World" and two data items, "Hello" and World". Here is what I'm trying, but nothing is showing up on the page when I run it:

<asp:GridView ID="GridView" runat="server">
    <Columns>
        <asp:TemplateField HeaderText ="Hello World">
            <ItemTemplate>
                <asp:Label ID="lblHello" runat ="server" Text ="Hello"/>
            </ItemTemplate>
            <ItemTemplate>
                <asp:Label ID="lblWorld" runat ="server" Text ="World"/>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
解决方案

You want to assign either IEnumerable, DataSet or DataTable to display data in GridView.

<asp:GridView ID="GridView" runat="server" AutoGenerateColumns="False">
    <Columns>
        <asp:TemplateField HeaderText="Hello World">
            <ItemTemplate>
                <asp:Label ID="lblHello" runat="server"
                Text='<%# Eval("Text1") %>' />
                <asp:Label ID="lblWorld" runat="server"
                Text='<%# Eval("Text2") %>' />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

public class Item
{
    public string Text1 { get; set; }
    public string Text2 { get; set; }
}

protected void Page_Load(object sender, EventArgs e)
{
    GridView.DataSource = new List<Item>
        {
            new Item {Text1 = "Hello", Text2 = "World"}
        };
    GridView.DataBind();
}

Updated:

<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        GridView.DataSource =
            new Dictionary<string, string> { { "Hello", "World" } };
        GridView.DataBind();
    }
</script>
<asp:GridView ID="GridView" runat="server" AutoGenerateColumns="False">
    <Columns>
        <asp:TemplateField HeaderText="Hello World">
            <ItemTemplate>
                <asp:Label ID="lblHello" runat="server"
                Text='<%# Eval("Key") %>' />
                <asp:Label ID="lblWorld" runat="server"
                Text='<%# Eval("Value") %>' />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

这篇关于如何在我的.aspx文件中对GridView进行硬编码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 02:27