7/23 Ajax .NET save content automatically and redirect to same page without new tabs

这两天改Form,比较有意思的是从DB里automatic fill-up,我凭借GUID等Key fields把信息很容易从staffbanner Table中取出来。

接着就是Email Footer有个图片,这个图片如果镶嵌在project 下的img folder里,要求收件人pump out输入credential,我看了一下因为src是本地的相关地址,所以就建议改为open access的地方。在测试的过程中,需要不断的打开supervisor approval的page,就需要看代码,看读什么field,然后去DB找那一栏,复制出来然后进入。

另外要注意的地方,我差点犯了大错,就是被hidden的fields,我不能只把style = display:none就不管了,还要把相关的requiredvalidator disable掉,否则move到production用户永远的不能proceed。幸亏我偶尔测试了一下,否则直接GG,险些犯下大错。

后来就是wendy冒出来个要求说想点开的那个faculty account request page在同一个tab里,于是我就搜了搜,把代码由:

<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/naDefault.aspx?positiontype=faculty"
								Target="_self">
中的Target 属性,_blank是新的tab, _self顾名思义是自己。(Reference: 点击打开链接 )

完成任务。

最后再记个有趣的帖子吧,昨天看Ajax看的,今天突然想,申请学校和工作的那些save button,是不是类似的技术,就搜了搜,下面是结果。

Reference: http://www.codeproject.com/Articles/34281/Easy-Automatically-Save-Form-with-NET-and-AJAX

add a ScriptManager, Timer and an UpdatePanel from AJAX Extensions tab.

<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:Timer ID="Timer1" runat="server" Interval="15000" ontick="Timer1_Tick"></asp:Timer> 

<div> 

    <asp:TextBox ID="TextBox1" runat="server" 
	Height="118px" TextMode="MultiLine" Width="468px"></asp:TextBox>
    <asp:Button ID="Button1" runat="server" Text="Save" 
	οnclick="Button1_Click" /><br /><br />

<asp:UpdatePanel ID="UpdatePanel1" runat="server">

    <ContentTemplate>
        <asp:TextBox ID="TextBox2" runat="server" Height="120px" 
		TextMode="MultiLine" Width="466px"></asp:TextBox>
    </ContentTemplate>

    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick"/>
    </Triggers>

</asp:UpdatePanel> 

</div>

</form> 


在UpdatePanel里,有trigger,用Timer trigger,通过用contentID调到上边的timer里,在timer里有ontick的call-back处理函数,这里就可以把内容存到DB。

Sure, here's a simple demo on how to automatically add "@using ThatOtherNamespace" to a Blazor page: 1. In your Blazor project, navigate to the "wwwroot" folder and create a new folder called "utilities". 2. In the "utilities" folder, create a new file called "AutoAddNamespace.cs". 3. In the "AutoAddNamespace.cs" file, add the following code: ``` using System.Threading.Tasks; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Rendering; namespace YourProjectNamespace.Utilities { public class AutoAddNamespace : ComponentBase { protected override async Task OnAfterRenderAsync(bool firstRender) { if (firstRender) { await JSRuntime.InvokeVoidAsync("autoAddNamespace"); } } [JSInvokable] public static void AddNamespace() { // Get the current page's markup var markup = RenderFragmentToString(builder => { builder.OpenComponent<AutoAddNamespace>(0); builder.CloseComponent(); }); // Check if the namespace is already added if (!markup.Contains("@using ThatOtherNamespace")) { // Add the namespace to the markup markup = markup.Replace("@page", "@using ThatOtherNamespace\n@page"); // Refresh the page with the updated markup JSRuntime.InvokeVoidAsync("updatePageMarkup", markup); } } private static string RenderFragmentToString(RenderFragment renderFragment) { var result = new StringBuilder(); var renderer = new HtmlRenderer(new RendererContext(new HtmlEncoder())) { Builder = new RenderTreeBuilder(new HtmlRenderer(new RendererContext(new HtmlEncoder()))), NewLine = "\n" }; renderer.Render(renderFragment); renderer.Builder.Flush(); result.Append(renderer.Builder.ToString()); return result.ToString(); } } } ``` 4. In your Blazor page, add the following code at the top: ``` @inject IJSRuntime JSRuntime @using YourProjectNamespace.Utilities ``` 5. At the bottom of your Blazor page, add the following code: ``` @code { protected override void OnInitialized() { JSRuntime.InvokeVoidAsync("registerAddNamespaceMethod"); } } ``` 6. In the "wwwroot/index.html" file, add the following code inside the "head" tag: ``` <script> function autoAddNamespace() { DotNet.invokeMethodAsync("YourProjectNamespace", "AddNamespace"); } function updatePageMarkup(markup) { document.documentElement.innerHTML = markup; } function registerAddNamespaceMethod() { DotNet.registerMethod("YourProjectNamespace", "AddNamespace", AutoAddNamespace.prototype.AddNamespace); } </script> ``` That's it! Now, every time the Blazor page is loaded, the "AutoAddNamespace" component will automatically check if "@using ThatOtherNamespace" is already added to the page markup. If not, it will add it and refresh the page with the updated markup.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值