0

I have had a look at numerous other threads on here that have a similar problem but have been unable to resolve my issue. I'm using the following code for my combobox:

<sq8:ComboBox runat="server" ID="cboAgree"><Items>
<sq8:ComboBoxItem runat="server" Text="Agree"></sq8:ComboBoxItem>
<sq8:ComboBoxItem runat="server" Text="Disagree"></sq8:ComboBoxItem>
</Items>
</sq8:ComboBox>
<sq:BindableControl runat="server" TargetControlID="cboAgree" DataField="Agreement"></sq:BindableControl>

I want to get the value selected (either "Agree" or "Disagree") and use that as a condition to hide/unhide a grid on my form:

   if (value == "Disagree") {
  commentsGrid.style.visibility = "visible";
  }else{
  commentsGrid.style.visibility = "hidden";
  } 

I've tried a number of things but it seems like the problem is to do with the way the form loads and the combobox doesn't have a value at that point. I always get an error that my comboBox "cboAgree" is null.

Could anyone advise?

Adam
  • 7
  • 1
  • 7

1 Answers1

0

I believe what you want - based on how you have the code structured in your other post, is to add an event handler to your combo box so that when the selected index is changed, you check the selection.

This scenario is covered in the documentation here but basically what you want to do is:

  1. Add the following block inside your existing javascript:
function onSelectedIndexChanged(sender, eventArgs) 
{
    var item = eventArgs.get_item();

    if (value == "Disagree") {
        commentsGrid.style.display = "block";
    } else {
        commentsGrid.style.display = "none";
    } 
}

I would suggest setting the display property to none and block instead of using visibility with hidden and visible. This answer covers the differences and why you should prefer using the display property in this instance.

  1. Update your ComboBox to call the new Javascript method:
<sq8:ComboBox runat="server" ID="cboAgree"
onclientselectedindexchanged="onSelectedIndexChanged">
Matt Stannett
  • 2,700
  • 1
  • 15
  • 36
  • Hi Matt, thanks for the explanation! It works but when i inspect the page, i get the following error `Uncaught TypeError: Cannot read property 'get_item' of undefined` Also, i think it's maybe to do with the loading of the form.. the default value in the dropdown is "Agree" so initially the grid is hidden, when i change it to "Disagree" it appears but if i change it back to "Agree" nothing happens. I think this may be where the error is occuring? Is there anything I can add to the javascript code that will help with this? I will show you what i have already.. – Adam May 30 '19 at 08:12
  • `Sys.Application.add_load(FormLoad) function FormLoad() { onSelectedIndexChanged(true); } ` This is what i have for loading the form. – Adam May 30 '19 at 08:15
  • Hmmm so that error message tells me that `eventArgs` is undefined. If you inspect the rendered HTML is your ComboBox rendered as a `Select` control with some `Option` elements inside it? – Matt Stannett May 30 '19 at 09:36
  • If the above is true then the following should work: https://pastebin.com/cc4DfA61. – Matt Stannett May 30 '19 at 09:46
  • Hi Matt, thanks again for helping! I tried the code you've given and not get this error.. `Uncaught TypeError: Cannot read property 'undefined' of undefined` – Adam May 30 '19 at 10:23
  • I inspected the HTML element when the form is running and it shows this.. https://pastebin.com/jmqN89DE – Adam May 30 '19 at 10:28
  • Very interesting, I've never used Telerik controls before but it will be something silly. Are you able to upload a sample project the reproduces the error that I can run on my machine to see what's going on? – Matt Stannett May 30 '19 at 10:45
  • I don't think I can. I'm using a framework called Cora Sequence. The project is a process stored within the framework and can't be ran outside of it. – Adam May 30 '19 at 11:09
  • I downloaded the [ASP.NET Ajax examples](https://demos.telerik.com/aspnet-ajax/) from the Telerik site after following [this documentation](http://kb.pnmsoft.com/help/combobox-control-overview) which when I clicked on "Client-side programming overview" took me back to the [Telerik documentation](https://docs.telerik.com/devtools/aspnet-ajax/controls/combobox/client-side-programming/overview). I know you've tried this method before and got the undefined error when trying to call `get_item`. I got the following working after downloading the samples [code here](https://pastebin.com/ysa6FiXQ). – Matt Stannett May 31 '19 at 08:42
  • @Adam I'm not really sure what else to suggest sorry. Maybe try using the [debugger](http://kb.pnmsoft.com/help/working-with-the-debugger) functionality or reach out to the support team [here](http://support.pnmsoft.com/_forms/default.aspx?ReturnUrl=%2f_layouts%2f15%2fAuthenticate.aspx%3fSource%3d%252F) or [here](http://www.pnmsoft.com/resources/more-info/on-demand-knowledge/). – Matt Stannett May 31 '19 at 08:49
  • 1
    No problem @Matt ! Thanks for all your help. As i mentioned one of your methods worked partially so i can try and build on that! Thanks again! :) – Adam May 31 '19 at 11:15