Click here to Skip to main content
15,909,091 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello sir,
I created web page in that i used javascript in .aspx file.I need followings to be done.:

I am having **"save"** button,but in the source code i used java script for **save** button,where i declared a function called **OnClientClick="javascript : validateTextTest()"** and in the head of source code i called this function validateTextTest().

Below is the save button in source code:

<asp:Button ID="Save" runat="server" onclick="Save_Click" Text="Save" OnClientClick="javascript : validateTextTest()"
Width="63px" />

Now i need is can i call a function **validateTextTest**() in save button of **.cs file**.Because am having two to three textboxs,if i leave one texbox out of three textbox it should not insert into DB.

So please tel me how to call the function in .cs file .Please to help me.
Thanks,
Pradeep
Posted
Comments
Chandrashekar SK 11-Jun-12 7:39am    
You can use built in validation tools provided by asp.net. This way you can avoid validateTextTest() and rest works fine.

Write a server side event for the button_click, in this event call javascript as

JavaScript
string script = "<script language="'javascript'">validateTextTest();</script>";
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), Guid.NewGuid().ToString(), script, false);


Thanks
SP
 
Share this answer
 
Hi,

If you need a more complicated post back behaviour you can include a hidden which contains a value which includes your desired post back behaviour.

XML
<asp:hidden id="hdnAction" runat="Server" xmlns:asp="#unknown" />


Update your javascript method to assign an action and submit the form:

JavaScript
function validateTextTest() {

   // validation code

   document.getElementById('hdnAction').value = 'MyAction';
   document.forms[0].submit();

}


Then in your page load:

C#
protected void Page_Load(Object sender, EventArgs e)
{

  if(IsPostBack)
  {
    switch(hdnAction.Value)
    {
      case "MyAction":
        //do action code
        break;
    }
  }

}


You then remove the server event binding from your button:

XML
<asp:button id="Save" runat="server" text="Save" onclientclick="javascript : validateTextTest()" xmlns:asp="#unknown">
Width="63px" /></asp:button>
 
Share this answer
 

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