简体   繁体   English

成功使用Querystring和ASP.NET AJAX

[英]Using Querystring and ASP.NET AJAX successfully

Good day to all of you. 祝大家好。

I am using a FormView in ASP.NET application (VS 2010 SP1), and this FormView incorporates two functions: Insert and Update. 我在ASP.NET应用程序(VS 2010 SP1)中使用FormView,并且此FormView包含两个功能:插入和更新。 The FormView uses only 1 template: EditItemTemplate in order to achieve the aforementioned functionalities. FormView仅使用1个模板:EditItemTemplate来实现上述功能。

When Inserting a new record, the generated ID is returned by the ObjectDataSource, and is used as a querystring to be appended to the current URL, and then is used by the datasource to select the newly inserted record for editing. 插入新记录时,ObjectDataSource返回生成的ID,并将其用作要附加到当前URL的查询字符串,然后由数据源用于选择新插入的记录以进行编辑。 The FormView is surrounded by an update panel to utilize AJAX technology. FormView周围有一个更新面板,以利用AJAX技术。

The ASPX code is: ASPX代码为:

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
    <asp:FormView ID="fvw_MoFA_NGOs_List" runat="server" DataKeyNames="id"
        DataSourceID="ods_MoFA_List" OnInit="fvw_MoFA_NGOs_List_Init" 
        OnItemCommand="fvw_MoFA_NGOs_List_ItemCommand" DefaultMode="Edit"
        Width="98%">
        <EditItemTemplate>
            <table style="width: 50%;">
                <tr>
                    <td>
                        id:
                    </td>
                    <td>
                        <asp:Label ID="idLabel1" runat="server" Text='<%# Eval("id") %>' />
                    </td>
                </tr>
                <tr>
                    <td>
                        Arabic Name:
                    </td>
                    <td>
                        <asp:TextBox ID="Ar_NameTextBox" runat="server" Text='<%# Bind("Ar_Name") %>' />                                        
                    </td>
                    <td>
                        <asp:RequiredFieldValidator ID="vld_ArNm" ControlToValidate="Ar_NameTextBox" runat="server" EnableClientScript="true" ErrorMessage="Arabic name is required">*</asp:RequiredFieldValidator>
                    </td>
                </tr>
                <tr>
                    <td>
                        English Name:
                    </td>
                    <td>
                        <asp:TextBox ID="En_NameTextBox" runat="server" Text='<%# Bind("En_Name") %>' />
                    </td>
                    <td>
                        <asp:RequiredFieldValidator ID="vld_EnNm" ControlToValidate="En_NameTextBox" runat="server" EnableClientScript="true" ErrorMessage="English name is required">*</asp:RequiredFieldValidator>
                    </td>
                </tr>
                <tr>
                    <td>
                        Governorate of Headquarters:
                    </td>
                    <td>
                        <asp:DropDownList ID="DropDownList1" runat="server" SelectedValue='<%# Bind("Hq_Governorate") %>'
                            DataSourceID="ods_Governorates" DataTextField="Mohafaza_EN" DataValueField="Mohafaza_pcode_PK">
                        </asp:DropDownList>
                    </td>
                </tr>

                <tr>
                <br />
                </tr>

                <tr>
                    <td>
                        <asp:Button ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert"
                            Text="Insert" />
                    </td>
                    <td>
                        <asp:Button ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update"
                            Text="Update" />
                    </td>
                </tr>
                <tr>
                    <td colspan='2'>

                    </td>
                </tr>
                <tr>
                    <td colspan='2'>
                        <asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
                            Text="Cancel" Visible="false" />
                    </td>
                </tr>
            </table>
        </EditItemTemplate>
    </asp:FormView>
</ContentTemplate>

The associated ObjectDataSource is (note the SelectParameters ): 关联的ObjectDataSource是(请注意SelectParameters ):

<asp:ObjectDataSource ID="ods_MoFA_List" runat="server" DeleteMethod="Delete" InsertMethod="Insert_Get_NwId"
        OldValuesParameterFormatString="original_{0}" OnInserted="ods_MoFA_List_Inserted"
        SelectMethod="GetDataById" TypeName="NGOs_IIMS.NGOs_Module_IIMS.DAL.NGOs_IIMSTableAdapters.NGOs_MoFA_NGOs_ListsTableAdapter"
        UpdateMethod="Update" OnInserting="ods_MoFA_List_Inserting" OnUpdating="ods_MoFA_List_Updating">
        <DeleteParameters>
            <asp:Parameter Name="Original_id" Type="Int32" />
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="Ar_Name" Type="String" />
            <asp:Parameter Name="En_Name" Type="String" />
            <asp:Parameter Name="Rdate" Type="DateTime" />
            <asp:Parameter Name="Creator" Type="Int32" />
            <asp:Parameter Name="Hq_Governorate" Type="String" />
        </InsertParameters>
        <SelectParameters>
            <asp:QueryStringParameter DefaultValue="-1" Name="pid" QueryStringField="MoFAid"
                Type="Int32" />
        </SelectParameters>
        <UpdateParameters>
            <asp:Parameter Name="Ar_Name" Type="String" />
            <asp:Parameter Name="En_Name" Type="String" />
            <asp:Parameter Name="Rdate" Type="DateTime" />
            <asp:Parameter Name="Creator" Type="Int32" />
            <asp:Parameter Name="Hq_Governorate" Type="String" />
            <asp:Parameter Name="Original_id" Type="Int32" />
        </UpdateParameters>
    </asp:ObjectDataSource>

And the querystring appending is done when the new record is inserted: 当插入新记录时,将完成querystring附加操作:

protected void ods_MoFA_List_Inserted(object sender, ObjectDataSourceStatusEventArgs e)
{
    if (e.ReturnValue != null)
        Response.Redirect(Request.Url.AbsolutePath + "?" + New_MoFA_NGO + "=" + e.ReturnValue.ToString());   
}

The problem is: 问题是:

When inserting, the whole page is refreshed despite AJAX, but when updating the inserted record AJAX works correctly. 插入时,尽管使用AJAX,也会刷新整个页面,但是在更新插入的记录时,AJAX可以正常工作。

Is there something wrong in using the querystring with AJAX? 将querystring与AJAX一起使用会出现问题吗? should I depend on the Session variables here even though they are not the best choice? 即使不是最佳选择,我仍应依赖此处的Session变量吗?

    <asp:ScriptManager ID="ScriptManager1" runat="server" />
    <asp:UpdatePanel runat="server" ID="UpdatePanel1" UpdateMode="Conditional">
      <ContentTemplate>
      //Your Html code 
      </ContentTemplate>
    </asp:UpdatePanel>

This will prevent refreshing the page. 这将阻止刷新页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM