how to call a c# function if user click on ok button of java script confirm box in .net

hi, i have one problem that is i'm adding the new user to database, if that user is already exist in the database one pop up should display with message like 'user already exists are u sure you want to update the role of the user' with 'yes' and 'no ' buttons on pop up,

if user click on 'yes' button then it should call edituser() method of c3.net other wise it should reset the selected controls.

how to do this using extenal .js file with javascript confirm box

 

-1
neeraja
2/4/2008 5:33:47 AM
ūüďĀ asp.net.client-side
ūüďÉ 24353 articles.
‚≠ź 2 followers.

ūüí¨ 7 Replies
ūüĎĀÔłŹ‚Äćūüó®ÔłŹ 4321 Views

Hi

When user click on "Yes" set a asp.net hidden field value to be "true" or "1", if user clicks on "No/Cancel" set the hidden field to be "false" or "0".

Based on the hidden field value call C# function in C#.

 


Shekar.Y(GGKTech)
http://ggktech.com
Dont forget to click ¬Ä¬úMark as Answer¬Ä¬Ě on the post that helped you.
-1
chandrashekar
2/4/2008 7:52:51 AM

can u please provide me one example how to do that one

-1
neeraja
2/4/2008 8:55:59 AM

This article show how to call C# function from javascript

http://forums.asp.net/t/999909.aspx

http://www.dotnet247.com/247reference/msgs/30/153188.aspx

 

 


Vikram
www.vikramlakhotia.com


Please mark the answer if it helped you
1
vik20000in
2/4/2008 9:01:39 AM
i'm not able to do can u please provide me how to declare hidden filed values and how to set those to confirm of javascript.

 i did like this:

void getSelectedUserDetails()

    {
        newConnection.Open();
        OracleCommand selectCommand4 = new OracleCommand("select roleid from userinrole where userid=&userid", newConnection);
        selectCommand4.Parameters.Add(new OracleParameter(":userid", OracleType.VarChar, 5));
        selectCommand4.Parameters[":userid"].Value = this.GridView1.SelectedValue.ToString();
        try
        {
            object obj11 = selectCommand4.ExecuteOracleScalar();
            int int1 = Convert.ToInt32(obj11.ToString());
            DropDownList1.SelectedValue = obj11.ToString();
            ListViewBind();
        }
        catch
        {
            DropDownList1.SelectedValue = "Select";
            ListBox1.Items.Clear();
            ClientScript.RegisterStartupScript(this.GetType(), "Select", "");
            edituser ();
          }
if user clicks on yes or ok button of confirm box edituser() method needs to be called how to do this.
        newConnection.Close();
    }

 

1
neeraja
2/4/2008 10:09:21 AM

Something like this should work for you:

private void Button1_Click(object sender, System.EventArgs e)
{
 bool isConfirmNeeded = false;
 string confirmMessage = string.Empty;

 // All server side execution goes here and set isConfirmNeeded to true,
 // and create the confirmMessage text, if user confirmation is needed.

 if ( isConfirmNeeded )
 {
  System.Text.StringBuilder javaScript = new System.Text.StringBuilder();

  javaScript.Append(" ");

  RegisterStartupScript(scriptKey, javaScript.ToString());
 }
}

private void Page_Load(object sender, System.EventArgs e)
{
 // Insure that the __doPostBack() JavaScript is added to the page...
 this.GetPostBackEventReference(this, string.Empty);

 if ( this.IsPostBack )
 {
  string eventTarget = (this.Request["__EVENTTARGET"] == null) ? string.Empty : this.Request["__EVENTTARGET"];
  string eventArgument = (this.Request["__EVENTARGUMENT"] == null) ? string.Empty : this.Request["__EVENTARGUMENT"];

  if ( eventTarget = "UserConfirmationPostBack" )
  {
   if ( eventArgument == "true" )
    // User said to go ahead and do it...
   else
    // User said NOT to do it...
  }
 }
}

NC...

-1
NC01
2/4/2008 2:13:47 PM

 thanks a lot this code resolved my problem.

1
neeraja
2/5/2008 9:08:06 AM

Well then, something like this should work for you:

private void Button1_Click(object sender, System.EventArgs e)
{
 bool isConfirmationNeeded = false;

 // All server side processing goes here. Then set the boolean
 // isConfirmationNeeded to true, if user confirmation is needed.

 if ( isConfirmationNeeded )
 {
  System.Text.StringBuilder javaScript = new System.Text.StringBuilder();

  javaScript.Append(" ");

  RegisterStartupScript(scriptKey, javaScript.ToString());
 }
}

private void Page_Load(object sender, System.EventArgs e)
{
 // Insure that the __doPostBack() JavaScript is added to the page...
 this.GetPostBackEventReference(this, string.Empty);

 if ( this.IsPostBack )
 {
  string eventTarget = (this.Request["__EVENTTARGET"] == null) ? string.Empty : this.Request["__EVENTTARGET"];
  string eventArgument = (this.Request["__EVENTARGUMENT"] == null) ? string.Empty : this.Request["__EVENTARGUMENT"];

  if ( eventTarget = "UserConfirmationPostBack" )
  {
   if ( eventArgument == "true" )
   {
    // User said to go ahead and do it...
    this.updateUser();
   }
   else
   {
    // User said NOT to do it...
   }
  }
 }
}

private void updateUser()
{
 // Database code to update the user goes here...
}

Note that RegisterStartupScript and RegisterClientScriptBlock have changed since version 1.1 and you will get a compiler warning with the above.
See http://msdn2.microsoft.com/en-us/library/system.web.ui.clientscriptmanager.aspx for more info.

NC...

-1
NC01
2/5/2008 5:30:22 PM