473,395 Members | 1,975 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

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
5 5390
scripto
143 100+
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
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
4,871 Expert 4TB
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
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
4,871 Expert 4TB
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

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

Similar topics

5
by: =?Utf-8?B?YmVuamk=?= | last post by:
Hi, Working with VB.NET 2005. I've got a mailmessage object and I set it up so that when the recipient replies, the reply address is a string of two different email addresses together. I used a...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.