views:

270

answers:

1

Hello folks.

I am new to all of this so please be gentle! Ok, currently I have the following scenario.

I have three methods of searching for product details:

  1. A cascading drop down list (ddlbuyer, ddlSub, ddlProd) and an associated radiobutton (radProd)

  2. An autocomplete textbox that searches for product information (txtPrdAC)

  3. A product list search whereby a user enters the number of items they wish to see (txtHowMany) and then specifies which sub category they wish to see (radTopx)

What I would ideally like to have is the following situation.

A user can only use one of the methods of searching for products as list above. If they click on the first ddl then any data that may be present in the text boxes is cleared. Similarly if they click on the txtPrdAC or txtHowMany, any information that was present in the ddl list is cleared and the return back to it's original, default value.

I would like to do this in c# code behind and have so far got the following code that is working...ish! Well, it sets the focus of the radio button anyway.

 ddlProd.Attributes.Add("onchange", "return SetRadioFocus('" 
                                   + radBuyer.ClientID + "');");   
 radTopx.Attributes.Add("onclick", "return SetRadioFocus('" 
                                   + radTopx.SelectedItem + "');");   
 tbxProdAC.Attributes.Add("onclick", "return SetRadioFocus('" 
                                   + radProd.ClientID + "');");   

 ClientScript.RegisterClientScriptBlock(this.GetType(), "MyScript", @"   
          function SetRadioFocus(target)   
          {                    
            document.getElementById(target).checked = true;
          }", true);

However, I am still missing certain aspects i.e. the cleainr the text boxes and or ddl. Again, apologies for the ignorance, but this has thrown me. Thank you for any assistance that may come my way.

A: 

I can imagine a complete solution for this problem potentially being fairly complex, but a simple solution that I think would do what you're asking for could be as follows:

    ddlProd.Attributes.Add("onchange", "ResetDefaults('"+ddlProd.ClientID+"'); SetRadioFocus('"
                                      + radBuyer.ClientID + "');");
    radTopx.Attributes.Add("onclick", "ResetDefaults('"+radTopx.ClientID+"'); SetRadioFocus('"
                                      + radTopx.SelectedItem + "');");
    tbxProdAC.Attributes.Add("onclick", "ResetDefaults('"+tbxProdAC.ClientID+"'); SetRadioFocus('"
                                      + radProd.ClientID + "');");

    ClientScript.RegisterClientScriptBlock(this.GetType(), "MyScript", @"
      function ResetDefaults(exclude)
      {
        if(exclude != '" + ddlProd.ClientID + @"') document.getElementById('" + ddlProd.ClientID + @"').value = null;
        if(exclude != '" + txtPrdAC.ClientID + @"') document.getElementById('" + txtPrdAC.ClientID + @"').value = "";
        if(exclude != '" + txtHowMany.ClientID + @"') document.getElementById('" + txtHowMany.ClientID + @"').value = "";
      }
      function SetRadioFocus(target)   
      {
        document.getElementById(target).checked = true;
      }", true);

That being said, I'm not entirely sure if this is exactly the behavior that you would want. Whenever the user clicks one of these textboxes, the values in all of the other controls will be cleared, so I don't see how the user would be able to fill out the whole thing unless these fields are mutually exclusive and the user is only supposed to enter data into one of them. Otherwise, you may need to consider a refining this logic to only clear the control values under certain conditions.

Also, you might want to use the onblur event instead of onclick. The user may tab the focus to the controls instead of clicking on them, in which case I don't think the onclick event will be fired. The onblur event is triggered whenever the focus leaves the control. You may also want/need to add more javascript code that can detect if the user actually made any changes to the value of the control, or if they simply focused on the control and then left it.

Dr. Wily's Apprentice
Thanks a lot for the pointers Dr Wily - as you say, I think I may have to refine some of the logic but this looks an excelletn starter for me to work on.Thank you very much for looking into this for me.
MrDean