【问题标题】:Attempting to set Default Value of TextBox1 within FormView尝试在 FormView 中设置 TextBox1 的默认值
【发布时间】:2021-02-08 02:04:49
【问题描述】:

使用 C# 和 WebForms 的 ASP.Net

我只是想在页面加载时使用值已批准的值填充 FormView 中的 TextBox。 FormView 的默认模式是编辑,它将仅保留在编辑中。我需要一些有效的代码和语法。我已经尝试过,但我无法填充该字段。我包括 FormView 标记和背后的代码。我感谢任何可以提供帮助的人。我知道这可能很简单,但我遇到了各种各样的麻烦,我还没有找到任何可行的方法。

我遇到的大多数解决方案都是针对插入的。

FormView1 标记

<asp:FormView ID="FormView1" runat="server" DataKeyNames="id" DataSourceID="test_default" DefaultMode="Edit" AllowPaging="True" >
     <EditItemTemplate>
         <asp:TextBox ID="TextBox2" runat="server" BackColor="#FFFFCC" Height="35px" Width="200px"></asp:TextBox>
         <br />
         id:
         <asp:Label ID="idLabel1" runat="server" Text='<%# Eval("id") %>' />
         <br />
         status:

代码背后

 protected void Page_Load(object sender, EventArgs e)
 {
     if (FormView1.CurrentMode == FormViewMode.Edit)
     {
         TextBox TextBox2 = FormView1.FindControl("TextBox2") as TextBox;
         if (TextBox2 != null)
         {
             TextBox2.Text = "approved";
         }

【问题讨论】:

  • 你有formview的数据绑定事件吗?您应该将代码放在数据绑定事件中。

标签: c# asp.net .net webforms


【解决方案1】:

成功了!

 protected void Page_PreInit(object sender, EventArgs e)
    {
        if (submitted_to_email != null)
        {
            _ = Context.User.Identity.Name;
            if (User.Identity.IsAuthenticated)
                submitted_to_email.Text = User.Identity.Name;
        }

        if (TextBox1 != null)
        {
            
                TextBox1.Text = "approved";
        }


    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-08
    • 2011-03-18
    • 1970-01-01
    • 1970-01-01
    • 2014-02-16
    • 1970-01-01
    • 2013-06-17
    相关资源
    最近更新 更多