tags:

views:

85

answers:

1

everything works great except that when event fire (btnSearch_click) the control (txtSubject, or dates or ddlStatus, depends on what i have selected from the dropdonlist) disappears...

here is my code...:

<script type="text/javascript">

        $(document).ready(function() {

        $('.ddlFilter').change(function() { 

                var sel = $(this).val();

                $('#div_date').hide();
                $('#div_subject').hide();
                $('#div_status').hide();

                if (sel === 'Date') {
                    $('#div_date').show();
                }
                else if (sel == 'Subject') {
                    $('#div_subject').show();
                }
                else if (sel == 'Status') {
                    $('#div_status').show();
                }
            });

        }); 

    </script>



 <div id="select">
            Filter Results by:

            <asp:DropDownList CssClass="ddlFilter"   ID="ddlFilterResultBy" 
                        runat="server" Width="221px"> 
                <asp:ListItem Text="Select..." Value=""></asp:ListItem> 
                <asp:ListItem Text="Date" Value="Date"></asp:ListItem> 
                <asp:ListItem Text="Subject" Value="Subject"></asp:ListItem> 
                <asp:ListItem Text="Status" Value="Status"></asp:ListItem> 
            </asp:DropDownList> 

            </div>
            <div id="holder">
            <div id="div_date" style="width: 250px; display: none;" class="sectionrowDate">
                Date Range:

                <uc1:DatePicker ID="dpFromDate" runat="server"   />
                <uc1:DatePicker ID="dpToDate" runat="server"  />
            </div>
            <div id="div_subject" style="display: none;" class="sectionrow">
                Subject: 
                <asp:TextBox ID="txtSubject" runat="server" Width="225px" SkinID="Picker"></asp:TextBox>
            </div>

            <div id="div_status" style="display: none;" class="sectionrow">
                Status:

                <asp:DropDownList DataSourceID="ods_status" DataValueField="Id" DataTextField="Name"
                    AppendDataBoundItems="true" ID="ddlStatus" runat="server" Width="152px">
                </asp:DropDownList>

            </div> 
            </div>

            <asp:Button ID="btnSearch" Text="Search" runat="server" OnClick="btnSearch_Click" />
        </div>
A: 

When your btnSearch's click event is fired, you're doing a full postback back to the server. As a result, your ASPX page is getting rendered based on how you have it defined in the ASPX page above. In this case, div_date, div_status, and div_subject are rendered with a display of none. And your select list will reset to the default item (which would be the first ListItem).

You need to keep track of what's visible and what's selected before the postback occurs if you want to keep the same behavior (full postback). I have a few thoughts on how to handle this.

You may want to have a hidden input element who's value you set in each if {} statement in your JavaScript's .change() handler. If that hidden value is set to runat="server", then you can more easily get it's value on the server during your Page_Load and then set the right visibility of your div's (although those div's will also have to be runat="server" unless you render them dynamically.

<input type="hidden" id="hidCriteria" runat="server" />

and your JavaScript would have...

$("#hidCriteria").val("DATE");  //or "STAT" or "SUBJ" depending on what's selected

in each if(sel == 'xxx') section in order to keep track of what's visible.

I think you could also change your div_date, div_status and div_subject to asp:Panel's and I think their state (visible or not) will be kept in viewstate and be preserved on a postback. I'm not 100% sure of that, but that could be an easier test to try out before the hidden input control.

Something else to consider is if the btnSearch click event is to keep the user on the same page, maybe you could wire up the btnSearch's click event to a jQuery $.ajax() call to do an async post to the server to do your work and keep your UI untouched by a postback. You could do this:

$("#btnSearch").click(function() {
  $.ajax({
    url: 'myService.svc/DoSearch', //url you're posting to,
    type: 'POST',
    dataType: 'json', //up to  you...could be 'xml', 'text', etc.
    data: {
      //your search criteria here
    },
    ...
  });
});

Sorry if this was kind of long. Hope this helps!

David Hoerster
i really wanted to use jquery ajax to post and reterive my data but the problem i faced is displaying the data in the gridview controlyes i am posting to the same page but my search dropdonwlist is not in the div so it holds the value of whatever i have selected.... is there a way i can execute jquery after it postback?
Abu Hamzah
Sure, anything is possible. :) But I think that using jquery to populate a gridview may not he the right approach. I've never tried it. You may be better off looking at a client-side grid like jqgrid or flexigrid that are jquery-based grids. You'd probably have to re-architect your solution for that and introduce wcf services or something like that. So using jquery to populate a gridview probably isn't an easy task; but using a client-side grid may be. It really depends on your app's requirements.
David Hoerster