calling javascript at server side

Posted by Shoyebaziz123 under ASP.NET on 10/5/2009 | Views : 4925 | Status : [Member] | Replies : 4
Hi Friends
I have a Textbox and a button
when i am clicking the button it should be shows javascript alert messege if textbox is empty otherwise shows javascript msg "Hello"

i have return this


Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
If TextBox1.Text <> "" Then
Button1.Attributes.Add("onclick", "javascript:return " & "alert('Hello')")
Else
Button1.Attributes.Add("onclick", "javascript:return " & "alert('Select Financial Year First')")
End If

End Sub


it's works fine but if TextBox is Blank.. first time clicking button nothing happens , second time show messege 'Select Financial Year First' then if delete the text
and clicking button first it shows 'Select Financial Year First' then after clicking it shows 'Hello'
what is going wrong?
Thanks in advance




Responses

Posted by: Kalyaninalluri on: 10/5/2009 [Member] Bronze

Up
0
Down
function pr()
{
document.getElementById("divBar").innerHTML += ". ";
window.setTimeout("pr()",1000);
}

...

<asp:button runat=server id=myButton text="Press">
....



on serverside:

private void Page_Load(object sender, System.EventArgs e)
{
myButton.Attributes.Add("onclick","pr()");
}



Shoyebaziz123, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Raghavendrank on: 10/5/2009 [Member] Starter

Up
0
Down
java script :

<script language="javascript" type="text/javascript">
function validate()
{
if (document.getElementById("<%=txtName.ClientID%>").value=="")
{
alert("Name Feild can not be blank");
document.getElementById("<%=txtName.ClientID%>").focus();
return false;
}
}
</script>

-----------------------------------------------------
Source Code :

<asp:TextBox ID="txtName" runat="server" />
<asp:Button ID="btnSubmit" OnClientClick=" return validate()" runat="server" Text="Submit" OnClick="btnSubmit_Click"/>

Code Behind :

protected void Page_Load(object sender, EventArgs e)
{
btnSubmit.Attributes.Add("onclick", "return validate()");

}

protected void btnSubmit_Click(object sender, EventArgs e)
{
WebMsgBox.Show(" In button click");
}



Shoyebaziz123, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Virendradugar on: 10/6/2009 [Member] [MVP] Silver

Up
0
Down
Hi,

I will explain you that why it is not working on the first time. Reason is when you click the button for the first time, that time Javascript is not assigned to the button. But once your code is executed of button click, now it adds attribute to button depending upon the condition. So second time when you click, it shows the java script.

Simple solution to this is :
1. Create a JavaScript function which check for the null value.
2. Bind that function in Page_load to the button.

<script language="javascript" type="text/javascript"> 

function ValidateFinancialYear()
{
if (document.getElementById("<%=TextBox1.ClientID%>").value=="")
{
alert("Select Financial Year");
document.getElementById("<%=TextBox1.ClientID%>").focus();
return false;
}
else
{
alert('Hello');
return true;
}
}
</script>


Now bind this function in page_load to the button.

Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Button1.Attributes.Add("onclick", "return ValidateFinancialYear();")
End Sub


You can also use requirefieldValidator to achieve the same. There is no need to write any javascript function.

<asp:RequiredFieldValidator ID="req1" runat="server" ErrorMessage="Select Financial Year" Display="Static" ControlToValidate="TextBox1">      

</asp:RequiredFieldValidator>


For more information about validation controls check this http://www.devhood.com/Tutorials/tutorial_details.aspx?tutorial_id=46 .

Hope this helps..

Thanks,
Virendra Dugar

Shoyebaziz123, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Virendradugar on: 10/6/2009 [Member] [MVP] Silver

Up
0
Down
I received your response in my email which is
"
Thanks For help,
Your Coding works well....but in the else part of javascript i want to call Server method Insert_Property()

Protected Sub btnProperty_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Insert_Property()

End Sub
but it not works.
"

In the JavaScript function ValidateFinancialYear(), in else part remove alert('hello'). But return true must be there.
So in this case, it will do a postback and will execute your server side button click event code.

If you still face any problem, Kindly post your code.

Thanks,
Virendra dugar

Shoyebaziz123, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response