Showing posts with label extenders. Show all posts
Showing posts with label extenders. Show all posts

Monday, March 26, 2012

causing validation from client side without postback?

Now that I added callout extenders to my validators how can I get them to work 1- on the client before calling an AJAX webservice and 2- in the webservice? I am not using an update panel so can I turn off server side validation and write my own for the webservice to call?

Hi Echo,

Echo88:

Now that I added callout extenders to my validators how can I get them to work 1- on the client before calling an AJAX webservice and 2- in the webservice? I am not using an update panel so can I turn off server side validation and write my own for the webservice to call?

My understanding of your issue is that you want to validate the inputs. If all these are correct then call the WebService ,otherwise, show the error message by using ValidatorCalloutExtender. If I have misunderstood, please feel free to let me know.

Validators are call before the form is submitted , but to call the WebService we don't need to postback. So I suggest that you should write your own validate Javascript code instead of ValidatorCalloutExtender controls.

Here is the sample to call a WebService.

<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="DataTableService.asmx" InlineScript="true" />
</Services>
</asp:ScriptManager
<input type="button" value="Get DataTable" onclick="getDataTable();" /
<div id="result"></div
<script language="javascript" type="text/javascript">
function getDataTable()
{
DataTableService.GetDataTable(onSucceeded, onFailed);
}

function onSucceeded(result)
{
//handle code
}

function onFailed(error)
{
//handle code
}

You can also transfer all the inputs and validate on the server side in the WebService.

I hope this help.

Best regards,

Jonathan


Thanks - sucks that I can not use those jazzy callouts. But a question I have is - doesn't it call a client script prior to postback?? My understanding is that validation takes place before posting the form data to the server, so one would think there is a call to a script that precedes the actual postback. Am I wrong?


Hi Echo88,

But a question I have is - doesn't it call a client script prior to postback??

Yes , you are right. The problem is that if all the inputs are correct , the form will postback next. But to call a webservice , we usually do not need to postback. It will cause the page refreshment. Based on this , I think write your own Javascript function is a better way in this situation. If you don't want to validate on the client side , you can also transfer all the inputs to the server side and validate on the WebService.

function setBoy()
{
var boy = new Object();
boy.Name = "Terry";
var girl = new Object();
girl.Name = "Mary";
boy.GirlFriend = girl;

BoyGirlService.SetBoyWithGirlFriend(boy, onSetBoySucceeded, onFailed); // call the WebService.
}

I hope this help.

Best regards,

Jonathan


exactly - I am calling a webservice instead of a postback. But am I right to say there should be a way to cause validation on the client - regardless of postback? I just thought there would be a method to execute independently. What aboutPage_ClientValidate() ??


Hi Echo88,

Yes , I agree with you. You can use it like this.

Button1.Attributes["OnClick"] = "Javascript:if (typeof(Page_ClientValidate) == 'function') if(Page_ClientValidate()) {return confirm('Are you sure to submit ?'); } else return false;";

Best regards,

Jonathan


I tried that - no dice. The button is set to cause validation and uses submit behavior. However, nothing is happening? I guess it is not finding Page_ClientValidate function. When include Page_ClientValidate by itself - it throws an error. So why is not included?


Hi Echo88,

Sorry for the delay. Here is my test sample and it works pretty well.

<%@. Page Language="C#" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script runat="server"> protected void Page_Load(object sender, EventArgs e) { Button1.Attributes["OnClick"] = "Javascript:if (typeof(Page_ClientValidate) == 'function') if(Page_ClientValidate()) {return confirm('Are you sure to submit ?'); } else return false;"; }</script><html xmlns="http://www.w3.org/1999/xhtml" ><head runat="server"> <title>Untitled Page</title></head><body> <form id="form1" runat="server"><%=DateTime.Now.ToString()%> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="RequiredFieldValidator" ControlToValidate="TextBox1"></asp:RequiredFieldValidator> <asp:Button ID="Button1" runat="server" Text="Button" UseSubmitBehavior="true" CausesValidation="true" /> </form></body></html>
So please compare it with yours and if it doesn't work, please feel free to let me know with more information. Thanks.

Best regards,

Jonathan


Your example works - even though it does perform a postback - I turned off the submitbehavior. Still works:) so there is hope- I suspect the

Page_ClientValidate function is not getting generated somehow. Could the fact that I am using them all as part of a user control be the problem?
Thanks - its really appreciated. 

Ok - I found out something interesting... The Page_Validators array is not populating on this one page. Any ideas why they are not populating Page_Validators ?


OK - I found the root of the cause. In my control I have readonly mode that would make all validators invisible if it was in readonly mode. Apparently, I did set the function incorrectly and all validators were not being rendered.

Saturday, March 24, 2012

CascadingDropDown setting SelectedValue programmatically doesnt work

Hi

I have a page which has four dropdowns "linked" together via CascadingDropDown Extenders.

The page works fine, except that I sometimes want the dropdowns to be pre-populated and specific items selected programmatically.

e.g.

1cddlMakes.SelectedValue = cmk.Vehicle.Make.ID2cddlModels.SelectedValue = cmk.Vehicle.Model.ID3cddlTypes.SelectedValue = cmk.Vehicle.Type.ID4cddlEngines.SelectedValue = cmk.Vehicle.Engine.ID

The first 2 dropdowns load and display the correct selected value, but the other two dropdowns SelectedValues don't get set and stay on the "Please Select..." option.

Any ideas?

Using the sample page, I have no issue programmatically setting any of the 3 cascading drop down extenders' selected value. Are you setting the extenders or the controls?
12 CascadingDropDown1.SelectedValue ="Acura";3 CascadingDropDown2.SelectedValue ="Integra";4 CascadingDropDown3.SelectedValue ="Green";

Wednesday, March 21, 2012

CascadingDropDown Memoryleak, something with ParentControlID?

Hi, I'm currently using two cacadingdropdown extenders which retrieves data from webmethods (no webservices) embedded in aspx page. One is the parent of the other. And now each time I refresh the memory increases 800-1000K at the time. But the strange thing is that if i remove the parent child relationship (no extender has parentcontrolid defined) the memory leaks do not accur.

Am I doings something wrong, is something missing in webconfig?

12<!--34-->5<configuration>6<appSettings/>7<connectionStrings>8<add name="UtstyrConnectionString" connectionString="Data Source=WIN2003STDBASE;Initial Catalog=Utstyr;Integrated Security=True" providerName="System.Data.SqlClient"/>9</connectionStrings>10<system.web>11<pages enableEventValidation="false"></pages>1213<compilation debug="true">14<assemblies>15<add assembly="Microsoft.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>16<add assembly="System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/></assemblies></compilation>1718<authentication mode="Windows"/>19 <identity impersonate="true"/>20 <authorization>21 <allow users="*"/>22 </authorization>23<httpHandlers>24<remove verb="*" path="*.asmx"/>25<add verb="*" path="*.asmx" validate="false" type="Microsoft.Web.Script.Services.ScriptHandlerFactory, Microsoft.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>26<add verb="GET" path="ScriptResource.axd" type="Microsoft.Web.Handlers.ScriptResourceHandler" validate="false"/>27</httpHandlers>28<httpModules>29<add name="WebResourceCompression" type="Microsoft.Web.Handlers.WebResourceCompressionModule, Microsoft.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>30<add name="ScriptModule" type="Microsoft.Web.UI.ScriptModule, Microsoft.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>31</httpModules>32</system.web>33</configuration>

Cheers GauteO

PS both my dropdownboxes and cacadingdropdownextenders are in an updatepanel

Here is a simple sample of what I mean. If anybody could run this to determine if it is a internal problem.

1<%@. Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>2<%@. Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc2" %>3<%@. Register Assembly="Microsoft.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"4 Namespace="Microsoft.Web.UI" TagPrefix="asp" %>5<script runat="server">6 [System.Web.Services.WebMethod]7 [Microsoft.Web.Script.Services.ScriptMethod()]8 public static AjaxControlToolkit.CascadingDropDownNameValue[] Get(string knownCategoryValues, string category)9 {10 int count = 5;11 AjaxControlToolkit.CascadingDropDownNameValue[] cddnv = new AjaxControlToolkit.CascadingDropDownNameValue[count];12 for (int i = 0; i < count; i++)13 cddnv[i] = new AjaxControlToolkit.CascadingDropDownNameValue("Gaute" + i, i.ToString());14 return cddnv;15 }16 </script>17<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">18<html xmlns="http://www.w3.org/1999/xhtml" >19<head runat="server">20 <title>Untitled Page</title>21</head>22<body>23 <form id="form1" runat="server">24 <div>25 <asp:ScriptManager ID="ScriptManager1" runat="server">26 </asp:ScriptManager>27 <asp:DropDownList ID="DropDownList1" runat="server">28 </asp:DropDownList>29 <asp:DropDownList ID="DropDownList2" runat="server">30 </asp:DropDownList>31 <cc2:CascadingDropDown ID="CascadingDropDown1" runat="server" Category="Valg" LoadingText="Laster" PromptText="Velg" ServiceMethod="Get" TargetControlID="DropDownList1">32 </cc2:CascadingDropDown>33 <cc2:CascadingDropDown ID="CascadingDropDown2" runat="server" Category="Velger" LoadingText="Laster" PromptText="Velg" ServiceMethod="Get" TargetControlID="DropDownList2" ParentControlID="DropDownList1">34 </cc2:CascadingDropDown>35 </div>36 </form>37</body>38</html>

This code gives me a memory leak of 700-1000k each time the page posts back. And if I remove the ParentControlId from CacadingDropDown2 (line 33) the memomry leaks stops. Testet the samplewebsite in ajaxtoolkit (cacadingdropdown) and that didn't have the memoryleaks. I have tried to elumate(copy) the procedure in the site, but cant stop the leaks.

GauteO


*bump*

also tried with RC1 release, same problem..


What Browser are you testing with? Or is this problem occuring in all browsers?

The reason I ask, is that I found that several issues with the CascadingDropDowns disappeared when I moved to IE 7.0. For example, with IE 6.x I have found that having two different sets of CascadingDropDowns on the same page will break the functionality of the SelectedValue tags... Only the first set of CascadingDropDowns will get the correct initial value...


Currently I have only testet with ie6, I shall test on other browser when I get to work again (as I'm currently home). I usally test in opera, firefox, ie6 and ie7+(vista browser). But I am really cursious if anybody else get this memory leak when one of the cascadingdropdown extenderes has an parentcontrolid assign to it, and stops when none have an parentcontrolid. They provide/retrieves the same information in both instances, only diffrence that the cacadingdropdown with parentcontrolid has the "knownvalues" of its parent (and 700-1000k is pretty much for 5 values beeing sent, as in the sample in the second post). Maybe I must embed the information in XML (as they do in the sample site ajaxcontrolkit)?

Hmm, I have testet the scenario on opera and ie7+ and the memory leaks do not accur. I also testet the sitehttp://ajax.asp.net/ajaxtoolkit/CascadingDropDown/CascadingDropDown.aspx again, with ie6, and the memory leak problem accurs there too (was sure that I tested this before and didn't have the problem, it was beta2 not rc1 which I testet now). It seems that ie6 is the problem, I am running 2003 server on a virtual pc(vmware), but it does not have the latest updates (trying to install them, but windows update just stops). Maybe this problem only accurs on old versions of ie6 (IE that follows a clean install of 2003 server), if anybody could confirm this it would be of great help (use the cacadingdropdown example on the ajaxcontrolkit website). My current version of ie6 is:

GauteO


That this doesn't happen in other browsers (including IE7) suggests to me that it's probably not something the Toolkit is doing wrong. Please let us know what happens with an updated IE6.

CascadingDropDown in repeater

I have three dropdownlists, three cascadingdropdown extenders, and updatepanel within a repeater. I need to update a label control inside of the updatepanel when the selectedindexchanged event fires for the last dropdown. The problem I'm having is that the event doesn't seem to fire the first time the selectedindex is changed. It works as expected after changing the first time. Anyone have any ideas as to why this is happening. Here is my code.

<asp:RepeaterID="uxPublicationRepeater"OnItemDataBound="PublicationRepeater_ItemDataBound"OnItemCommand="Pub_ItemCommand"runat="server">

<ItemTemplate>

<ajax:CascadingDropDownID="uxPublicationCascading"runat="server"

Category="Publication"

ServiceMethod="GetPublicationsByMarket"TargetControlID="uxPubDropDownList"

ParentControlID="uxMarketDropDownList"PromptText="Select.."LoadingText="Loading.."/>

<ajax:CascadingDropDownID="uxPublicationYearCascading"runat="server"

Category="PublicationYear"PromptText="Select.."LoadingText="Loading.."

ServicePath="~/Services/LookupWebService.asmx"ServiceMethod="GetYearsByPublication"

TargetControlID="uxPublicationDateDropDownList"ParentControlID="uxPubDropDownList"/>

<asp:DropDownListID="uxMarketDropDownList"runat="server"/>

<asp:DropDownListID="uxPubDropDownList"runat="server"/>

<asp:DropDownListID="uxPublicationDateDropDownList"OnSelectedIndexChanged="IndexChanged"AutoPostBack="true"runat="server"/>

<asp:UpdatePanelID="uxUpdatePanel"ChildrenAsTriggers="false"UpdateMode="conditional"runat="server">

<ContentTemplate>

<asp:LabelID="uxLabel"runat="server"/>

</ContentTemplate>

<Triggers>

<asp:AsyncPostBackTriggerControlID="uxPublicationDateDropDownList"EventName="SelectedIndexChanged"/>

</Triggers>

</asp:UpdatePanel>

</ItemTemplate>

</asp:Repeater>

Thanks for the help.

Have you taken a look at the sample?http://www.asp.net/AJAX/Control-Toolkit/Live/CascadingDropDown/CascadingDropDown.aspx

This could be related to EventValidation (see http://www.asp.net/AJAX/Control-Toolkit/Live/Walkthrough/CCDWithDB.aspx specfically:"Finally, in order for the values to be submitted, EventValidation needs to be disabled for the page. EventValidation ensures that the values in each control match the values that were present when the page was rendered, but since these drop downs are populating on the client side, this is never true.")

-Damien


I tried turning event validation off and still no good. The event fires the first time if the controls are outside of the repeater, but doens't fire until the second time when inside the repeater.


No one else has ran into this problem? If I take the cascadingdropdown extender out, the events fire correctly. Just to recap, dropdownlists in a repeater, selectedindexchange event fires correctly. Dropdownlists and cascadingdropdown extenders inside a repeater the event doesn't fire until the second time the selectedindex is changed. If anyone is interested I have a small sample app that illustrates the problem.


c.barnes01:

No one else has ran into this problem? If I take the cascadingdropdown extender out, the events fire correctly. Just to recap, dropdownlists in a repeater, selectedindexchange event fires correctly. Dropdownlists and cascadingdropdown extenders inside a repeater the event doesn't fire until the second time the selectedindex is changed. If anyone is interested I have a small sample app that illustrates the problem.

Hi,

I'd like to see your small sample, it will be preferred to use NorthWind database or a fake data source built in code.


Here is a small app that illustrates the problem.

<%@. Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" EnableEventValidation="false" Inherits="_Default" %><%@. Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> <title>Untitled Page</title></head><body> <form id="form1" runat="server"> <asp:ScriptManager ID="ScriptManager1" runat="server" /> <div> <asp:Repeater ID="uxTestRepeater" OnItemDataBound="Repeater_DataBound" runat="server"> <ItemTemplate> <cc1:CascadingDropDown ID="uxCountriesCascading" Category="Countries" runat="server" ParentControlID="uxCountryDropDownList" TargetControlID="uxStatesDropDownList" LoadingText="Loading.." PromptText="Select.." ServiceMethod="GetScopedStates" /> <asp:DropDownList ID="uxCountryDropDownList" runat="server" /> <asp:DropDownList ID="uxStatesDropDownList" AutoPostBack="true" OnSelectedIndexChanged="SelectedIndex_Changed" runat="server" /><br /> <asp:UpdatePanel ID="uxUpdatePanel" runat="server"> <ContentTemplate> <asp:Label ID="uxStateLabel" runat="server" Text="Select a State" /> </ContentTemplate> <Triggers> <asp:AsyncPostBackTrigger ControlID="uxStatesDropDownList" EventName="SelectedIndexChanged" /> </Triggers> </asp:UpdatePanel> <hr /> </ItemTemplate> </asp:Repeater> </div> </form></body></html>

using System;using System.Collections;using System.Collections.Generic;using System.Collections.Specialized;using System.Data;using System.Configuration;using System.Web;using System.Web.Security;using System.Web.Services;using System.Web.Script.Services;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;using AjaxControlToolkit;public partialclass _Default : System.Web.UI.Page{protected void Page_Load(object sender, EventArgs e) {if (!Page.IsPostBack) {this.uxTestRepeater.DataSource = GenerateRepeaterDataSource();this.uxTestRepeater.DataBind(); } }private List GenerateRepeaterDataSource() { List toReturn =new List(); toReturn.Add(new MyClass(1,"Chris"));return toReturn; }protected void Repeater_DataBound(object sender, RepeaterItemEventArgs e) { DropDownList countryDropDownList = e.Item.FindControl("uxCountryDropDownList")as DropDownList; countryDropDownList.DataSource = GenerateCountries(); countryDropDownList.DataTextField ="Value"; countryDropDownList.DataValueField ="Key"; countryDropDownList.DataBind(); }private Dictionary<int,string> GenerateCountries() { Dictionary<int,string> toReturn =new Dictionary<int,string>(); toReturn.Add(1,"USA"); toReturn.Add(2,"Canada"); toReturn.Add(3,"Mexico");return toReturn; }protected void SelectedIndex_Changed(object sender, EventArgs e) { DropDownList stateDropDown = senderas DropDownList; Label stateLabel = stateDropDown.NamingContainer.FindControl("uxStateLabel")as Label; stateLabel.Text = stateDropDown.SelectedItem.Text; } [WebMethod] [ScriptMethod]public static CascadingDropDownNameValue[] GetScopedStates(string knownCategoryValues,string category) { CascadingDropDownNameValue[] toReturn =null; StringDictionary values = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);if (values.Count == 1) {string key = values["undefined"];switch (key) {case"1": toReturn = GetStatesForUsa();break;case"2": toReturn = GetStatesForCanada();break;case"3": toReturn = GetStatesForMexico();break;default:break; } }return toReturn; }public static CascadingDropDownNameValue[] GetStatesForUsa() { CascadingDropDownNameValue[] toReturn =null; toReturn =new CascadingDropDownNameValue[5]; toReturn[0] =new CascadingDropDownNameValue("SC","1"); toReturn[1] =new CascadingDropDownNameValue("GA","2"); toReturn[2] =new CascadingDropDownNameValue("NY","3"); toReturn[3] =new CascadingDropDownNameValue("FL","4"); toReturn[4] =new CascadingDropDownNameValue("NC","5");return toReturn; }public static CascadingDropDownNameValue[] GetStatesForCanada() { CascadingDropDownNameValue[] toReturn =null; toReturn =new CascadingDropDownNameValue[5]; toReturn[0] =new CascadingDropDownNameValue("Saskatchewan","1"); toReturn[1] =new CascadingDropDownNameValue("Alberta","2"); toReturn[2] =new CascadingDropDownNameValue("British Columbia","3"); toReturn[3] =new CascadingDropDownNameValue("Nunavut","4"); toReturn[4] =new CascadingDropDownNameValue("Yukon","5");return toReturn; }public static CascadingDropDownNameValue[] GetStatesForMexico() { CascadingDropDownNameValue[] toReturn =null; toReturn =new CascadingDropDownNameValue[5]; toReturn[0] =new CascadingDropDownNameValue("Hidalgo","1"); toReturn[1] =new CascadingDropDownNameValue("Veracruz","2"); toReturn[2] =new CascadingDropDownNameValue("Puebla","3"); toReturn[3] =new CascadingDropDownNameValue("Durango","4"); toReturn[4] =new CascadingDropDownNameValue("Sonora","5");return toReturn; }}public class MyClass{private int id;public int ID {get {return id; }set { id =value; } }private string name;public string Name {get {return name; }set { name =value; } }public MyClass(int id,string name) {this.id = id;this.name = name; }}

I still haven't found a solution to this yet. Anyone have any suggestions?

Thanks


Bump


What is the full type name of the toReturn( List toReturn =new List();) object?


It should have been, List<MyClass> toReturn = new List<MyClass>();

thanks


I looked into it through, and found out that the reason is because the items are filled on client side.

1. On the first request( when isPostback is false ), there is no items in DDLState.

2. DDLState is filled when the page has been rendered in the browser

3. Select an item in the DDL to start a postback

4. The DDL tries to find the currentIndex( it's -1 since it can't be found ) and compares it with previousIndex( it's -1 since there is no item previously ) to see if a selectedChanged event should be fired. And it won't be fired because they are equal

5. On the second request, the previously posted item is kept in ViewState and subsequently added into DDLState's items collection

6. The previously index is 0 because the only item currently in DDLState matches the previous value. And currently posted item's value still doesn't exist, so currentIndex is -1. They are not equal, thus the SelectedIndexChanged event is fired.



Thanks for looking into the problem. It's weird that it works when not in a repeater.