1

Here are TextBox and is's event handler:

<asp:Repeater ID="rpt_users" runat="server" OnItemCommand="rpt_users_ItemCommand" OnItemDataBound="rpt_users_ItemDataBound"><ItemTemplate>
        <tr class="c0">
            <td>
                <asp:TextBox runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "daysleft") %>' OnTextChanged="Unnamed_TextChanged" AutoPostBack="true"/>
            </td>
        </tr>
    </ItemTemplate></asp:Repeater>

protected void Unnamed_TextChanged(object sender, EventArgs e)
{
    var repeaterItem = (sender as TextBox).NamingContainer as RepeaterItem;
    var hiddenFieldKey = repeaterItem.FindControl("LoginField") as HiddenField;
    DataTable data = ViewState["Data"] as DataTable;
    var dataRow = data.Rows.Find(hiddenFieldKey.Value);
    var userName = (dataRow[0].ToString()).Replace("'", "''");
    ... //more actions below
}

This event handler can't be triggered now. It worked fine sometimes ago, but recently I deleted Page_Init event handler from code-behind and replace it with Page_Load event handler. Can these facts be connected? Which conditions should be complied to make this handler reachable?

6
  • I think TextChanged Event doesn't work in Web Applications, it works only with Windows Form Applications... You can rather use JavaScript or jQuery for the same..... Commented Aug 28, 2014 at 12:25
  • Are you keeping this Texbox inside the DataGrid or ListView Control or any other data binding control? Can you post the markup? Commented Aug 28, 2014 at 12:28
  • @AbhayPrince TextChanged exists in Web Forms msdn.microsoft.com/en-us/library/….
    – Tasos K.
    Commented Aug 28, 2014 at 12:28
  • @Deepu, it is placed in Repeater. I shared more details.
    – splash27
    Commented Aug 28, 2014 at 12:33
  • Can you share code of Page_Load mehod (code that was in Page_Init)?
    – ntl
    Commented Aug 28, 2014 at 12:43

1 Answer 1

1

Repeater data binding should be inside not is postback condition

public class Test
{
    public string daysleft { get; set; }
}

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindRepeaterItems();
        }
    }

    private void BindRepeaterItems()
    {
         var items = new List<Test>
         {
                new Test {daysleft = "Deepu"},
                new Test {daysleft = "Darsh"}
         };
         rpt_users.DataSource = items;
         rpt_users.DataBind();
    }

    protected void Unnamed_TextChanged(object sender, EventArgs e)
    {
        var repeaterItem = (sender as TextBox).NamingContainer as RepeaterItem;
        var hiddenFieldKey = repeaterItem.FindControl("LoginField") as HiddenField;
    }
}

<asp:Repeater ID="rpt_users" runat="server">
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "daysleft") %>' OnTextChanged="Unnamed_TextChanged" AutoPostBack="true"/><br />
</ItemTemplate>
</asp:Repeater>
5
  • It solved the problem with handler, but cause another problem. It makes page selector dissapear after every partial postback. For example, I changed value in textbox or selected second page in page selector, and page selector dissapears.
    – splash27
    Commented Aug 28, 2014 at 13:14
  • if your first problem solved, can you mark up vout / answer so that it would be useful for others :) Commented Aug 28, 2014 at 13:18
  • You mean second page or second textbox control? Commented Aug 28, 2014 at 14:04
  • I mean second problem. if I use (!IsPostBack) over all code in Page_load, some controls such as page selector and sort type selector (based on DropDownList) stop work. Which part of code should be placed inside of (!IsPostBack) statement? Only rpt_users.DataSource = items; rpt_users.DataBind(); ? Or data preparation also must be inside?
    – splash27
    Commented Aug 28, 2014 at 14:14
  • check the modified code... call BindRepeaterItems() method when you needed for sort / any other place Commented Aug 28, 2014 at 14:20

Not the answer you're looking for? Browse other questions tagged or ask your own question.