Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7647051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:22:00+00:00 2026-05-31T10:22:00+00:00

Code Behind (C#): protected void Page_Load(object sender, EventArgs e) { try { if (!Page.IsPostBack)

  • 0

Code Behind (C#):

protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!Page.IsPostBack)
            {
                List<string> lst = new List<string>();

                lst.Add("1");
                lst.Add("2");

                dlSample.DataSource = lst;
                dlSample.DataBind();
            }
        }
        catch (Exception ex)
        {

            throw;
        }
    }

    protected void dlSample_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        try
        {
            if (e.Item.DataItem.ToString().Equals("1"))
                e.Item.DataItem = "one";
        }
        catch (Exception ex)
        {

            throw;
        }
    }

ASP:

<asp:DataList ID="dlSample" runat="server" OnItemDataBound="dlSample_ItemDataBound">
        <ItemTemplate>
            <asp:Label ID="lbl" runat="server" Text='<%# Eval("") %>'></asp:Label>
        </ItemTemplate>
        </asp:DataList>

I have used List on my code and inserting items on it. After that I have binded it programmatically and on my ItemDataBound event I have modified an item at run-time. I have problem on displaying the items on the DataList control. My question is how would I display it using the Eval data-binding expression on my ASP or is there any approach aside on Eval?

Thank you very much in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T10:22:02+00:00Added an answer on May 31, 2026 at 10:22 am

    At the time when dlSample_ItemDataBound method is called, the expressions in the ItemTemplate are already evaluated and, even the DataItem is changed, the effect won’t be reflected.

    You can use the following code block.

    <asp:Label ID="lbl" runat="server" 
             Text='<%# (string)Container.DataItem == "1" ? "one" : (string)Container.DataItem %>'
             >
    </asp:Label>
    

    You can remove the OnItemDataBound="dlSample_ItemDataBound", because won’t be used anymore.

    As an alternative, if you still want to use this handler:

    <asp:DataList ID="dlSample" runat="server" OnItemDataBound="dlSample_ItemDataBound">
         <ItemTemplate>
              <asp:Label ID="lbl" runat="server"></asp:Label>
          </ItemTemplate>
    </asp:DataList>
    
    protected void dlSample_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            //dataitem is supposed to be a string object, so you can cast it to string, no need to call ToString()
            var item = (string)e.Item.DataItem;
    
            // find the label with "lbl" ID, use e.Item as the Naming Container
            var lbl = (Label)e.Item.FindControl("lbl");
            if (item == "1")
               lbl.Text = "one";
            else
               lbl.Text = item;
        }
    

    I always prefer the first way to do these things.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have AutoEventWireup=true and in my code behind protected void Page_Init(object sender, EventArgs e)
Here’s my code behind: protected void gvviewapproval_RowUpdated(object sender, GridViewUpdatedEventArgs e) { using (SqlConnection cn
In my code behind I wire up my events like so: protected override void
Code Behind: [WebMethod] public static string emp() { return BlaBla; } Aspx Page: $(document).ready(function()
The code behind file creates a list of employees and the asp.net page loops
In code-behind of an ASP.NET page I have this method: public string TestFunc() {
I tried this, but could not get through:- code behind protected HtmlTableRow trComment; protected
In code-behind I can do this to select something: // Select item in first
ASP.Net: In code-behind I can simulate <%# Eval(Property)%> with a call to DataBinder.Eval(myObject,Property); How
I am currently publishing code behind .aspx in SharePoint. I can automatically publish the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.