iT邦幫忙

0

ASP.NET C# 不同Button呼叫同個Function問題

大大好 我有兩個ImageButton1,ImageButton2,一個creatPDF的Function
想問有辦法做到
點ImageButton1呼叫creatPDF Function時不跳"另存新檔"的訊息(mark掉的地方)
點ImageButton2呼叫creatPDF Function時跳出"另存新檔"的訊息(mark掉的地方)
請問有辦法做到嗎??

public void creatPDF()
    {
            .
            .
            .
       /* Response.Clear();
        Response.ContentType = "application/pdf";
        Response.ContentEncoding = System.Text.Encoding.UTF8;
        Response.AddHeader("content-disposition", "attachment; filename=" + fileName);
        Response.Cache.SetCacheability(HttpCacheability.NoCache);*/
            .
            .
            .
        /*Response.WriteFile(filePath);
        Response.Write(doc1);
        Response.End();
        Response.Close();*/
        PdfWriter.Close();
    }
2
Homura
iT邦高手 3 級 ‧ 2018-06-05 11:46:58
最佳解答

creatPDF()這個函數加入一個變數來控制就行
然後在裡面判斷變數要做那些事情

public void creatPDF(bool msg)
{
    if(msg)
    {
        Response.Clear();
        Response.ContentType = "application/pdf";
        Response.ContentEncoding = System.Text.Encoding.UTF8;
        Response.AddHeader("content-disposition", "attachment; filename=" + fileName);
        Response.Cache.SetCacheability(HttpCacheability.NoCache);*/
    }
    else
    {
        Response.WriteFile(filePath);
        Response.Write(doc1);
        Response.End();
        Response.Close();
    }
          
    PdfWriter.Close();
}

按鈕地方這樣寫

protected void ImageButton1_Click(object sender, EventArgs e)
{
    creatPDF(true);
}
protected void ImageButton2_Click(object sender, EventArgs e)
{
    creatPDF(false);
}

感謝大大指點

1

最簡單的方式,加一個flag。

感謝大大指點

1
allenlwh
iT邦研究生 4 級 ‧ 2018-06-05 11:53:26

我之前有做個類似的功能

button.Click += HandleClick;

private void HandleClick(object sender, EventArgs e)
{
    //執行功能時,將button.Text帶進去Function
    CopyToFile(btn.Text); 
}

private void CopyToFile(string btnName)
{
    //Do something...
    if (btnName=="funtion1")
    {
    
    }
    
    if (btnName=="funtion2")
    {
    
    }
}
看更多先前的回應...收起先前的回應...

感謝大大指點

Homura iT邦高手 3 級 ‧ 2018-06-05 15:25:04 檢舉

因為btnName只會有一個,建議用switch比較好

allenlwh iT邦研究生 4 級 ‧ 2018-06-05 15:41:26 檢舉

感謝homura的說明,補充如下:

switch (btnName)
{
    case "品管":
        foldername = DateTime.Now.ToString("yyyyMMdd");
        targetDirectory = @"w:\◎品管課\";
        break;
    case "業務":
        foldername = DateTime.Now.ToString("yyyyMMdd");
        targetDirectory = @"w:\◎業務部\";
        break;
    case "研發":
        foldername = DateTime.Now.ToString("yyyyMMdd");
        targetDirectory = @"w:\◎研發部\";
        break;
    case "個人":
        foldername = DateTime.Now.ToString("yyyyMMdd");
        targetDirectory = textBox1.Text.ToString() + @"\";
        break;
    default:
        targetDirectory = textBox1.Text.ToString() + @"\";
        break;
}
Homura iT邦高手 3 級 ‧ 2018-06-05 16:41:44 檢舉

/images/emoticon/emoticon12.gif

受益良多 感謝大大們

我要發表回答

立即登入回答