当页面重新加载时,jquery脚本工作

时间:2012-10-24 06:50:13

标签: asp.net ajax jquery

你好我有一个页面,其中我在文本框的自动完成事件中使用了ajax,即一些列表填充在文本框中,因此选择的项目被添加到网格中它仅在第一次页面重新排列时但在项目添加到网格中之后有效autocomplete事件停止为此工作我必须重新调整页面,但我不这样,因为当我刷新页面时,网格中添加的所有项目都将丢失,当我最终点击网格中的保存按钮项目时,我也有问题,我有另一个同一页面上的文本框我在自动完成事件中填充了列表,但是当我在控件中键入一些值时它也无法工作,只有当第一页被重新调整时才有效,所以这是什么问题

<div id="inner">
  <fieldset>
        <legend>Purchase Order</legend>
       <asp:UpdatePanel ID="UpdatePanel1" runat="server">
       <ContentTemplate>


            <table style="width:44%;">

               <tr>


                   <td>
                       <div class="dvlefttd2">
                           <asp:Label ID="lblpartyname" runat="server" Text="Party Name"></asp:Label>
                       </div>
                   </td>
                   <td colspan="3">
                    <div class="ui-widget" style="display:block;margin-top:18px;">
                            <asp:TextBox style="min-width:216px" ID="tbAuto" class="tb" runat="server">
                            </asp:TextBox>
                            <div style="visibility:hidden">
                            <asp:TextBox runat="server" ID="tbHidden"  class="tb"></asp:TextBox></div>
                    </div>
                   </td>
                 </tr>
                <tr>

                    <td>
                        <div class="dvlefttd2">
                            <asp:Label ID="lblQno" runat="server" Text="Q. No."></asp:Label>
                        </div>
                    </td>
                    <td>
                   <asp:TextBox ID="txtQno" runat="server" 
                     autocomplete="off" MaxLength="8" 
                            onkeypress="return isNumberKey(event)" 
                    Width="75px">
                    </asp:TextBox>
                    </td>
                    <td>


                </tr>
                <tr>

                    <td>

                        <div class="dvlefttd1">
                            PoNo
                        </div>

                    </td>
                    <td>
                        <div style="margin-top:-10px;">
                            <asp:TextBox ID="txtPoNo" runat="server" autocomplete="off" MaxLength="8" 
                                onkeypress="return isNumberKey(event)" ReadOnly="True" Width="75px"></asp:TextBox>
                        </div>
                    </td>
                    <td ID="tdcheck" style="width:195px">
                        <div class="dvlefttd2" style="padding-top:20px">
                            <asp:CheckBox ID="chkConsignee" runat="server" AutoPostBack="True" 
                                OnCheckedChanged="chkConsignee_CheckedChanged" Text="Consignee" 
                                TextAlign="Left" />
                        </div>
                    </td>
                   <td id="ColConsignee" runat="server" colspan="3">
                            <asp:DropDownList ID="ddlConsignee" runat="server" AutoPostBack="True" 
                                OnSelectedIndexChanged="ddlConsignee_SelectedIndexChanged" Width="221px">
                            </asp:DropDownList>
                            <asp:TextBox ID="txtConsignee" runat="server" autocomplete="off" MaxLength="30" 
                            Visible="false" Width="100px"></asp:TextBox>
                    </td>

                </tr>
              </table>
           <fieldset>
           <legend>Item Info

           </legend>
               <table style="width:100%;">
                   <tr>
                       <td colspan="2">
                       <div class="dvitems">
                          Item
                       </div>
                       </td>

                       <td>
                       <div class="dvitems">
                            Grain
                        </div>
                        </td>
                       <td style="width: 79px">
                       <div class="dvitems">
                           Unit
                       </div>   
                        </td>
                       <td style="width: 79px">
                       <div class="dvitems">
                       Cases
                       </div>
                       </td>
                       <td>

                        </td>
                   </tr>
                   <tr>
                       <td style="width:44px" valign="top" colspan="2">


                       <div class="ui-widget" style="display:block;">
                       <asp:TextBox style="min-width:226px;min-height:20px;" ID="txtitem" class="tb" runat="server">
                       </asp:TextBox>
                       <div style="visibility:hidden">
                       <asp:TextBox runat="server" ID="hditem"  class="tb"></asp:TextBox>
                       </div>
                       </div>
                       </td>

                       <td style="width:49px" valign="top">

                           <asp:DropDownList ID="ddlGrain" runat="server" style="min-width:100px;min-height:20px">
                               <asp:ListItem>Long</asp:ListItem>
                               <asp:ListItem>Short</asp:ListItem>
                           </asp:DropDownList>
                       </td>
                       <td  valign="top">

                           <asp:DropDownList ID="ddlUnit" runat="server" style="min-height:27px;min-width:100px">
                           </asp:DropDownList>
                       </td>
                       <td  valign="top">
                           <asp:TextBox ID="txtcases" runat="server" style="min-height:20px;min-width:100px"></asp:TextBox>
                       </td>
                       <td valign="top" style="width:49px">

                           &nbsp;</td>
                   </tr>


                   <tr>
                       <td style="width:44px">

                           <div class="dvitems">
                               Qty
                           </div>

                        </td>
                       <td style="width:44px">
                         <div class="dvitems">
                               Rate
                           </div>
                       </td>
                       <td>



                           <div class="dvitems">
                               Amt
                           </div>



                       </td>
                       <td colspan="2">
                       <div class="dvitems">
                       Remark 
                       </div>   

                       </td>

                       <td>
                       </td>
                   </tr>


                   <tr>
                       <td style="width: 44px">
                           <asp:TextBox ID="txtQty" runat="server" autocomplete="off" MaxLength="5" 
                               onkeypress="return isNumberKey(event)" style="min-height:20px;min-width:100px"></asp:TextBox>
                       </td>
                       <td style="width: 44px">
                       <asp:TextBox ID="txtRate" runat="server" 
autocomplete="off" MaxLength="6" 
                               onblur="setAmount();"  
                               OnTextChanged="txtRate_TextChanged" 
style="min-width:100px;min-height:20px"></asp:TextBox>
                       </td>
                       <td>
                           <asp:TextBox ID="txtAmount" runat="server" autocomplete="off" Enabled="False" 
                               MaxLength="7" style="min-width:100px"></asp:TextBox>
                       </td>
                       <td colspan="2">
                           <asp:TextBox ID="txtRemark" runat="server" autocomplete="off" MaxLength="50" 
                               style="min-width:237px"></asp:TextBox>
                       </td>

                       <td>
                           <asp:Button ID="btnAdd" runat="server" CssClass="btn" OnClick="btnAdd_Click" 
                               Text="Add" />
                       </td>
                   </tr>


               </table>
               <asp:Panel ID="Panel1" runat="server" Height="120px" ScrollBars="Both">
                                                <div id="dvgrid" runat="server">
                                                <asp:GridView ID="gvItem" runat="server" CssClass="mGrid" 
                                                        AutoGenerateColumns="False"  PagerStyle-CssClass="pgr"  
AlternatingRowStyle-CssClass="alt"
                                                    CellPadding="4" ForeColor="#424242" GridLines="None" Height="84px" 
                                                    OnRowCancelingEdit="gvItem_RowCancelingEdit" OnRowDeleting="gvItem_RowDeleting" 
                                                    OnRowEditing="gvItem_RowEditing" OnRowUpdating="gvItem_RowUpdating" 
                                                    OnSelectedIndexChanged="gvItem_SelectedIndexChanged">
                                                    <AlternatingRowStyle BackColor="White" />
                                                    <Columns>
                                                        <asp:CommandField HeaderText="Edit" ShowEditButton="True" />
                                                        <asp:BoundField DataField="No" HeaderText="SrNo">
                                                            <ItemStyle />
                                                        </asp:BoundField>
                                                        <asp:BoundField DataField="Size" HeaderText="Size">
                                                            <ItemStyle  />
                                                        </asp:BoundField>
                                                        <asp:BoundField DataField="GSM" HeaderText="GSM">
                                                            <ItemStyle  />
                                                        </asp:BoundField>
                                                        <asp:BoundField DataField="Grain" HeaderText="Grain">
                                                            <ItemStyle />
                                                        </asp:BoundField>
                                                        <asp:BoundField DataField="UnitName" HeaderText="Unit" />
                                                        <asp:BoundField DataField="Qty" DataFormatString="{0:F}" HeaderText="Qty">
                                                            <ItemStyle />
                                                        </asp:BoundField>
                                                        <asp:BoundField DataField="Rate" DataFormatString="{0:F}" HeaderText="Rate">
                                                            <ItemStyle  />
                                                        </asp:BoundField>
                                                        <asp:BoundField DataField="Total" HeaderText="Total" />
                                                        <asp:BoundField DataField="Remark" HeaderText="Remark">
                                                            <ItemStyle />
                                                        </asp:BoundField>
                                                        <asp:CommandField HeaderText="Delete" ShowDeleteButton="True" />
                                                        <asp:TemplateField>
                                                            <ItemTemplate>
                                                                <asp:HiddenField ID="hdnCatCode" runat="server" Value='<%# Eval("CCode") %>' />
                                                                <asp:HiddenField ID="hdnItem" runat="server" Value='<%# Eval("ICode") %>' />
                                                                <asp:HiddenField ID="hdnUnitID" runat="server" Value='<%# Eval("UnitID") %>' />
                                                            </ItemTemplate>
                                                        </asp:TemplateField>

                                                            

                                                    </Columns>
                                                    <EditRowStyle BackColor="#CCCCCC" />
                                                    <FooterStyle BackColor="#424242" Font-Bold="True" ForeColor="White" />
                                                    <HeaderStyle BackColor="#424242" Font-Bold="True" ForeColor="White" />
                                                    <PagerStyle  ForeColor="White" HorizontalAlign="Center" />
                                                    <RowStyle BackColor="White" />
                                                    <SelectedRowStyle BackColor="#fff" Font-Bold="True" ForeColor="#333333" />
                                                    <%-- <SortedAscendingCellStyle BackColor="#F5F7FB" />
                                  <SortedAscendingHeaderStyle BackColor="#6D95E1" />
                                  <SortedDescendingCellStyle BackColor="#E9EBEF" />
                                  <SortedDescendingHeaderStyle BackColor="#4870BE" />--%>
                                                </asp:GridView>                       

                                        </div>
                                            </asp:Panel>
           </fieldset>

                            <table>
                            <tr>
                            <td>
                            <!--<asp:Label ID="lblMsg1" runat="server" ForeColor="#CC3300"></asp:Label>-->
                            <div id="dvPrint" visible="false">
                                <%=sPrint %>
                            </div>
                            </td>
                            </tr>
                            <tr>
                             <td>
                        <div id="dvchck">
                            <asp:CheckBox ID="chkSearch" style="text-align:center" onclick="Cash_Cheque();"  runat="server" Text="Search">
                            </asp:CheckBox>
                         <div>

 </div>

                        </div>

                        </td>
                        <td>
                        <asp:Button ID="btnSave" runat="server" OnClick="btnSave_Click" Text="Save" 
                                Height="25px" Width="75px" CssClass ="btn" />
                            <!--<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/images/btn.png"  />-->

                            <asp:Button ID="btnCancel" runat="server" OnClick="btnCancel_Click" 
                                Text="Cancel" Height="25px" CssClass="btn" Width="75px" />

                        </td>

                            </tr>
                            <tr style="display: none" id="trSearch" runat="server">
                        <td style="height: 40px" colspan="2">
                            <table style="width: 550px" border="0">
                                <tbody>
                                    <tr>
                                        <td>
                                            <strong>Code </strong>
                                        </td>
                                        <td>
                                            <asp:TextBox ID="txtSCode" runat="server" Width="75px" onkeypress="return isNumberKey(event)"
                                                OnTextChanged="txtSCode_TextChanged" autocomplete="off"></asp:TextBox>                                                <asp:Button ID="btnSearch" OnClick="btnSearch_Click" runat="server" Text="Search" Width="71px" CssClass="btn"></asp:Button>
                                        </td>
                                        <td style="font-weight: bold" valign="top">
                                        </td>
                                        <td style="font-weight: bold" valign="top" align="right">
                                            <asp:UpdatePanel ID="UpdatePanel2" runat="server">
                                                <ContentTemplate>
                                                    <asp:TextBox ID="txtSName" runat="server" autocomplete="off" Width="175px" AutoPostBack="True"
                                                        Visible="False"></asp:TextBox>
                                                </ContentTemplate>
                                            </asp:UpdatePanel>
                                        </td>
                                        <td style="font-weight: bold" valign="top" align="right">

                                        </td>
                                    </tr>
                                </tbody>
                            </table>
                        </td>
                    </tr>
                    <tr>
                        <td align="center" colspan="2">
                            <asp:Panel ID="pnlPo" runat="server" Height="400px" Width="750px" ScrollBars="Horizontal">
         <!--<div class="GridDock" id="dvGridWidth" style="overflow:scroll;border:1px;">-->
                                <asp:GridView ID="gvPO" runat="server" AutoGenerateColumns="False" CellPadding="4"
                                    ForeColor="#424242" GridLines="None" AllowPaging="True" 
  OnPageIndexChanging="gvPO_PageIndexChanging"
                                    OnRowDataBound="gvPO_RowDataBound" Width="1px">
                                    <AlternatingRowStyle BackColor="White" />
                                    <Columns>
                                        <asp:TemplateField HeaderText="Code">
                                            <ItemTemplate>
                                                <asp:HiddenField ID="hdnFrght" runat="server" Value='<%# Eval("mstfrghtper") %>' />
                                                <asp:HiddenField ID="hdnTin" runat="server" Value='<%# Eval("mstlotno") %>' />
                                                <asp:HiddenField ID="hdnVat" runat="server" Value='<%# Eval("mstsection") %>' />
                                                <asp:LinkButton ID="lnkCode" runat="server" Text='<%# Eval("mstcode") %>' OnClick="lnkCode_Click">code</asp:LinkButton>
                                                <asp:HiddenField ID="hdnParty" runat="server" Value='<%# Eval("mstptcode") %>' />
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <asp:BoundField DataField="mstdate" HeaderText="Date" DataFormatString="{0:dd-MM-yyyy}" />
                                        <asp:BoundField DataField="mstContactPerson" HeaderText="Consignee" />
                                        <asp:BoundField DataField="msttaxper" HeaderText="Tax" DataFormatString="{0:F}" />
                                        <asp:BoundField DataField="msttota" HeaderText="Total" DataFormatString="{0:F}" />
                                        <asp:BoundField DataField="mstdeliDes" HeaderText="delivery" />
                                        <asp:BoundField DataField="mstindno" HeaderText="Tin" />
                                        <asp:BoundField DataField="mstpodate" HeaderText="Del date" DataFormatString="{0:dd-MM-yyyy}" />
                                        <asp:BoundField DataField="mstpayDes" HeaderText="PayDes">
                                            <FooterStyle Wrap="False" />
                                            <HeaderStyle Wrap="False" />
                                            <ItemStyle Font-Bold="False" Font-Italic="False" Font-Underline="False" Wrap="False" />
                                        </asp:BoundField>
                                        <asp:BoundField DataField="mstrema" HeaderText="Remark">
                                            <ItemStyle Width="70px" Wrap="True" />
                                        </asp:BoundField>
                                        <asp:BoundField DataField="mstchno" HeaderText="PoRefNo" />
                                        <asp:TemplateField HeaderText="Print">
                                            <ItemTemplate>
                                                <%--<asp:LinkButton ID="LinkButton1" runat="server" onclick="lnkPrint_Click" OnClientClick="window.open ('Report.aspx','mywindow','width=350,height=250');">Print</asp:LinkButton>--%>
                                                <asp:LinkButton ID="lnkPrint" runat="server" OnClick="lnkPrint_Click">Print</asp:LinkButton>
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <asp:TemplateField HeaderText="Delete">
                                            <ItemTemplate>
                                                <%--<asp:LinkButton ID="LinkDelete" runat="server" OnClick="LinkDelete_Click">Delete</asp:LinkButton>--%>
                                                <asp:ImageButton ID="DeleteButton" runat="server" Width="20px" Height="20px" OnClientClick="if (!window.confirm('Are you sure you want to delete this item?')) return false;"
                                                    OnClick="LinkDelete_Click" ImageUrl="~/images/delete.png" CommandName="delete">
                                                </asp:ImageButton>
                                                <controlstyle width="20px" height="20px" />
                                                <itemstyle width="50px" />
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                    </Columns>
                                    <EditRowStyle BackColor="#2461BF" />
                                    <FooterStyle BackColor="#424242" Font-Bold="True" ForeColor="White" />
                                    <HeaderStyle BackColor="#424242" Font-Bold="True" ForeColor="White" />
                                    <PagerStyle BackColor="#424242" ForeColor="White" HorizontalAlign="Center" />
                                    <RowStyle BackColor="#EFF3FB" />
                                    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                                </asp:GridView>
                               <!-- </div>-->
                            </asp:Panel>
                            <%-- </ContentTemplate>
                  </asp:UpdatePanel>--%>
                        </td>
                    </tr>


                            </table>



       </ContentTemplate>

       </asp:UpdatePanel>
   </fieldset>
</div>

JQuery的

<script>
  $(document).ready(function() {
        SearchText();
        SearchItem();
    });
  function SearchText() {
  $('input[name$="tbAuto"]').autocomplete({
        source: function (request, response) {
            $.ajax({
                url: "PartyList.asmx/FetchPartyList",
                data: "{ 'prefix': '" + request.term + "' }",
                dataType: "json",
                type: "POST",
                contentType: "application/json; charset=utf-8",
                success: function (data) {
                    response(data.d);
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    alert(textStatus);
                }
            });
        },
        minLength: 1,
        focus: function(event, ui) {
            $('input[name$="tbAuto"]').val(ui.item.Name);
            return false;
        },
        select: function(event, ui) {
            $('input[name$="tbAuto"]').val(ui.item.Name);
            $('input[name$="tbHidden"]').val(ui.item.value);
            return false;
        }
    }).data('autocomplete')._renderItem = function(ul, item) {
        return $('<li>').data('item.autocomplete', item).append('<a>' + item.Name + '</a>').appendTo(ul);
    };
}


function SearchItem() {
  $('input[name$="txtitem"]').autocomplete({
            source: function (request, response) {
                $.ajax({
                    url: "Itemslist.asmx/FetchItemList",
                    data: "{ 'prefix': '" + request.term + "' }",
                    dataType: "json",
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    success: function (data) {
                        response(data.d);
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        alert(textStatus);
                    }
                });
            },
            minLength: 1,
            focus: function(event, ui) {
                $('input[name$="txtitem"]').val(ui.item.Name);
                return false;
            },
            select: function(event, ui) {
                $('input[name$="txtitem"]').val(ui.item.Name);
                $('input[name$="hditem"]').val(ui.item.value);
                return false;
            }
        }).data('autocomplete')._renderItem = function(ul, item) {
            return $('<li>').data('item1.autocomplete', item).append('<a>' + item.Name + '</a>').appendTo(ul);
        };
 }




</script>

1 个答案:

答案 0 :(得分:1)

当您使用UpdatePanel时,在UpdatePanel中的帖子之后,您需要重新初始化您的javascript,因为在重播时Dom已经更改并且之前的代码不再起作用。

所以在你的代码上添加:

$(document).ready(function() {
    var prm = Sys.WebForms.PageRequestManager.getInstance();    
    prm.add_initializeRequest(InitializeRequest);
    prm.add_endRequest(EndRequest);

    SearchText();
    SearchItem();
});

function InitializeRequest(sender, args) {      
}

function EndRequest(sender, args) {
    SearchText();
    SearchItem();
}