获取ListView中UserControl TextBox的更新值

时间:2014-03-26 15:33:52

标签: c# asp.net listview user-controls

我有一个页面SendResults.aspx,它包含一个按钮和一个ListView,其中ItemTemplate设置为一个用户控件(3个标签和2个文本框),从匹配的对象中获取它的数据。 在Page_Load上我用List填充数据(这很有效)。 单击按钮时,我想在用户控件的文本框中输入用户输入并对其执行操作。 但是我总是得到初始值而不是更新的值。 这是代码: 用户控件" 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>

用户控制代码隐藏:

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;
            }
        }
    }

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>

SendResults代码隐藏:

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;
        }
    }
}

问题是在这一行: TextBox textBox =(TextBox)match.FindControl(&#34; Team1TextBox&#34;); string txt = textBox.Text;

我总是从数据库中获取初始值,而不是用户更新的输入。

请帮助我了解此事。

1 个答案:

答案 0 :(得分:0)

每次回发时,您的列表都会被覆盖。在Page_Load for SendResults

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

}

除了检查IsPostBack之外,您还需要在ViewState中处理保存控件属性。正如这里建议的那样:User control (ascx) and properties

帖子示例:

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

您可以在控件类中执行此操作。