[英]How to check if button is clicked or not in method?
I want to check if button is clicked or not in method 我想检查方法中是否单击了按钮
I tried but it's not working 我试过了,但它不起作用
Code: 码:
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>"));
}
Any ideas? 有任何想法吗? Thanks in advance
提前致谢
I believe you should be able to check: 我相信你应该能够检查:
Request.Form.Get("__EVENTTARGET")
Request.Form.Get("__EVENTARGUMENT")
To do that, which the target has the unique ID of the control, and the argument has the event args. 要做到这一点,目标具有控件的唯一ID,并且参数具有事件args。 I've used that before;
我以前用过; however, not everything may set these values.
然而,并非一切都可以设定这些价值。 Also, button click event won't fire until after LoadViewState, so you can't put something in there to set a boolean value.
此外,按钮单击事件将在LoadViewState之后才会触发,因此您无法在其中放置一些内容来设置布尔值。
The btnAddExperience_Click
event will be executed well after the LoadViewState
event (perhaps reading this article would be beneficial). 在
LoadViewState
事件之后, btnAddExperience_Click
事件将很好地执行(也许阅读本文将是有益的)。
I would suggest using a form field to indicate that this button has been clicked: 我建议使用表单字段来表示已单击此按钮:
<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" />
Then in code behind: 然后在代码后面:
protected override void LoadViewState(object savedState)
{
base.LoadViewState(savedState);
bool btnAddExperienceClicked = bool.Parse(Request.Form["hidButtonClicked"]);
if (btnAddExperienceClicked == true)
{
GenerateControls();
}
GenerateControls1();
}
You could declare a bool in your form/control to store the state of the button then use the MouseDown and MouseUp events of the button to set your bool value. 你可以在表单/控件中声明一个bool来存储按钮的状态,然后使用按钮的MouseDown和MouseUp事件来设置你的bool值。 Then check the value of the bool in your LoadViewState method.
然后检查LoadViewState方法中bool的值。
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
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.