asp.net c# 中的输入框
我需要用 C# 在我的网站上创建一个简单的输入框。 当我在这样的代码中调用它时,它应该弹出,
String input = InputBox("Name the file");
然后我需要用户稍后在代码中输入的 use 字符串 在 .net 应用程序中,这很容易完成,但如何才能使其在 Web 应用程序中工作呢?我认为用ajax应该可以,但是对于这样一个(看似)微不足道的事情来说似乎相当复杂。 有没有任何类型的库或框架,我可以立即使用它?
提前致谢
I need to create a simple input box on my website in C#.
It should pop up, when i call it in the code like that
String input = InputBox("Name the file");
and then I need the use string the users enters later in the code
In a .net application, it is pretty easy to accomplish, but how can i make it work in a web application? I think it should be possible with ajax, but it seems pretty complicated for such a (seemingly) trivial thing.
Is there any kind of library or framework, that I can use for this right away?
thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在我看来,您正在寻找的行为是获得一个带有文本框的弹出窗口,供用户输入值并单击“确定”。是这样吗?
你说得对,网络应用程序更复杂。在 Windows 应用程序中,每当您运行 C# 代码时,无论发生什么,都会在那一刻发生。这非常简单。然而,在 Web 应用程序中,所有 C# 甚至在页面呈现在浏览器中之前就运行。因此,Web 表单中的 C# 无法真正弹出窗口。
为了获得弹出窗口,您需要使用 JavaScript 来完成此操作。弹出窗口内的文本框应该是
控件。如果您最熟悉 .NET 控件,则可以使用 Ajax 控件工具包。如果您熟悉 jQuery,您应该查看 jQuery UI。It sounds to me like the behavior you are looking for is to get a popup window with a text box for the user to enter a value and click ok. Is that right?
You're right in saying that it is more complicated with a web app. In a windows app, whenever you run the C# code, whatever happens, happens at that moment. It's pretty straightforward. However, in a web app, all of the C# runs before the page is even rendered in the browser. Therefore, C# in web forms can't really pop up a window.
In order to get a popup, you'll need to do that with JavaScript. The textbox inside the popup should be an
<asp:Textbox>
control. You can use the Ajax Control Toolkit if youre most comfortable with .NET controls. If youre comforatble with jQuery, you should check out jQuery UI.我假设您使用网络表单。
最简单的事情就是制作一个只有一个输入框的网络表单(
)。添加带有 onclick 事件的按钮 (
。在 onclick 事件处理程序中,您可以对值执行一些操作。I'm assuming you use webforms.
The most simple thing is to make a webform with one input box (
<asp:textbox runat="server" id="inputfield" />
). Add a button with an onclick event (<asp:button runat="server" id="button" onclick="OnClick" />
. In the onclick eventhandler you do something with the value.这到底是什么时候?请记住,Web 开发与应用程序开发的断开连接性质之间存在根本区别。在网页在浏览器中呈现之前,所有服务器端 C# 代码都已完成执行。那么什么时候调用这段代码呢?另外,您将如何将数据传回服务器?表单帖子? AJAX 调用?
如果您希望它“弹出”并使用 AJAX 回发,我推荐 jQuery UI 对话框作为实际的弹出窗口。然后,在其
close
事件上,您可以对服务器进行 AJAX 调用来发布数据。And when exactly is this? Keep in mind that there's a fundamental difference between the disconnected nature of web development vs. application development. All of your server-side C# code has already finished executing before the web page renders in the browser. So when are you going to call this code? Also, how are you going to pass the data back to the server? A form post? An AJAX call?
If you want it to "pop up" and to post back with AJAX, I recommend the jQuery UI Dialog as the actual pop-up. Then on its
close
event you can make the AJAX call to the server to post the data.如果您正在寻找一个简单的 POPUP 解决方案来获取用户输入,我建议您查看 JQuery 的对话框小部件。特别是模式形式,这里是一些更多信息的链接: http://jqueryui.com /demos/dialog/#modal-form
If you are looking for a simple solution for a POPUP that will get user input, I recomend checking out JQuery's dialog widget. In particular the modal form, here is a link to some more information: http://jqueryui.com/demos/dialog/#modal-form
ASP.NET 有一个 TextBox 控件就是这样做的。所有带有 runat="server" 的项目都可以通过服务器端代码访问。
ASP.NET has a TextBox control that does just this. All items with runat="server" are accessible through server-side code.