Click here to Skip to main content
15,889,838 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
Comments
ZurdoDev 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.
[no name] 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
ZurdoDev 21-Nov-12 9:38am    
Have you stepped through it? What happens when it runs?
[no name] 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
ZurdoDev 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.

1 solution

 
Share this answer
 
Comments
[no name] 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900