简体   繁体   English

我应该使用C#Event / Postback还是Javascript做简单的事情?

[英]Should I use a C# Event / Postback, or Javascript for something simple?

For the sake of argument, I have a DropDownList, with a variable number of items in. Let's say they come from an ObjectDataSource. 为了论证,我有一个DropDownList,其中包含可变数量的项。假设它们来自ObjectDataSource。 I also have a TextBox, which I want to contain that value. 我还有一个TextBox,我想包含该值。 Bear with me here. 在这里忍受我。

EG 例如

<asp:DropDownList runat="server" ID="ddlOptions">
    <asp:ListItem Text="Option 1" Value="Value1"></asp:ListItem>
    <asp:ListItem Text="Option 2" Value="Value2"></asp:ListItem>
    <asp:ListItem Text="Option 3" Value="Value3"></asp:ListItem>
</asp:DropDownList>

<asp:TextBox runat="server" ID="txtOptionValue"></asp:TextBox>

My question is - should I use a Postback to set the value, or some Javascript? 我的问题是-我应该使用Postback来设置值还是使用Javascript? I can either use the SelectedIndexChanged event of the DropDownList, or I can hook it up using some jQuery .change(func...) magic. 我可以使用DropDownList的SelectedIndexChanged事件,也可以使用一些jQuery .change(func ...)魔术将其连接起来。

I'm not asking how to do this, but which would be the preferred method ? 我不是在问如何做到这一点,但哪种方法首选 Personally, I think using jQuery looks cleaner to the end-user, but using a postback is more of a 'standard' way of doing things? 就个人而言,我认为使用jQuery对最终用户来说看起来更干净,但是使用回发更是一种“标准”的处理方式? Is there anything I'm missing? 有什么我想念的吗?

JAVASCRIPT is meant to be used whatever you can do on client side. JAVASCRIPT可以在客户端执行任何操作。

The thing that you are doing is clearly possible to be done using jQuery so there is no need to hit the server just to set the value. 您正在做的事情很明显可以使用jQuery完成,因此无需点击服务器即可设置值。

Try to reduce server hit as many as possible. 尝试尽可能减少服务器命中率。 This should be the motto of developers 这应该是开发人员的座右铭

是的,您是对的,jQuery是最好的选择,但是这完全取决于您的要求,如果页面没有任何回发,那么您可以选择JQuery,否则最好选择回发。

You should probably think about the overall architecture and user experience requirements. 您可能应该考虑整体架构和用户体验要求。

Lets say your clients want a smooth experience, something like a single page app. 假设您的客户希望获得流畅的体验,例如单页应用程序。 Then yes, start throwing in all the javascript and jquery you can to achieve as this, with something like WebAPI instead of asp.net. 然后,是的,开始使用WebAPI之类的东西而不是asp.net抛出所有可以实现的javascript和jquery。 Check out ASP.NET SPA . 查看ASP.NET SPA

Lets say your client isn't requiring anything fancy. 可以说您的客户不需要任何花哨的东西。 Why go through all the trouble of adding javascript when you can quickly achieve it by using standard asp.net callbacks. 当您可以使用标准的asp.net回调快速实现JavaScript时,为什么还要麻烦添加JavaScript。

It looks like you are using ASP.net. 看来您正在使用ASP.net。 I would stick with normal out-of-the-box asp.net unless the requirements say otherwise. 除非需求另有说明,否则我会坚持使用现成的常规asp.net。 No sense in throwing in all these other libraries or dependencies if if this is the only place you'll use it. 如果这是您将要使用的唯一库,则没有其他所有其他库或依赖项。

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

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