|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionI have been searching in the internet to find ways to call JavaScript validation code from a User Control, but failed to find any. Then one of my friends helped me out in doing the task. This article will help new learners and developers who come across the scenario which I had. In our user control there is a dropdownlist dynamically loaded with data. It happens that we require to set focus on the Submit button on the change handler of the dropdown and even want to put some validation into it. Once the user changes an option in the dropdownlist and press the Enter button, it should call the Submit button event handler function. This will become clear while I take you through the example. Key Scenario
Implementation of the User ControlWe can create a User Control using VS.NET 2003 by selecting a new item in the Solution Explorer project view. On creating the User Control we add the following code in the code behind. On the page load, we register the JavaScript function so as to call it from the code behind. 'Put user code to initialize the page here
'<summary>
'This function will check whether selected index is 0
'</summary>
Dim strScript1 As String = "< script language" & _
"=JavaScript > function CodeBehindButtonJs() {"
strScript1 += "if (document.forms[0]." & uCtrlId & _
"_ddlItem.selectedIndex == 0){alert('Validation:" & _
"Please Select Item');return false;};}"
strScript1 += "<"
strScript1 += "/"
strScript1 += "script >"
'<summary>
'This function will set focus on submit
'button on dropdownlist change event.
'</summary>
Dim strScript2 As String = "< script language=" & _
"JavaScript > function CodeBehindDDlJs(me) {"
strScript2 += "document.forms[0]." & uCtrlId & "_btnSubmit.focus();}"
'Here you can achieve id's by passinf this pointer too.
strScript2 += "alert('You selected '+ me.value)}"
strScript2 += "<"
strScript2 += "/"
strScript2 += "script>"
'<summary>
'Register javascript block to server control event.
'</summary>
If (Not Page.IsStartupScriptRegistered("Startup1")) Then
Page.RegisterStartupScript("Startup1", strScript1)
End If
If (Not Page.IsStartupScriptRegistered("Startup2")) Then
Page.RegisterStartupScript("Startup2", strScript2)
End If
'<summary>
'Call javascript function on controls event
'</summary>
ddlItem.Attributes.Add("onchange", "return CodeBehindDDlJs(this);")
btnSubmit.Attributes.Add("onClick", "return CodeBehindButtonJs();")
If we View Source any aspx page having the User Control, we will find that the dropdownlist source will look like this: <select name="UserJs1:ddlItem" id="UserJs1_ddlItem"
onchange="return CodeBehindDDlJs();" style="width:120px;" >
The dropdownlist ID in the above case is Public uCtrlId As String = ""
Implementation of Main.aspxNow we create the Main.aspx. In Main.aspx drag and register the User Control. In the HTML part, include the following property as shown. This will set the value of the User Control ID and it will be retrieved in the User Control for JavaScript validation. <body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<uc1:userJs id="UserJs1" runat="server" uCtrlId="UserJs1"> </uc1:userJs>
</form>
< /body>
We can see that our case was different. If we have to control any web control by itself like on a change event handler, we have to trap the selected index and then the above process won't be difficult. In such a case we have to pass the ' Note: I tried to trap the
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||