【问题标题】:textbox can't grab text property from findControl文本框无法从 findControl 中获取文本属性
【发布时间】:2012-11-25 12:50:15
【问题描述】:

我在 ASP 中使用 CreateUserWizardControl,并希望将注册表单中的一些数据写入我自己的表中。我想在创建用户的那一刻这样做。

当我调试时,这个问题似乎是在这一行中,我在之前的行中声明的文本框 t 没有抓取 UserName 文本框。它确实找到了一些东西,因为它不为空,但 t 的文本属性是一个空字符串:

t = (TextBox)(this.CreateUserWizard1.FindControl("UserName"));

在下一行抛出异常:“对象引用未设置为对象的实例。”

o.organisation_name = t.Text;

以下是我的完整代码隐藏文件:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

public partial class registreer : System.Web.UI.Page
{

protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
{
    try
    {
        BLLorganisation BLLo = new BLLorganisation();
        Organisation o = new Organisation();
        TextBox t = new TextBox();
        t = (TextBox)(this.CreateUserWizard1.FindControl("UserName"));
        o.organisation_name = t.Text;
        o.fk_user_id = (Guid)(Membership.GetUser(CreateUserWizard1.UserName).ProviderUserKey);
        BLLo.insertOneOrganisation(o);
    }
    catch (Exception ex)
    {
        feedback.InnerHtml = ex.Message;
        feedback.Style.Add("display", "block");
    }
}
}

这是我对标记的控制:

<asp:CreateUserWizard ID="CreateUserWizard1" runat="server" 
            oncreateduser="CreateUserWizard1_CreatedUser">
            <WizardSteps>
                <asp:CreateUserWizardStep ID="CreateUserWizardStep1" runat="server" 
                    Title="Registreer uw organisatie">
                    <ContentTemplate>
                        <table>
                            <tr>
                                <td align="center" colspan="2">
                                    <h1>Registreer uw organisatie</h1></td>
                            </tr>
                            <tr>
                                <td align="right">
                                    <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">Naam van de organisatie:</asp:Label>
                                </td>
                                <td>
                                    <asp:TextBox ID="UserName" runat="server"></asp:TextBox>
                                    <asp:RequiredFieldValidator ID="UserNameRequired" runat="server" 
                                        ControlToValidate="UserName" ErrorMessage="Vul de naam van uw organisatie in." 
                                        ToolTip="Vul de naam van uw organisatie in." ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>
                                </td>
                            </tr>

有人知道出了什么问题吗?如果您需要查看更多代码,请告诉我!

【问题讨论】:

  • 显然 CreateUserWizard1 中没有名为 UserName 的控件。你能分享文本框用户名的标记吗?
  • CreateUserWizard1.UserName 不是 UserName 文本框的值吗?
  • @HenkMollema,我尝试用o.organisation_name = (String)(Membership.GetUser(CreateUserWizard1.UserName).UserName); 替换o.organisation_name = t.Text;,现在我不再遇到异常了!这是领先一步,非常感谢你!现在我确实在我的 try-branch 的最后一行收到了 Sequence contains no elements 异常...
  • 只需CreateUserWizard1.UserName 就足够了。无需通过用户名获取用户的用户名。如果我错了,请纠正我。您能否用您的新代码更新您的问题,以便我们查看您的新错误?

标签: c# asp.net textbox


【解决方案1】:
TextBox t = (TextBox)  CreateUserWizard1.ContentTemplateContainer.FindControl("UserName");
if(t!=null)
   o.organisation_name = t.Text;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-22
    • 2023-03-09
    • 1970-01-01
    • 2017-10-05
    • 1970-01-01
    • 2015-10-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多