获取ListView中UserControl文本框的更新值

本文关键字:更新 文本 ListView UserControl 获取 | 更新日期: 2023-09-27 17:53:49

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

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

请帮帮我,我是新手。

获取ListView中UserControl文本框的更新值

你的列表被覆盖,每次你发回来。在Page_Load中添加SendResults

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

除了检查IsPostBack,你还需要在ViewState中保存你的控件属性。用户控件(ascx)和属性

from post:

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

您可以在控制类中这样做。