如何在回发后停止丢失值?

时间:2016-10-11 13:55:20

标签: javascript c# jquery asp.net gridview

我有一个带文本框的GridView,用户可以在文本框中输入信息,它将根据文本框中的值在网格上创建行。但是将行添加到网格的按钮导致回发,因此我丢失了后面代码中的所有值。

网格和文本框代码:

<div style="padding-top:20px">
                <fieldset class="groupbox_nohide" >    
                    <div style="width:100%">
                        <asp:UpdatePanel ID="udpCosts" UpdateMode="Conditional" runat="server">
                            <ContentTemplate>
                                <asp:GridView  style="width:75%;float:left"  
                                    ID="gvCosts" 
                                    ShowHeaderWhenEmpty="true"
                                    CssClass="tblResults" 
                                    runat="server" 
                                    OnRowDataBound="gvCosts_RowDataBound"                             
                                    DataKeyField="ID" 
                                    AutoGenerateColumns="false"
                                    allowpaging="false"
                                    AlternatingRowStyle-BackColor="#EEEEEE">
                                    <HeaderStyle CssClass="tblResultsHeader" />
                                    <Columns>       
                                        <asp:BoundField DataField="CreditorCode" HeaderText="Creditor Code"></asp:BoundField>
                                    </Columns>
                                </asp:GridView>
                            </ContentTemplate>
                            <Triggers>
                                <asp:AsyncPostBackTrigger ControlID="btnCreateCost" />
                                <asp:AsyncPostBackTrigger ControlID="btnUpdateCost" /> 
                            </Triggers>
                        </asp:UpdatePanel> 
                        <div id="divAddEditPiece" runat="server">      
                <table>
                    <tr class="tblResults">
                        <td class="header" style="border-right:2px">
                            Creditor A/C<label style="color:Red"></label>
                        </td>
                    </tr>
                    <tr class="tblResults">

                        <td class="tblAddDetail" style="border-right:2px">
                            <a style="float:left;width:16px;height:16px;margin-right:0px;left:0px;top:1px" title="Pick from list..." class="iconSearch" id="btnAddDetailCreditor"></a>
                            <asp:HiddenField ID="hfCreditorID" runat="server"  />
                            <input type="text" id="txtAddEditCreditorCode" class="lookuppopup" style="text-transform:uppercase;width:80px" runat="server"/>
                        </td>
                        <td class="tblAddDetail" colspan="15">
                            <asp:LinkButton ID="btnCreateCost" OnClientClick="tinyMCE.triggerSave(false, true);" runat="server" style="float:left;width:25px" CssClass="btnNewSmall">Add</asp:LinkButton>
                        </td>
                    </tr>     
                </table>
            </div> 

然后在javascript代码中,hiddenfield和textbox设置其值:

$('#<%= hfCreditorID.ClientID %>').val(result.ID);
$('#<%= txtAddEditCreditorCode.ClientID %>').val(result.AccountCode);

但是一旦我点击创建按钮,值就会被设置为空白,因此隐藏字段和文本框后面的代码始终为空白:

protected override void OnInit(EventArgs e)
{
     btnCreateCost.Click += new EventHandler(btnCreateCost_Click);
}
protected void btnCreateCost_Click(object sender, EventArgs e)
    {
        int  tmpCredID;

        bool valid = true;
        string ErrMsg = "Error: The following fields cannot be empty:";

        if (!int.TryParse(hfCreditorID.Value, out tmpCredID))
        {
            valid = false;
            ErrMsg += "<br />Creditor Code";
        }
}

如何将这些值传递给后面的代码?

1 个答案:

答案 0 :(得分:0)

在jquery中为btnCreateCost添加一个click事件。然后你可以使用ajax帖子来完成你的工作。

$('#btnCreateCost').click(function(e){
    e.preventDefault();
     $.ajax({
        type: "Post",
        url: "yourwebmethodurl",
        data: "{'parametername':'" + parametervalue + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {
            alert(mes);
        },
        error: function (err) {
            alert(err);
        }
    })
});

以下是示例来自的页面。我拿了两个帖子把它放在一起。 https://forums.asp.net/t/2047043.aspx?Stop+Page+Post+Back+using+Javascript+jQuery