Reputation: 728
Is there a way to make the following code works? Basically, when I click on btnOne, If a is true, i expect it to popup the confirm box. And If i press yes, then it will execute btnTwo method. Otherwise, it will not do anything. At the moment, it doesn't do the popup and i am not sure why. Can someone please point me in the right direction? or possibly let me know other way of achieving this.
Any information would be much appreciated.
For example:
public void btnTwo(object sender, EventArgs e)
{
//Do some processing
}
public void btnOne(object sender, EventArgs e)
{
if( a == true )
btnTwo.Attributes["onClick"] = "return confirm('test');"
btnTwo(sender, new EventArgs());
}
Upvotes: 0
Views: 1173
Reputation: 38
I think you are mixing to much client and server side? Could you check "a" (hidden field) on client side?
Or after check value in code behind add start up script with confirm and then on yes "click" second button in javascript.
Protected Sub btnSecond_Click(sender As Object, e As System.EventArgs) Handles btnSecond.Click
Me.lblInfo.Text = "SecondClick is done!"
End Sub
Protected Sub btnFirst_Click(sender As Object, e As System.EventArgs) Handles btnFirst.Click
a = 10
Dim action As String = "<script> if(confirm('sure ?')){ document.getElementById('" & btnSecond.ClientID & "').click()} </script>"
If (a > 5) Then
Page.ClientScript.RegisterStartupScript(Me.Page.GetType(), "startConfirm", action)
End If
End Sub
And markup:
<form id="form1" runat="server">
<div>
<asp:Button runat="server" ID="btnFirst" />
</br>
<asp:Button runat="server" ID="btnSecond" />
<asp:Label runat="server" ID="lblInfo" />
</div>
</form>
Upvotes: 1
Reputation: 46067
Make sure that the code is being reached, and use OnClientClick
instead:
protected void Page_Load(object sender, EventArgs e)
{
bool a = true;
if (a)
btnTwo.OnClientClick = "return confirm(\"Are you sure?\");";
}
Upvotes: 0