Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top