为用户控件单击提交按钮后未调用绑定事件的原因(添加代码)

时间:2014-03-16 01:49:24

标签: asp.net user-controls runtime repeater

我有一个使用单用户控件的网页和一个使用相同用户控件的asp:repeater,它们都是在Page_Load中创建的。在提交按钮点击事件中,两者都有所不同。在page_load之前单击提交按钮后,将调用每个转发器(depdentBasicInfo)的用户控件的绑定事件。但绑定事件不适用于单用户控件(spouseBasicInfo)。为什么?用户控件也创建一个运行时控件(假设一个TextBox)。我发现在单击提交后无法检索运行时控件的Text属性,因为在PostBack之后控件变为null。如何轻松检索财产?任何人都可以帮助我吗?感谢。

<!--USER CONTROL -->
<asp:Panel runat="server" ID="PnlSpouseInformation" Visible="true">
<h3 id="ApplicantLabel" runat="server"></h3>
<div class="dependentInformation">
    <asp:PlaceHolder ID="phDependentInformation" runat="server"></asp:PlaceHolder>
</div>
</asp:Panel>


<!--  code behind   -->
    public partial class userInfo : System.Web.UI.UserControl
{
    public string identity;
    public string  applicantTitle
    {
        set { ApplicantLabel.InnerText = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    public void Bind()
    {
        WebControl textBox = new TextBox
        {
            Text = identity,
            ID = "textbox"
        };
        phDependentInformation.Controls.Add(textBox);
    }
}

<html>
<head runat="server">
<title>Test user control binding</title>
</head>
<body>
<form id="form1" runat="server">
<div>
        <uc1:userInfo runat="server" ID="SpouseBasicInfo" Visible="false" />
        <asp:Repeater runat="server" ID="RptDependents"                   OnItemCreated="RptDependents_ItemCreated">
            <ItemTemplate>
                <uc1:userInfo runat="server" ID="DependentBasicInfo" />
            </ItemTemplate>
        </asp:Repeater>
        <asp:LinkButton runat="server" ID="submit"      OnClick="OnClickSubmit"><span>Submit</span></asp:LinkButton>
</div>
</form>
</body>

    public partial class _default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SpouseBasicInfo.identity = "spouse";
            SpouseBasicInfo.Bind();
            SpouseBasicInfo.Visible = true;

            List<String> list = new List<string>();
            list.Add("Dependent A");
            list.Add("Dependent B");

            RptDependents.DataSource = list;
            RptDependents.DataBind();
        }
    }

    protected void OnClickSubmit(object sender, EventArgs e)
    {
        if (!Page.IsValid)
        {
            return;
        }
        var textbox = SpouseBasicInfo.FindControl("textbox") as TextBox;
        string spouseName = textbox.Text;
    }

    protected void RptDependents_ItemCreated(object sender, RepeaterItemEventArgs e)
    {
        switch (e.Item.ItemType)
        {
            case ListItemType.Item:
            case ListItemType.AlternatingItem:
                {
                    var dependentInfo = e.Item.DataItem as String;
                    var dependentBasicInfo = e.Item.FindControl("DependentBasicInfo") as userInfo;
                    if (dependentBasicInfo == null) return;
                    dependentBasicInfo.applicantTitle = "Dependent " + (e.Item.ItemIndex + 1);
                    dependentBasicInfo.identity = dependentInfo;
                    dependentBasicInfo.Bind();
                }
                break;
        }
    }
}

</html>

1 个答案:

答案 0 :(得分:0)

您能否发布您的代码,以便我们查看它?此外,您说回发后运行时控件变为空。是否在呈现页面后控件集的text属性?如果是这样,您可以在页面上使用隐藏的asp标签来保存text属性。当页面被回发时,隐藏标签仍然保持为所述控件的文本属性保持的最后一个值。

&#34;用户控制(spouseBasicInfo)。为什么?用户控件也创建一个运行时控件(假设一个TextBox)。我发现在单击提交后无法检索运行时控件的Text属性,因为在PostBack之后控件变为null。如何轻松检索财产?任何人都可以帮助我吗?感谢&#34;

我很久没有和asp.net合作了,所以我不认为我能回答你提出的所有问题。但是,如果您在运行时创建一个文本框并使用占位符将对象传递到屏幕然后是,则回发后文本框控件将为null。这是因为它不是像运行时创建的文本框或标签那样的asp.control。您可以通过在屏幕上创建隐藏的标签来检索以前在文本框中保存的值。将您的值放在隐藏标签和运行时创建的文本框中。回发后,隐藏标签仍将保留回发之前的值,您可以检索,因为您通常会检索控件中保存的值。我希望这有助于您更接近解决问题。对不起,我无法提供更多帮助。祝你好运。