locked
Event Validation and DropDownList RRS feed

  • Question

  • User-1454252679 posted

    Hi !

     I've recently started working on a small client-side framework for an ASP.NET application. Anyway.. a funky problem I got into:

    I have a DropdownList server control  created by ASP.NET containing just one ListItem. I later use Callbacks & Javascript on the client side to add more items to the list element. It works and the user can select from all the new options.  When I postback the data though, I get an invalid postback argument exception.

    Funny thing is : if I create the Dropdown with the same number of elements (ie. use Javascript only to change elements, not to ADD new ones) the event passes validation and the selection works fine.

    I know I can generally solve this by disabling page validaion, but I'd really like to find where the problem lays.

    As I understand, I must somehow RegisterForEventValidation diferently on the DropDownList, but I failed to find info on how I can use that...

    Sunday, January 15, 2006 8:34 AM

All replies

  • User-768391503 posted

    Problem is by adding elelemnts to a dropdown list it is not seen in viewstate and such and it says it is not there. The way to get around it is to place the selected value(s) in a hidden textbox and get the values that way.

    Eric

    Sunday, January 15, 2006 9:51 AM
  • User-1454252679 posted

    I see..

     what I don't understand though: what information does a  DropDownList element saves in the ViewState ? It would seem redundant to save all the item values and text. I guess a simple 'IdexSelected' would be enough, right

    And if only the index selected is saved, then I can't see what the validation problem is: the ViewState says the previously selected item was 0 ... now it is 3... get on with the postback.

     

     

    Monday, January 16, 2006 2:05 AM
  • User-2062274770 posted

    Radu,

       It *does* save all the information in Viewstate... but since only the index is passed when you post back to the server, none of the other changes can be recorded.

       However, this doesn't quite seem to touch your issue, which appears to concern some HTML or script in a form field.  Are you populating a hidden field with your values when they come back, and if so, would clearing that field before postback solve your problem?

    [edited by: PeterBrunone at 4:39 PM (GMT -5) on Mon, Jan 16 2006]
    *ahem*  Next time I should think about what I'm writing instead of just parroting the question!  The value -- not the index -- is passed to the server during postback.

    Also, it would appear that there's some sort of constraint in 2.0 whereby adding or removing items from the dropdownlist (client-side) will cause the request validator to freak out.  Is this what's happening to you?

    Monday, January 16, 2006 12:29 PM
  • User-1454252679 posted

    Also, it would appear that there's some sort of constraint in 2.0 whereby adding or removing items from the dropdownlist (client-side) will cause the request validator to freak out.  Is this what's happening to you?

    Yes, this is exactelly the problem ! As long as the number of items stays the same, the event validation is ok (even if the values are changed). Changing the number of items gives an error.!?

    So this is a known constraint? Any way to get arround it (i.e. without disabling validation for the page)?

    thanks for the help...

    Tuesday, January 17, 2006 2:32 AM
  • User-2062274770 posted

    I'm not 100% sure that this is indeed a known problem; right now I'm basing that (tentative) conclusion on some other questions I've seen.

    Can we see some code for this?  Ideally you would post a link to a page where we can see the problem, but if not, the rendered HTML and javascript would be some help.

     

    Tuesday, January 17, 2006 10:41 AM
  • User-1454252679 posted

    Hmm.. I can't post a link to a page.. it's a work-in-progress framework and it would be tedious to setup a public IIS server that supports this partial work..

    What I'm trying to do is creating an AJAX-enabled callback control. I have a custom HTTP module that listens for requests from the client (via XmlHTTP), it re-renders the Dropdown list into a string, then returns that string as a response. Javascript then assigns this text as the .innerHTML of the control.

    this markup will simulate this problem.

    <body>

    <form id="form1" runat="server">

    <div>

    <span id="Callback" >

    <asp:DropDownList ID="DropDownProblem" runat="server">

    <asp:ListItem Text="0" Value="1"></asp:ListItem>

    </asp:DropDownList>

    </span>

    <asp:Button ID="Submit" runat="server" Text="Submit" />

    </div>

    </form>

    <script type="text/javascript" >

    alert(document.getElementById('Callback').innerHTML);

    some = "<select name=\"DropDownProblem\" id=\"DropDownProblem\">";

    some = some +"<option value=\"1\">0</option>";

    some = some +"<option value=\"2\">1</option>";

    some = some +"<\select>";

    document.getElementById('Callback').innerHTML=some;

    alert(document.getElementById('Callback').innerHTML);

    </script>

    </body>

     

    Tuesday, January 17, 2006 11:33 AM
  • User-2062274770 posted

    All right, that's a start; maybe this will be close enough.

    For what it's worth, I would suggest assigning your string to the outerHTML of the dropdownlist rather than the innerHTML of the span... OR if you're working with more than just IE, set the innerHTML of the DDL to just the options list...  The reason for this is that there may be more to the dropdownlist HTML than you're adding here, so it might be good to leave the original <select> tag intact.

    Let me know if this has any effect whatsoever; just for the record, ELB doesn't have the same problems presented by a regular dropdownlist (not that you necessarily want to go there, but it bears mentioning)...

     

    Tuesday, January 17, 2006 12:07 PM
  • User-1454252679 posted

    Hmm... silly thing (and a bit offtopic). Changing the javascript code  to:

    alert(document.getElementById('DropDownProblem').innerHTML);

    some = some +"<option value=\"1\">0</option>";

    some = some +"<option value=\"2\">1</option>";

    document.getElementById('DropDownProblem').innerHTML=some;

    alert(document.getElementById('DropDownProblem').innerHTML);

    and now the innerHTML of the DDL instead of the expected

    <option value="1">0</options><option value="2">1</options>

    is :

    0</options><option value="2">1</options>

    Only in IE, but it eats away the first tag nomatterwhat.. kinda frustrating.

    PS. Yeah... I've already visited your site before! ELB looks quite feature-full (and I might just resign and start writing my own dropdown),alas is not an option for me: I have to write this framework for my company and my boss will never accept that I suggesst a third-party lib.

    Tuesday, January 17, 2006 12:35 PM
  • User1617854993 posted
    Hi Radu094,
     I am interested in implementing some client callbacks as I upgrade an older asp-based app. My question is : would it be too much to ask for some sample code on how you implement the client-callback ?
     If not possible then I understand. I'll simply continue my research.
    Have a great day.
    Bob        

    Tuesday, January 17, 2006 12:56 PM
  • User-2062274770 posted

    I don't know about Radu, but I use the following XMLHTTP code for client callbacks in ELB; this is pretty standard stuff

    var oRequest;

    var binIE = document.all;

     if (window.XMLHttpRequest) { // Mozilla
      oRequest = new XMLHttpRequest();
      oRequest.overrideMimeType('text/html');
      }
     else if (window.ActiveXObject) { // IE
      oRequest = new ActiveXObject("Microsoft.XMLHTTP");
      }
     oRequest.onreadystatechange = function() {
      if (oRequest.readyState==4) {
        var returnText;
        if(binIE) { returnText = oRequest.ResponseText; }
        else { returnText = oRequest.responseText; }
        doSomeFunctionWith(returnText);

        return;
        }

     oRequest.open('GET', sRequestURL, true);
     oRequest.send(null);

    Tuesday, January 17, 2006 2:38 PM