2013-10-01 111 views
0

我要檢查,如果被點擊按鈕或者未方法如何檢查方法中按鈕是否被點擊?

我試過,但它不工作

代碼:

protected override void LoadViewState(object savedState) 
{ 
    base.LoadViewState(savedState); 

    //Here If (btnAddExperience_Click() is clicked) 
    { 
    GenerateControls(); 
    } 

    GenerateControls1(); 
} 


private void GenerateControls() 
{ 
    foreach (string i in NoOfControls) 
    { 
     VisaUserControl ctrl = (VisaUserControl)Page.LoadControl("VisaUserControl.ascx"); 

     ctrl.ID = i; 
     this.rpt1.Controls.Add(ctrl); 
     rpt1.Controls.Add(new LiteralControl("<BR>")); 
    } 
} 

protected void btnAddVisa_Click(object sender, EventArgs e) 
{ 
    Button thisButton = (Button)sender; 
    List<string> temp = null; 
    var uc = (VisaUserControl)this.LoadControl(@"VisaUserControl.ascx"); 

    string id = Guid.NewGuid().ToString(); 
    uc.ID = id; 

    temp = NoOfControls; 
    temp.Add(id); 
    NoOfControls = temp; 
    rpt1.Controls.Add(uc); 
    rpt1.Controls.Add(new LiteralControl("<BR>")); 
} 

private void GenerateControls1() 
{ 
    foreach (string i in NoOfControls) 
    { 
     ExperienceUserControl ctrl = (ExperienceUserControl)Page.LoadControl("ExperienceUserControl.ascx"); 

     ctrl.ID = i; 
     this.rpt1.Controls.Add(ctrl); 
     rpt2.Controls.Add(new LiteralControl("<BR>")); 
    } 
} 

protected void btnAddExperience_Click(object sender, EventArgs e) 
{ 
    List<string> temp = null; 
    var uc = (ExperienceUserControl)this.LoadControl(@"ExperienceUserControl.ascx"); 

    string id = Guid.NewGuid().ToString(); 
    uc.ID = id; 

    temp = NoOfControls; 
    temp.Add(id); 
    NoOfControls = temp; 
    rpt2.Controls.Add(uc); 
    rpt2.Controls.Add(new LiteralControl("<BR>")); 
} 

任何想法?在此先感謝

回答

1

我相信你應該能夠檢查:

Request.Form.Get("__EVENTTARGET") 
Request.Form.Get("__EVENTARGUMENT") 

爲了做到這一點,其目標有控制的唯一ID,並且參數具有事件參數。我以前用過;然而,並非所有的東西都可以設定這些值此外,按鈕點擊事件不會觸發,直到LoadViewState之後,所以你不能放置任何東西來設置布爾值。

1

btnAddExperience_Click事件將在LoadViewState事件後執行(可能讀取this article會有好處)。

我會建議使用表單字段以指示該按鈕被點擊:

<script type="text/javascript"> 

    function setButtonClickedField() 
    { 
     document.getElementById("hidButtonClicked").value = "true"; 
    } 

    function resetButtonClickedField() 
    { 
     document.getElementById("hidButtonClicked").value = "false"; 
    } 

    // use this or a cross-browser attach event to attach the reset to the window/document onload event 
    var tempFunction; 

    if (window.onload) 
    { 
     tempFunction = window.onload; 
    } 

    window.onload = function() 
    { 
     if (tempFunction) 
     { 
      tempFunction(); 
     } 

     resetButtonClickedField(); 
    } 

</script> 

<input type="hidden" id="hidButtonClicked" name="hidButtonClicked" value="false" /> 

<asp:Button runat="server" ID="btnAddExperience" OnClick="btnAddExperience_Click" OnClientClick="setButtonClickedField" Text="Add Experience" /> 

然後在後面的代碼:

protected override void LoadViewState(object savedState) 
{ 
    base.LoadViewState(savedState); 

    bool btnAddExperienceClicked = bool.Parse(Request.Form["hidButtonClicked"]); 

    if (btnAddExperienceClicked == true) 
    { 
     GenerateControls(); 
    } 

    GenerateControls1(); 
} 
0

你可以在你的窗體/控件聲明一個布爾值來存儲按鈕的狀態,然後使用按鈕的MouseDown和MouseUp事件來設置你的布爾值。然後檢查LoadViewState方法中的布爾值。

public partial class Form1 : Form 
{ 
    bool buttonClicked = false; 

    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void button1_MouseDown(object sender, MouseEventArgs e) 
    { 
     buttonClicked = true; 
    } 

    private void button1_MouseUp(object sender, MouseEventArgs e) 
    { 
     buttonClicked = false; 
    } 

    private void LoadViewState() 
    { 
     if (buttonClicked) 
     { 
      //Do something 
     } 
    } 
} 
相關問題