By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,068 Members | 825 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,068 IT Pros & Developers. It's quick & easy.

Trying to use confirm("myMessage") in ascx page

P: 3
Ok, I'm somewhat new to .NET C# language and am having a bit of trouble.

I'm trying to run the javascript confirm() method from my ascx page but I'm currently using C# as the main language of the page so it's not recognizing the confirm method.

On previous attempts, I tried to do this straight from the radiobutton control that I want it linked to, but I have another method that I need to run after it if the user did click ok on the confirm dialog. When I tried to run the confirm method and a C# method i wrote in a <script language=C# > block, it runs the confirm method but never gets the the other method. This is the syntax i used: onclick="if(confirm('messageNeededToDisplay'))othe rMethod()". I assume this is because of the difference in the languages used to run the methods.

So, is there a way to create a C# method that will do the same actions as the confirm() method (give a dialog box that will return true or false based on the user's decision)? I can list you all the references I am currently using if that will help decide whether I can do this with my current system setup and codebase.

Thanks for any and all help.
Dec 8 '06 #1
Share this Question
Share on Google+
5 Replies


scripto
100+
P: 143
By design, you cannot do message boxes on the server side - they must be called from the client side - vbscript or javascript. BUT, you can call the javascript from the server side. Take a look at how to use the RegisterClientScriptBlock() function.
Dec 8 '06 #2

P: 3
Ok, I do not think I will be able to use RegisterClientScriptBlock() because the radiobuttons that I have the if(confirm()) block linked to are dynamically generated per qualified timeoff request per user that the system runs through for the current user if it's a manager.

So the problem, with most workarounds that I've found, is the fact that I don't know how many radiobuttons there is going to be on the page when it's rendered for the user, so I can't attach a method to it on server-side,

Here is the script block I'm trying to use on my ascx page, and, following that, the code snappet that generates the code for the radiobuttons on the page.

`````````````````````````````````````````````````` `````````````````````

<script Language="C#" runat="server">
void Approve(object sender, EventArgs e)
{
RadioButton radbtn = (RadioButton) sender;
OnApproval(radbtn.ID);
}
</script>

`````````````````````````````````````````````````` `````````````````````

js = "OnClick=\"if(confirm('Are you sure you want to Aprrove/Decline this ATO Request?')) this.Approve;\"";

String radbtns = "&nbsp<input type='radio' id='y" + requestId +"' name='"+ requestId + "' value='yes' "+js+" >yes</input>&nbsp;"+ "<input type='radio' id='n"+ requestId + "' name='"+ requestId + "' value='no' "+js+" >no</input>&nbsp";

`````````````````````````````````````````````````` `````````````````````
The String Radbtns is passed, along with all the other data needed for all users the iteration goes through, to a label control that is created on the .ascx page and used in the ascx.cs page. And I can't register the script to this label as it would run and update all users info, which is not the desired effect.

I can provide the full pages if they will help.

Thanks for the help
Dec 8 '06 #3

kenobewan
Expert 2.5K+
P: 4,871
You are able to call the client-side script that you need from the server-side. I program in VB.NET, but it will be similar:

Expand|Select|Wrap|Line Numbers
  1.  radiobutton.attributes.add("onclick","return confirm()") 
That way you can call it in the server-side you need. You won't find a client-side solution I believe. Hope that this helps.
Dec 11 '06 #4

P: 3
Ok, I have almost totally redone my pages so that I can use the .Attributes.Add() function, but when I run my pages, it seems like the javascript isn't running on the clientside at all. No matter what I have the function do, nothing happens.

I've posted the new code at the bottom of the page so you guys can take a look at it. Tell me if you see anything that would cause my function not to work.


Thanks

ascx script block
Expand|Select|Wrap|Line Numbers
  1. void Do_Approve(object Source, EventArgs e)
  2. {
  3.     System.Web.UI.WebControls.RadioButton radbtn = 
  4.         (System.Web.UI.WebControls.RadioButton)Source;
  5.  
  6.     Approve(radbtn.ID);
  7. }
  8.  
ascx.cs radiobutton code block
Expand|Select|Wrap|Line Numbers
  1. System.Web.UI.WebControls.RadioButton radbtn = 
  2. new System.Web.UI.WebControls.RadioButton();
  3. radbtn.GroupName = requestId;
  4. radbtn.Text = optionText;
  5. radbtn.AutoPostBack = false;
  6. String tmp = "";
  7.  
  8. if(optionText == "yes")
  9. {
  10.     tmp = "y";
  11. }
  12. else
  13. {
  14.     tmp = "n";
  15. }
  16.  
  17. radbtn.ID = tmp+requestId;
  18. radbtn.Attributes.Add("Runat","Server");
  19. radbtn.Attributes.Add("OnClick","this.Do_Approve");
  20.  
  21. return radbtn;
  22.  
source code when view ascx page
Expand|Select|Wrap|Line Numbers
  1. <tr>
  2.     <td>Your NAME Here</td>
  3.     <td>12/11/2006</td>
  4.     <td>12/18/2006</td>
  5.     <td>Noon</td>
  6.     <td>
  7.     <span Runat="Server">
  8.     <input id="_ctl3_y98" type="radio" 
  9.     name="_ctl3:98" value="y98" onclick="this.Do_Approve" />
  10.     <label for="_ctl3_y98">yes</label>
  11.     </span>
  12.     <span Runat="Server">
  13.     <input id="_ctl3_n98" type="radio" 
  14.     name="_ctl3:98" value="n98" onclick="this.Do_Approve" />
  15.     <label for="_ctl3_n98">no</label>
  16.     </span>
  17.     </td>
  18.     <td>No</td>
  19. </tr>
  20.  
Dec 12 '06 #5

kenobewan
Expert 2.5K+
P: 4,871
OK - that you are not receiving an error message means that the JS is not being called. The first thing that I would do is remove the onClick in the button, so you don't have the client-side code being called before the server-side.

I would have the runat="server" in the form tag and use an asp:button rather than an html input one. That way the you can use the form event to trigger the button.add event.

I would then go through a debugging process - what we want to see is that your JS is being called and if you get an error it is a JS error. Once that happens, you have a JS and not a server-side code problem.

Tip - if this still isn't working then comment out as much C# code as possible until you just get the JS firing from the button.add event and build things up from there. Maybe an alert saying "Hello" to start.

Hope that this helps.
Dec 13 '06 #6

Post your reply

Sign in to post your reply or Sign up for a free account.