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
Please help me with below issue.
 

I have submit form in which there are 3 options- 1. Text,2.Video,3.Image
if user select anyone option from above then respective div will open.
 
my javascript function is-
function fn_radio()
{
 
//var form1="ContentPlaceHolder1";
var form1="ctl00_ContentPlaceHolder1";
//alert("hi");

if(document.getElementById(form1+'_rdo_text').checked)
{
//alert("text");
    document.getElementById("hidden_radio").value= "text";
    document.getElementById("div_text").style.display="block";
    document.getElementById("div_video").style.display="none";
    document.getElementById("div_image").style.display="none";
    document.getElementById("plus1").style.display="none";
}
if(document.getElementById(form1+'_rdo_video').checked)
{
//alert("video");
    document.getElementById("hidden_radio").value= "video";
    document.getElementById("div_video").style.display="block";
    document.getElementById("div_text").style.display="none";
    document.getElementById("div_image").style.display="none";
    document.getElementById("plus1").style.display="none";
}
if(document.getElementById(form1+'_rdo_image').checked)
{
//alert("image");
    document.getElementById("hidden_radio").value= "images";
    document.getElementById("div_text").style.display="none";
    document.getElementById("div_video").style.display="none";
    document.getElementById("div_image").style.display="block";
    document.getElementById("plus1").style.display="block";
}
 
 return true;   // return false to stop postback
}
 
Now the problem is- i am checking image size in code behind file. n if it returns false then i want to call above function.
 
what to do?
Pls help me out...
Posted 22-Apr-13 3:21am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Add this line to your code from where you want to call js function:-
 
ScriptManager.RegisterStartupScript(this, GetType(), "radioEle", "fn_radio();", true)
  Permalink  
v2
Comments
abforce at 22-Apr-13 8:34am
   
Note1 :It's better to use ClientScript , isn't ? Note 2: 'Me' is in VB not C#
TrushnaK at 22-Apr-13 8:44am
   
I used that because:-
 
You've pretty much identified the primary difference. The ScriptManager is meant to be used with async postbacks, which is why it works with the UpdatePanel. The ClientScript class is for synchronous postbacks. So, if you're going to be posting back from an UpdatePanel, be sure to use the ScriptManager instead of ClientScript.
Sweetynewb at 22-Apr-13 8:51am
   
yes but where should i write this? bcoz after return statement it exits codebehind funcion
TrushnaK at 22-Apr-13 8:55am
   
Before exit of your function. remember it calls after code behind function.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Hi Sweetynewb , good time...
 
you can try this strategy:
 
*** In Server side ( Code-Behind file / .cs file )
 
protected void BtnSubmit_clicked (object sender , eventargs e)
{
// check your image size here
if ( size is okay)
// do some thing
else
{
string str = "fn_radio();";
ClientScript.RegisterStartupScript(this.GetType , "myScript" , str , true);
}
}
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

In your code behind include the following code on condition:
System.Text.StringBuilder JSText = new System.Text.StringBuilder();
JSText.Append(@"<script language="'javascript'">");
JSText.Append(@"fn_radio();");
JSText.Append(@"</script>");
 
if (!ClientScript.IsStartupScriptRegistered("MyScript"))
{
ClientScript.RegisterStartupScript(this.GetType(), "MyScript", JSText .ToString());
}
 
If you are using AsyncPostback, you will have to use ScriptManager.RegisterStartupScript
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Instead of returning false from code behind,call this function like...
  Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "checkRadio", "fn_radio();");
  Permalink  
v3

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

  Print Answers RSS
0 OriginalGriff 375
1 Sergey Alexandrovich Kryukov 329
2 CPallini 260
3 DamithSL 214
4 Maciej Los 197
0 OriginalGriff 5,455
1 DamithSL 4,457
2 Maciej Los 3,885
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,115


Advertise | Privacy | Mobile
Web01 | 2.8.141216.1 | Last Updated 22 Apr 2013
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