MESCIUS SPREAD for ASP.NET 10.0J
コマンドボタンの追加と削除

コマンドバーには、独自のコマンドボタンを追加するほか、組み込みのコマンドボタンを削除することができます。いずれも、Render メソッドをオーバーライドして、コマンドボタンが配置されたHTMLテーブルをカスタマイズします。

ボタンの追加

次のサンプルコードは、コマンドバーにボタン(My Button)を追加します。

protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
    //Control updateBtn = FpSpread1.FindControl("Update"); //SP3以前の実装方法
    Control updateBtn = FpSpread1.FindControl(string.Format("{0}_{1}", FpSpread1.ClientID, "Update")); //SP4以降の実装方法
    if ((updateBtn != null))
    {
        TableCell tc = (TableCell)updateBtn.Parent;
        TableRow tr = (TableRow)tc.Parent;
        TableCell tc1 = new TableCell();
        tr.Cells.Add(tc1);

        Button btn = new Button();
        btn.CausesValidation = false;
        btn.Text = "My Button";
        btn.Attributes.Add("onclick", "javascript:" + this.Page.GetPostBackEventReference(FpSpread1, "my command") + "; return false;");
        tc1.Controls.Add(btn);
    }
    base.Render(writer);
}
Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
    'Dim updateBtn As Control = FpSpread1.FindControl("Update") 'SP3以前の実装方法
    Dim updateBtn As Control = FpSpread1.FindControl(string.Format("{0}_{1}", FpSpread1.ClientID, "Update")) 'SP4以降の実装方法
    If Not updateBtn Is Nothing Then
      
        Dim tc As TableCell = updateBtn.Parent
        Dim tr As TableRow = tc.Parent
        Dim tc1 As New TableCell()
        tr.Cells.Add(tc1)
            
        Dim btn As New Button()
        btn.CausesValidation = False
        btn.Text = "My Button"
        btn.Attributes.Add("onclick", "javascript:" + Me.Page.GetPostBackEventReference(FpSpread1, "my command") + "; return false;")
        tc1.Controls.Add(btn)
    End If
      
    MyBase.Render(writer)
End Sub

ボタンコマンドを処理するには、ButtonCommand イベントを実装します。ここでは、先頭列のフィルタリングをクリアします。

private void FpSpread1ButtonCommand(object sender, FarPoint.Web.Spread.SpreadCommandEventArgs e)
{
    FpSpread1.ActiveSheetView.AutoFilterReset(0);
}
Private Sub FpSpread1_ButtonCommand(ByVal sender As Object, ByVal e As FarPoint.Web.Spread.SpreadCommandEventArgs) HandlesFpSpread1.ButtonCommand
    FpSpread1.ActiveSheetView.AutoFilterReset(2)
End Sub

ボタンの削除

次のサンプルコードは、コマンドバーからコマンドボタンを削除します。

コマンドボタンを非表示にするには、CreateButton イベントを使用します。詳細については「コマンドボタンのカスタマイズ」を参照してください。

protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
    //SP3以前の実装方法
    //Control updateBtn = FpSpread1.FindControl("Update");
    //Control cancelBtn = FpSpread1.FindControl("Cancel");
    //Control copyBtn = FpSpread1.FindControl("Copy");
    //Control cutBtn = FpSpread1.FindControl("Clear");
    //Control pasteBtn = FpSpread1.FindControl("Paste");
    //SP4以降の実装方法
    Control updateBtn = FpSpread1.FindControl(string.Format("{0}_{1}", FpSpread1.ClientID, "Update"));
    Control cancelBtn = FpSpread1.FindControl(string.Format("{0}_{1}", FpSpread1.ClientID, "Cancel"));
    Control copyBtn = FpSpread1.FindControl(string.Format("{0}_{1}", FpSpread1.ClientID, "Copy"));
    Control cutBtn = FpSpread1.FindControl(string.Format("{0}_{1}", FpSpread1.ClientID, "Cut"));
    Control pasteBtn = FpSpread1.FindControl(string.Format("{0}_{1}", FpSpread1.ClientID, "Paste"));
    if (updateBtn != null)
    {
        TableCell tc = (TableCell)updateBtn.Parent;
        TableRow tr = (TableRow)tc.Parent;
        tr.Cells.Remove(tc);
        tc = (TableCell)cancelBtn.Parent;
        tr.Cells.Remove(tc);
        tc = (TableCell)copyBtn.Parent;
        tr.Cells.Remove(tc);
        tc = (TableCell)cutBtn.Parent;
        tr.Cells.Remove(tc);
        tc = (TableCell)pasteBtn.Parent;
        tr.Cells.Remove(tc);
    }
    base.Render(writer);
}
        
Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
    'SP3以前の実装方法
    'Dim updateBtn As Control = FpSpread1.FindControl("Update")
    'Dim cancelBtn As Control = FpSpread1.FindControl("Cancel")
    'Dim copyBtn As Control = FpSpread1.FindControl("Copy")
    'Dim cutBtn As Control = FpSpread1.FindControl("Clear")
    'Dim pasteBtn As Control = FpSpread1.FindControl("Paste")
    'SP4以降の実装方法
    Dim updateBtn As Control = FpSpread1.FindControl(string.Format("{0}_{1}", FpSpread1.ClientID, "Update"))
    Dim cancelBtn As Control = FpSpread1.FindControl(string.Format("{0}_{1}", FpSpread1.ClientID, "Cancel"))
    Dim copyBtn As Control = FpSpread1.FindControl(string.Format("{0}_{1}", FpSpread1.ClientID, "Copy"))
    Dim cutBtn As Control = FpSpread1.FindControl(string.Format("{0}_{1}", FpSpread1.ClientID, "Cut"))
    Dim pasteBtn As Control = FpSpread1.FindControl(string.Format("{0}_{1}", FpSpread1.ClientID, "Paste"))
    If Not updateBtn Is Nothing Then
        Dim tc As TableCell = updateBtn.Parent
        Dim tr As TableRow = tc.Parent
        tr.Cells.Remove(tc)
        tc = cancelBtn.Parent
        tr.Cells.Remove(tc)
        tc = copyBtn.Parent
        tr.Cells.Remove(tc)
        tc = cutBtn.Parent
        tr.Cells.Remove(tc)
        tc = pasteBtn.Parent
        tr.Cells.Remove(tc)
    End If

    MyBase.Render(writer)
End Sub
関連トピック

 

 


© MESCIUS inc. All rights reserved.