1

I have a page SendResults.aspx that holds a button and a ListView with ItemTemplate set to a user control (3 labels and 2 textboxes) that gets it's data from a matching object. On Page_Load I fill the List with data (this works well). When the button is clicked I want to take the user input in the user-control's textboxes and do something with it. However I always get the initial value and not the updated one. Here is the code: The user-control "MatchControl.ascx"

    <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="MatchControl.ascx.cs" Inherits="TotoMondeal.Controls.MatchControl" %>
<div>
    <asp:Image ID="Team1FlagImage" runat="server" />
<asp:Label ID="Team1Label" runat="server" Width="150px"></asp:Label>
<asp:TextBox ID="Team1TextBox" runat="server" MaxLength="2" TextMode="Number" Width="50px" AutoPostBack="true" OnTextChanged="Team1TextBox_TextChanged"></asp:TextBox>
<asp:Label ID="Colon" runat="server" Font-Size="XX-Large" Text=":"></asp:Label>
<asp:TextBox ID="Team2TextBox" runat="server" MaxLength="2" TextMode="Number" Width="50px"></asp:TextBox>
<asp:Label ID="Team2Label" runat="server" Width="150px"></asp:Label>
    <asp:Image ID="Team2FlagImage" runat="server" />
</div>

The user-control code-behind:

public partial class MatchControl : System.Web.UI.UserControl
{
            public Match Match
    {
        get
        {
            object obj = ViewState["Match"];
            return (obj == null) ? new Match() : (Match)obj;
        }
        set
        {
            ViewState["Match"] = value;
        }
    }

    public string Team1Score
    {
        get { return Team1TextBox.Text; }
        set { Team1TextBox.Text = value; }
    }
    public string Team2Score
    {
        get { return Team2TextBox.Text; }
        set { Team2TextBox.Text = value; }
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        Team1Label.Text = Match.Team1Name;
        Team2Label.Text = Match.Team2Name;

        Team1TextBox.Text = Match.Team1Score.ToString();
        Team2TextBox.Text = Match.Team2Score.ToString();

        Team1TextBox.Enabled = Match.EnableTextBox;
        Team2TextBox.Enabled = Match.EnableTextBox;

        Team1FlagImage.ImageUrl = @"~/FlagImages/" +Match.Team1Name + ".png";
        Team2FlagImage.ImageUrl = @"~/FlagImages/" + Match.Team2Name + ".png";

    }


    protected void Team1TextBox_TextChanged(object sender, EventArgs e)
    {
        TextBox textBox = sender as TextBox;

        if (textBox != null)
        {
            try
            {
                Match updatedMatch = new Match()
                {
                    MatchId = Match.MatchId,
                    MatchDate = Match.MatchDate,
                    Result = Match.Result,
                    Team1Name = Match.Team1Name,
                    Team1Score = Convert.ToInt32(textBox.Text),
                    Team2Name = Match.Team2Name,
                    Team2Score = Match.Team2Score,
                    EnableTextBox = Match.EnableTextBox
                };

                Match = updatedMatch;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }

The SendResults.aspx:

<%@ Page Title="שלח תוצאות" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="SendResults.aspx.cs" Inherits="TotoMondeal.SendResults" %>
<%@ Register TagPrefix="TOTO" TagName="MatchControl" Src="~/Controls/MatchControl.ascx" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
    <h2><%: Title %>.</h2>
    <div class="jumbotron">
        <asp:ListView ID="TodayMatchesList" runat="server">
            <ItemTemplate>
                <TOTO:MatchControl ID="MatchControl" Match="<%# Container.DataItem %>" runat="server" />
            </ItemTemplate>
        </asp:ListView>

        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />

    </div>
</asp:Content>

the SendResults code-behind:

public partial class SendResults : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
            List<Match> matches = new List<Match>();
            matches = Queries.GetTodayMatches(DateTime.Now);
            foreach (Match match in matches)
            {
                match.EnableTextBox = true;
            }
            this.TodayMatchesList.DataSource = matches;
            this.TodayMatchesList.DataBind();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < TodayMatchesList.Items.Count; i++)
        {
            MatchControl match = (MatchControl)TodayMatchesList.Items[i].FindControl("MatchControl");
            TextBox textBox = (TextBox)match.FindControl("Team1TextBox");
            string txt = textBox.Text;
        }
    }
}

The problem is that in this line: TextBox textBox = (TextBox)match.FindControl("Team1TextBox"); string txt = textBox.Text;

I always get the initial value from the database, and not the user updated input.

Please help I'm new at this.

Nir Schachter
  • 155
  • 1
  • 12

1 Answers1

0

Your List is getting overwritten every time you post back. Add this in Page_Load for SendResults

if ( !Page.IsPostBack )
{
    List<Match> matches = new List<Match>();
    matches = Queries.GetTodayMatches(DateTime.Now);
    ...etc...

}

In addition to checking IsPostBack you need to handle saving your control properties in the ViewState. As suggested here: User control (ascx) and properties

Example from post:

public string Title {
    get { return Convert.ToString(ViewState["Title"]); }
    set { ViewState["Title"] = value; }
}

You would do this in your control class.

Community
  • 1
  • 1
Rick S
  • 6,476
  • 5
  • 29
  • 43
  • Hi, I forgot to mention that I tried that, when I do I get a Null exception on the MatchControl Page_Load. Team1Label.Text = Match.Team1Name; the Match object is null... – Nir Schachter Mar 26 '14 at 15:42
  • I added another comment in my answer. You need to save your properties in the ViewState so they are persisted on postback. – Rick S Mar 26 '14 at 15:53
  • So after working on this for the last 2 hours I still don't get it. I have updated my code here for you to see, I guess this is not how I'm supposed to use the ViewState, also On the OnTextChange event of Team1TextBox I have an issue at this line: Team1Score = Convert.ToInt32(textBox.Text). I get the old value from the textbox and not the new one. – Nir Schachter Mar 26 '14 at 18:55
  • sorry for being such a bad student... can you help me a bit more with my last comment? – Nir Schachter Mar 27 '14 at 07:56
  • Without seeing all of your code it's hard for me to recommend what's next. Can't you step through the code and see what's happening? – Rick S Mar 27 '14 at 14:35
  • I should have added the if ( !Page.IsPostBack ) in the user control, now it's working. Thanks for all the help – Nir Schachter Mar 27 '14 at 15:41