【问题标题】:FormView FindControl EditTemplate in C#C#中的FormView FindControl EditTemplate
【发布时间】:2018-07-07 15:46:32
【问题描述】:

我在 C# asp.net webforms 的代码后面有这个,它不会执行,从 VB.NET 文件转置。 VB Webform 执行得很好,从 Pre-Render 中找到处于编辑模式的控件。但是 C# 版本没有。

有人能指出问题所在吗?该代码应该在 EditMode 中找到 Telerik TextBox 的内容,并使用 TextBox 的文本更改标题。

VB.NET 代码

Private Sub FormView1_PreRender(sender As Object, e As EventArgs) Handles FormView1.PreRender
    If FormView1.CurrentMode = FormViewMode.Edit Then
        Dim ProductNameTextBox As Label = FormView1.FindControl("BannerLabel")
        Dim StudentName As RadTextBox = FormView1.FindControl("FirstNameTxtBx")
        Dim UpdateButton As Button = FormView1.FindControl("UpdateButton")
        ProductNameTextBox.Text = "Edit " + StudentName.Text + "'s Profile"
        UpdateButton.Text = "Update changes to " + StudentName.Text + "'s Profile"
    End If
End Sub

C# 代码

private void FormView1_PreRender(object sender, EventArgs e)
{
    if (FormView1.CurrentMode == FormViewMode.Edit)
    {
        Label ProductNameTextBox = FormView1.FindControl("BannerLabel") as Label;
        RadTextBox StudentName = FormView1.FindControl("FirstNameTxtBx") as RadTextBox;
        Button UpdateButton = FormView1.FindControl("UpdateButton") as Button;
        ProductNameTextBox.Text = "Edit " + StudentName.Text + "'s Profile";
        UpdateButton.Text = "Update changes to " + StudentName.Text + "'s Profile";
    }
}

【问题讨论】:

    标签: c# vb.net formview findcontrol vb.net-to-c#


    【解决方案1】:

    您似乎忘记了 C# 版本中的 subscribeFormView1.PreRender 事件。

    在您的 VB.NET 代码中,您有 Handles FormView1.PreRender,它会自动为您订阅事件。 Handles 关键字在 C# 中没有对应的关键字,因此您需要自己订阅该事件。

    在代码中的某处添加以下行,以便在 Web 表单加载或初始化时执行:

    FormView1.PreRender += FormView1_PreRender;
    

    如果之后仍然无法正常工作,您可能需要提供更多信息,因为我在方法主体中没有发现任何错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-08
      • 1970-01-01
      • 1970-01-01
      • 2023-03-26
      • 2013-09-22
      相关资源
      最近更新 更多