Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET Javascript
Hi All,
 
I created two Controls. One is Button and Another One is TextBox Dynamically in .ascx. And i Register that in .aspx. When I run The Page I get Two Controls dynamically in runtime. Now I want to do Client Side validate the TextBox using JavaAscritp wheather it is Empty or not. Of Course I have to Inject JavaScript code in C#.
 
How Can I Validate without reloding the page.
 
Any Suggestion is highly appreciated.
 
Thanks
Shafik
Posted 21-Nov-12 2:54am
Shafik487
Comments
ryanb31 at 21-Nov-12 9:17am
   
Since you know the IDs of the dynamically created controls you could always write those to hidden fields and then access them in JavaScript. Or, if they are the only controls on the page you could just use jQuery selectors to get all textboxes, for example.
Shafik at 21-Nov-12 9:35am
   
Thanks Rayan. I am using Below Code in Button Click Event. But its not giving any result.
 
string script = "<script type=\"text/javascript\">function validate(){var name = document.getElementById(\"txtName\").value;if(name==\"\"){alert('Please Enter Your Name');retunr false;}}</script>";
// Gets the executing web page
Page page = HttpContext.Current.CurrentHandler as Page;
// Checks if the handler is a Page and that the script isn't allready on the Page
if (page != null && !page.ClientScript.IsClientScriptBlockRegistered("validate"))
{
page.ClientScript.RegisterClientScriptBlock(typeof(Sample), "validate", script);

 
}
 
Can u plz tell me what is the issue?
 
Thanks
Shafik
ryanb31 at 21-Nov-12 9:38am
   
Have you stepped through it? What happens when it runs?
Shafik at 21-Nov-12 9:42am
   
Its executing the whole code in debug mode but When I Click the Button, It's not showing the Alert Message
ryanb31 at 21-Nov-12 9:44am
   
Then the ID is likely not txtName. Step through the code and see what is happening. Also, view source in IE to see what the actual ID is in the html. Even though you assigned it an ID since it is on a user control the ID that is actually in the HTML will be different.
Shafik at 21-Nov-12 9:57am
   
Thanks Ryan Id is changed and Now I replacded that. When I debug Now I am getting Below JavaScript: <script type="text/javascript">function validate(){var name = document.getElementById("ucSample_txtName").value;if(name==""){alert('Please Enter Your Name');retunr false;}}</script>
 
Its not showing the alert message. Am I calling the JavaScript Function Right way?
 
Thanks
Shafik
ryanb31 at 21-Nov-12 10:04am
   
I'm not sure. Where do you call validate()?
Shafik at 21-Nov-12 10:06am
   
I am getting this output in the quotation "//]]> " When i click the Button. Can u tell me why. Looks My javascript is okay
ryanb31 at 21-Nov-12 10:07am
   
Well, you have spelled return wrong. You have retunr. The easiest thing to do though is to put a breakpoint and just step through it.
Shafik at 21-Nov-12 10:15am
   
Thanks Ryan That is a mistake Now My JavaScript: <script type="text/javascript">function validate(){var name = document.getElementById("ucSample_txtName").value;if(name==""){alert('Please Enter Your Name');return false;}}</script>
But its not Showing the Alert Message.
 
My rest code is Below:
 
Page page = HttpContext.Current.CurrentHandler as Page;
if (page != null && !page.ClientScript.IsClientScriptBlockRegistered("validate"))
{
page.ClientScript.RegisterClientScriptBlock(typeof(Sample), "validate", script);

}
 
Here Sample is my aspx class Name.
ryanb31 at 21-Nov-12 10:18am
   
OK, but when are you actually calling validate()? It doesn't get called automatically.
Shafik at 21-Nov-12 10:38am
   
Thanks Ryan I really don't know where i supposed to call. I just written that code and within double quotation i written validate(page.ClientScript.RegisterClientScriptBlock(typeof(Sample), "validate", script)
ryanb31 at 21-Nov-12 10:40am
   
You can use the OnClientClick event of an ASP.Net button. Set OnClientClick = "return validate();" so that if the return value is passed back the Click postback will not happen.
Shafik at 21-Nov-12 10:45am
   
Thanks Ryan to Reply Again. I used that from the begining. Code:
 
Panel p = (Panel)FindControl("pnlID");
Table tbl = (Table)FindControl("tblID");
btn = (Button)FindControl("btnShow");
btn.OnClientClick = "javascript:retrun validate();";
btn.Click += new EventHandler(btn_Click);
ryanb31 at 21-Nov-12 10:46am
   
return is misspelled again. But what happens when you debug it?
Shafik at 21-Nov-12 10:53am
   
Ryan Thanks for your patience. Now the Exception is: Microsoft JScript runtime error: Object expected
ryanb31 at 21-Nov-12 10:53am
   
Can you please debug it and step through the code?
Shafik at 21-Nov-12 10:59am
   
Yes.In view Source I got: <input type="submit" name="ucSample$btnShow" value="Show" önclick="javascript:return validate();" id="ucSample_btnShow" Its coming onClick but i mentioned onClientClick. I don't know how can i Call onClientClick Event in my C# Code
ryanb31 at 21-Nov-12 11:04am
   
Use IE's developer tools, put a breakpoint in the code and then see what happens.
Shafik at 21-Nov-12 11:06am
   
Okay Thanks Ryan
Shafik at 21-Nov-12 11:39am
   
I think Issue is here: <input type="submit" name="ucSample$btnShow" value="Show" önclick="javascript:return validate();" id="ucSample_btnShow" />
 
I written: btn.OnClientClick = "javascript:return validate();";
 
But its coming OnClick instead of OnClientClick

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
Comments
Shafik at 21-Nov-12 12:09pm
   
Thanks. You used static controls rather I used dynamic controls. code:
 
Panel p = (Panel)FindControl("pnlID");
Table tbl = (Table)FindControl("tblID");
btn = (Button)FindControl("btnShow");
btn.OnClientClick = "javascript:return validate();";
 
btn.Click += new EventHandler(btn_Click);
When I run the Program, In view source I see its coming OnClick not coming OnClientClick.How Can I get OnClientClick Evvent in view source and How I call from my C# Code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 545
1 CPallini 490
2 George Jonsson 297
3 OriginalGriff 250
4 Suvabrata Roy 209
0 OriginalGriff 4,873
1 CPallini 3,900
2 Sergey Alexandrovich Kryukov 3,474
3 George Jonsson 2,646
4 Gihan Liyanage 2,236


Advertise | Privacy | Mobile
Web04 | 2.8.140905.1 | Last Updated 21 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100