help needed calling C# method from javascript

G

Guest

I would like to be able to call methodTrue() when the user clicks yes and
methodFalse() when they click No. How do i accomplish that? here is my code:

private void methodTrue()
{
//method false details
}
private void methodFalse()
{
//method false details
}
public void CreateMessageProm2(System.Web.UI.Page aspxPage, string
strMessage, string strKeyPrompt)
{
try
{
StringBuilder sb = new StringBuilder();
sb.Append("<script language='javascript'>");
sb.Append("var results = window.confirm('" + strMessage + "');");
sb.Append("if (results == true)");
sb.Append("{ alert('The value is true');}"); //call methodTrue
sb.Append("else");
sb.Append("{ alert('the value is false');}"); //call methodFalse

sb.Append("</script>");

string strScript = sb.ToString();

if (!aspxPage.IsStartupScriptRegistered(strKeyPrompt))
{
aspxPage.RegisterStartupScript(strKeyPrompt, strScript);
}
}
catch(Exception exep)
{
string sMessage = exep.Message.ToString();
}
}
 
S

Shawn

You cannot achieve this without a postback. Assign a value, true or false,
to a hidden textbox and submit the form with your javascript. Then check
the textbox for a true or false value in your page_load and call the
appropriate method.

Shawn
 
G

Guest

There really are no buttons other than the ones that come with the
window.confirm script

Eliyahu Goldin said:
The user clicks buttons on his user machine. The c# methods belong to your
asp.net application that runs on your web server. You should make the
buttons to server controls and call methodTrue and methodFalse in the
serve-side onclick event handlers. To add client-side confirm functionality
you need to add client-side onclick event handlers:

YesButton.Attributes["onclick"] = String.Format ("return
window.confirm({0})", msgConfirmYes);
NoButton.Attributes["onclick"] = String.Format ("return
window.confirm({0})", msgConfirmNo);

You don't need to register any scripts.

Eliyahu

Mori said:
I would like to be able to call methodTrue() when the user clicks yes and
methodFalse() when they click No. How do i accomplish that? here is my code:

private void methodTrue()
{
//method false details
}
private void methodFalse()
{
//method false details
}
public void CreateMessageProm2(System.Web.UI.Page aspxPage, string
strMessage, string strKeyPrompt)
{
try
{
StringBuilder sb = new StringBuilder();
sb.Append("<script language='javascript'>");
sb.Append("var results = window.confirm('" + strMessage + "');");
sb.Append("if (results == true)");
sb.Append("{ alert('The value is true');}"); //call methodTrue
sb.Append("else");
sb.Append("{ alert('the value is false');}"); //call methodFalse

sb.Append("</script>");

string strScript = sb.ToString();

if (!aspxPage.IsStartupScriptRegistered(strKeyPrompt))
{
aspxPage.RegisterStartupScript(strKeyPrompt, strScript);
}
}
catch(Exception exep)
{
string sMessage = exep.Message.ToString();
}
}
 
E

Eliyahu Goldin

The user clicks buttons on his user machine. The c# methods belong to your
asp.net application that runs on your web server. You should make the
buttons to server controls and call methodTrue and methodFalse in the
serve-side onclick event handlers. To add client-side confirm functionality
you need to add client-side onclick event handlers:

YesButton.Attributes["onclick"] = String.Format ("return
window.confirm({0})", msgConfirmYes);
NoButton.Attributes["onclick"] = String.Format ("return
window.confirm({0})", msgConfirmNo);

You don't need to register any scripts.

Eliyahu
 
E

Eliyahu Goldin

Ok, then go for the hidden textbox as the other poster has suggested.

Eliyahu

Mori said:
There really are no buttons other than the ones that come with the
window.confirm script

Eliyahu Goldin said:
The user clicks buttons on his user machine. The c# methods belong to your
asp.net application that runs on your web server. You should make the
buttons to server controls and call methodTrue and methodFalse in the
serve-side onclick event handlers. To add client-side confirm functionality
you need to add client-side onclick event handlers:

YesButton.Attributes["onclick"] = String.Format ("return
window.confirm({0})", msgConfirmYes);
NoButton.Attributes["onclick"] = String.Format ("return
window.confirm({0})", msgConfirmNo);

You don't need to register any scripts.

Eliyahu

Mori said:
I would like to be able to call methodTrue() when the user clicks yes and
methodFalse() when they click No. How do i accomplish that? here is
my
code:
private void methodTrue()
{
//method false details
}
private void methodFalse()
{
//method false details
}
public void CreateMessageProm2(System.Web.UI.Page aspxPage, string
strMessage, string strKeyPrompt)
{
try
{
StringBuilder sb = new StringBuilder();
sb.Append("<script language='javascript'>");
sb.Append("var results = window.confirm('" + strMessage + "');");
sb.Append("if (results == true)");
sb.Append("{ alert('The value is true');}"); //call methodTrue
sb.Append("else");
sb.Append("{ alert('the value is false');}"); //call methodFalse

sb.Append("</script>");

string strScript = sb.ToString();

if (!aspxPage.IsStartupScriptRegistered(strKeyPrompt))
{
aspxPage.RegisterStartupScript(strKeyPrompt, strScript);
}
}
catch(Exception exep)
{
string sMessage = exep.Message.ToString();
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top