我怎样才能更好地控制在ASP.NET? [英] How can I take more control in ASP.NET?

查看:108
本文介绍了我怎样才能更好地控制在ASP.NET?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要建立一个非常,非常简单微web应用,我怀疑会感兴趣的几个堆栈Overflow'rs如果我把它做。我在我的C#在深入现场,这是香草ASP.NET 3.5收留了它(即不MVC)。

I'm trying to build a very, very simple "micro-webapp" which I suspect will be of interest to a few Stack Overflow'rs if I ever get it done. I'm hosting it on my C# in Depth site, which is vanilla ASP.NET 3.5 (i.e. not MVC).

流是非常简单的:

  • 如果用户输入与不指定所有参数的URL应用程序(或任何人都无效)我想只显示用户输入控件。 (只有两个。)
  • 如果用户输入与的的URL的应用程序做的拥有所有必需的参数,我想显示结果的的输入控件(这样他们就可以改变参数)
  • If a user enters the app with a URL which doesn't specify all the parameters (or if any of them are invalid) I want to just display the user input controls. (There are only two.)
  • If a user enters the app with a URL which does have all the required parameters, I want to display the results and the input controls (so they can change the parameters)

下面是我的自我强加的要求(设计和实施的混合物):

Here are my self-imposed requirements (mixture of design and implementation):

  • 我要提交使用GET而不是POST,主要是让用户可以轻松添加了书签。
  • 在我的的希望的URL落得看傻了提交后,有多余的点点滴滴就可以了。只是主URL和真实参数吧。
  • 在理想情况下,我想避免的JavaScript要求在所有。有在此应用程序没有很好的理由。
  • 我希望能够访问控制期间呈现时间和设定值等。特别地,我希望能够以设置控制的默认值传入的参数值,如果ASP.NET不能自动为我做到这一点(在其他的限制之内)。
  • 我很高兴能完成所有的参数验证自己,我不需要太多的服务器端事件的方式。这是非常简单的设置连接事件,按钮等一切在页面加载代替。
  • I want the submission to use GET rather than POST, mostly so users can bookmark the page easily.
  • I don't want the URL to end up looking silly after submission, with extraneous bits and pieces on it. Just the main URL and the real parameters please.
  • Ideally I'd like to avoid requiring JavaScript at all. There's no good reason for it in this app.
  • I want to be able to access the controls during render time and set values etc. In particular, I want to be able to set the default values of the controls to the parameter values passed in, if ASP.NET can't do this automatically for me (within the other restrictions).
  • I'm happy to do all the parameter validation myself, and I don't need much in the way of server side events. It's really simple to set everything on page load instead of attaching events to buttons etc.

这其中大部分是好的,但我还没有发现任何方式的完全的删除视图状态和保持的有用的功能的其余部分。使用后从这个博客帖子我一直设法避免让任何实际的的的视图状态 - 但它最终还是因为在URL中的参数,它看起来真的很丑陋

Most of this is okay, but I haven't found any way of completely removing the viewstate and keeping the rest of the useful functionality. Using the post from this blog post I've managed to avoid getting any actual value for the viewstate - but it still ends up as a parameter on the URL, which looks really ugly.

如果我让一个普通的HTML表单,而不是一个ASP.NET的形式(即取出 =服务器),那么我没有得到任何神奇的视图状态 - 但我不能访问控制编程

If I make it a plain HTML form instead of an ASP.NET form (i.e. take out runat="server") then I don't get any magic viewstate - but then I can't access the controls programmatically.

我的可以的做到这一切通过忽略大多数ASP.NET,构建了一个XML文档的LINQ to XML,贯彻的IHttpHandler 。这种感觉有点低的水平,但。

I could do all of this by ignoring most of ASP.NET and building up an XML document with LINQ to XML, and implementing IHttpHandler. That feels a bit low level though.

我意识到,我的问题可以通过任何放松我的约束来解决(例如,使用POST和不关心剩余参数)或通过使用ASP.NET MVC,但我的要求真的很不合理?

I realise that my problems could be solved by either relaxing my constraints (e.g. using POST and not caring about the surplus parameter) or by using ASP.NET MVC, but are my requirements really unreasonable?

也许ASP.NET只是没有规模的的这一类的应用程序吗?有一个很可能的替代,虽然:我只是愚蠢,而且也这样做,我只是还没有找到一个非常简单的方法

Maybe ASP.NET just doesn't scale down to this sort of app? There's a very likely alternative though: I'm just being stupid, and there's a perfectly simple way of doing it that I just haven't found.

任何想法,任何人吗? (如何有能力的人堕落的线索,评论等那很好 - 我希望我从来没有声称自己是一个ASP.NET专家,因为事实是恰恰相反......)

Any thoughts, anyone? (Cue comments of how the mighty are fallen, etc. That's fine - I hope I've never claimed to be an ASP.NET expert, as the truth is quite the opposite...)

推荐答案

这个解决方案会给你编程访问的全部的控制,包括对控件的所有属性。此外,只有文本框中的值将出现在在提交网址,以便您的GET请求的URL会更有意义的

This solution will give you programmatic access to the controls in their entirety including all attributes on the controls. Also, only the text box values will appear in the URL upon submission so your GET request URL will be more "meaningful"

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="JonSkeetForm.aspx.cs" Inherits="JonSkeetForm" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Jon Skeet's Form Page</title>
</head>
<body>
    <form action="JonSkeetForm.aspx" method="get">
    <div>
        <input type="text" ID="text1" runat="server" />
        <input type="text" ID="text2" runat="server" />
        <button type="submit">Submit</button>
        <asp:Repeater ID="Repeater1" runat="server">
            <ItemTemplate>
                <div>Some text</div>
            </ItemTemplate>
        </asp:Repeater>
    </div>
    </form>
</body>
</html>

然后在你的code-背后,你可以做你需要的一切pageLoad的

Then in your code-behind you can do everything you need on PageLoad

public partial class JonSkeetForm : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        text1.Value = Request.QueryString[text1.ClientID];
        text2.Value = Request.QueryString[text2.ClientID];
    }
}

如果你不希望有 =服务器的一种形式,那么你应该使用HTML控件。它更容易与您的目的的工作。只需使用普通的HTML标签,并把 =服务器,给他们一个ID。然后你就可以访问它们编程 $ C $无的ViewState ℃。

If you don't want a form that has runat="server", then you should use HTML controls. It's easier to work with for your purposes. Just use regular HTML tags and put runat="server" and give them an ID. Then you can access them programmatically and code without a ViewState.

唯一的缺点是,你不会有机会获得不少有帮助的ASP.NET服务器控件像的GridView 秒。我加入了一个转发在我的例子,因为我假设你想拥有相同的页面结果上的字段和(据我所知)一个转发是唯一的数据绑定控件将运行没有在表格标签的 =服务器属性。

The only downside is that you won't have access to many of the "helpful" ASP.NET server controls like GridViews. I included a Repeater in my example because I'm assuming that you want to have the fields on the same page as the results and (to my knowledge) a Repeater is the only DataBound control that will run without a runat="server" attribute in the Form tag.

这篇关于我怎样才能更好地控制在ASP.NET?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆