kranthiv
kranthiv

Reputation: 174

How to access Dataset from.cs to .aspx using eval()

I am coding Gridview programmatically. I have dataset in code behind part(.cs),when i am trying to access the dataset in .aspx page using Eval() "Error Creating Control" error is coming.

Is it the Right way using Eval()?

<asp:TemplateField>
    <ItemTemplate>
        <asp:Label ID="lblGridTier" runat="server" Text='<%#Eval(dt.Tables[0].Columns["Tier"])%>'></asp:Label>
    </ItemTemplate>
</asp:TemplateField>

Upvotes: 0

Views: 1370

Answers (3)

Ayhan Dorman
Ayhan Dorman

Reputation: 102

Eval function evaluates fields directly from the selected table of datasource which your grid is bound with. A couple of examples as follows:

<asp:TemplateField>
    <ItemTemplate>
        <asp:Label ID="lblSomeField" runat="server" Text='<%# Eval("field1")%>'></asp:Label>
        <asp:Image ID="lblImageLink" runat="server" ImageUrl='<%# Eval("imagefield", "http://somelink.com/images/{0}")%>' />
        <asp:HyperLink ID="lblMember" runat="server" NavigateUrl='<%# "/member.aspx?id=" + Eval("id") %>' Text='<%# Eval("membername") %>'></asp:HyperLink>
    </ItemTemplate>
</asp:TemplateField>

Upvotes: 0

Andrei
Andrei

Reputation: 56688

If you are doing something like this in the code behind to bind the GridView (and you actually should do something like this):

DateSet ds = ...
GridView1.DataSource = ds;
GridView1.DataBind();

then the correct way to use eval is to give it just the name of the column you want to show.

Text='<%#Eval("Tier")%>'

Upvotes: 2

Aftab Ahmed
Aftab Ahmed

Reputation: 1737

ok please note html is rendered first then your code behind file is called. Now here dataset is null (not yet created) when HTML is being rendered dataset is null so control will not be created and an exception will be thrown. You can use this work around on Page_Load you can set text property of label or check here if dataset is null then don't assign value using Eval. Hope it will help.

Upvotes: 0

Related Questions