我想检查方法
中是否点击了按钮我试过但它没有用
代码:
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>"));
}
有什么想法吗?提前致谢
答案 0 :(得分:1)
我相信你应该能够检查:
Request.Form.Get("__EVENTTARGET")
Request.Form.Get("__EVENTARGUMENT")
要做到这一点,目标具有控件的唯一ID,并且参数具有事件args。我以前用过;然而,并非一切都可以设定这些价值。此外,按钮单击事件在LoadViewState之后才会触发,因此您无法在其中放置一些内容来设置布尔值。
答案 1 :(得分: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();
}
答案 2 :(得分:0)
您可以在表单/控件中声明一个bool来存储按钮的状态,然后使用按钮的MouseDown和MouseUp事件来设置bool值。然后在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
}
}
}