How to call a method when binding a TextBox

asp.net

Solution

Something like this should be pretty close to what you're looking for:

<asp:TextBox Id="TextBox1" runat="server" Text='<%# FormatValue(Eval("Name"), Container.DisplayItemIndex) %>' />

And in the code-behind:

public object FormatValue(object value, int itemIndex)
{
    var input = GridView1.Rows[itemIndex].FindControl("TextBox1") as TextBox;
    if (input != null)
    {
        //do whatever you need with the old value
        var oldValue = input.Text.Trim();
    }

    //format the value and send it back
    return string.Format("My name is {0}", value);
}

Problem

I would like to Bind() a TextBox in an EditItemTemplate but I need to pass the original value of the textbox to a function before it's displayed. My goal is to format the value before displaying it. It's a complex formatting rule so I can't use any of the built-in formatters. It's easy to do when working with Eval() but with Bind() it's another story. I know it can be done using events in the code-behind but I was trying to do it all from the aspx page. Example: ``` <EditItemTemplate> <asp:TextBox ID="NameTextBox" Text=<%# Bind("Name") %> MaxLength="255" runat="server" /> </EditItemTemplate> ``` Thanks...

Original source