none
請教 VB.NET 轉成 C# 語法.. RRS feed

  • 問題

  •  

    請教各位...

     

    這是在網路上看到的一段語法用VB.net寫的...在轉換成 C#時會有錯誤發生..

    原VB.NET

     

    Code Snippet

      oFormView = CType(sender, FormView)
            If Not oFormView.Visible Then Exit Sub

            Select Case oFormView.CurrentMode
                Case FormViewMode.Edit '編輯模式
                    '隱藏新增鈕
                    oLinkButton = oFormView.FindControl("InsertButton")
                    oLinkButton.Visible = False
                    '顯示更新鈕
                    oLinkButton = oFormView.FindControl("UpdateButton")
                    oLinkButton.Visible = True
                    '顯示 EmployeeID 的 TextBox
                    oTextBox = oFormView.FindControl("txtEmployeeID")
                    oTextBox.Visible = False
                Case FormViewMode.Insert
                    '顯示新增鈕
                    oLinkButton = oFormView.FindControl("InsertButton")
                    oLinkButton.Visible = True
                    '隱藏更新鈕
                    oLinkButton = oFormView.FindControl("UpdateButton")
                    oLinkButton.Visible = False
                    '顯示 EmployeeID 的 TextBox
                    oTextBox = oFormView.FindControl("txtEmployeeID")
                    oTextBox.Visible = True
            End Select
        End Sub

     

     

    c#

    Code Snippet

                FormView oFormView = default(FormView);
                LinkButton oLinkButton = default(LinkButton);
                TextBox oTextBox = default(TextBox);

                oFormView = (FormView)sender;
                if (!oFormView.Visible) return;


                switch (oFormView.CurrentMode)
                {
                    case FormViewMode.Edit:
                        //編輯模式
                        //隱藏新增鈕
                      

                        oLinkButton = oFormView.FindControl("InsertButton");
                        oLinkButton.Visible = false;
                        //顯示更新鈕
                        oLinkButton = oFormView.FindControl("UpdateButton");
                        oLinkButton.Visible = true;
                        //顯示 EmployeeID 的 TextBox
                        oTextBox = oFormView.FindControl("txtEmployeeID");
                        oTextBox.Visible = false;
                        break;
                    case FormViewMode.Insert:
                        //顯示新增鈕
                        oLinkButton = oFormView.FindControl("InsertButton");
                        oLinkButton.Visible = true;
                        //隱藏更新鈕
                        oLinkButton = oFormView.FindControl("UpdateButton");
                        oLinkButton.Visible = false;
                        //顯示 EmployeeID 的 TextBox
                        oTextBox = oFormView.FindControl("txtEmployeeID");
                        oTextBox.Visible = true;
                        break;
                }

     

     

    錯誤都發生在..

    oLinkButton = oFormView.FindControl("InsertButton");

    錯誤訊息為:

    型別 'System.Web.UI.Control' 不能隱含轉換為 'System.Web.UI.WebControls.LinkButton'。已有明確轉換存在 (您是否漏掉了轉型?)

     

    請問要如何修改?

     

     

    2009年1月12日 上午 08:16

解答

  • oLinkButton = (LinkButton)(oFormView.FindControl("InsertButton"));

    或是

    LinkButton oLinkbutton = oFormView.FindControl("InsertButton");

     

    2009年1月12日 上午 08:24
  • Code Snippet

    LinkButton oLinkbutton = oFormView.FindControl("InsertButton") as LinkButton;

     

     

    2009年1月12日 上午 09:11
    版主

所有回覆

  • oLinkButton = (LinkButton)(oFormView.FindControl("InsertButton"));

    或是

    LinkButton oLinkbutton = oFormView.FindControl("InsertButton");

     

    2009年1月12日 上午 08:24
  • Code Snippet

    LinkButton oLinkbutton = oFormView.FindControl("InsertButton") as LinkButton;

     

     

    2009年1月12日 上午 09:11
    版主