asp.net c# 中的输入框

发布于 2024-11-06 10:30:22 字数 275 浏览 3 评论 0原文

我需要用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

万劫不复 2024-11-13 10:30:22

在我看来,您正在寻找的行为是获得一个带有文本框的弹出窗口,供用户输入值并单击“确定”。是这样吗?

你说得对,网络应用程序更复杂。在 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.

少女情怀诗 2024-11-13 10:30:22

我假设您使用网络表单。

最简单的事情就是制作一个只有一个输入框的网络表单()。添加带有 onclick 事件的按钮 (。在 onclick 事件处理程序中,您可以对值执行一些操作。

protected void OnClick(object sender, EventArgs args){
  string input = inputfield.Text;
  // do something
}

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.

protected void OnClick(object sender, EventArgs args){
  string input = inputfield.Text;
  // do something
}
赴月观长安 2024-11-13 10:30:22

当我在这样的代码中调用它时,它应该弹出

这到底是什么时候?请记住,Web 开发与应用程序开发的断开连接性质之间存在根本区别。在网页在浏览器中呈现之前,所有服务器端 C# 代码都已完成执行。那么什么时候调用这段代码呢?另外,您将如何将数据传回服务器?表单帖子? AJAX 调用?

如果您希望它“弹出”并使用 AJAX 回发,我推荐 jQuery UI 对话框作为实际的弹出窗口。然后,在其 close 事件上,您可以对服务器进行 AJAX 调用来发布数据。

It should pop up, when i call it in the code like that

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.

庆幸我还是我 2024-11-13 10:30:22

如果您正在寻找一个简单的 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

毅然前行 2024-11-13 10:30:22

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.

妄断弥空 2024-11-13 10:30:22
  public class InputBox
        {
            public static DialogResult Show(string title, string promptText, ref string value)
            {
                return Show(title, promptText, ref value, null);
            }




//Fuction


            public static DialogResult Show(string title, string promptText, ref string value,
                                            InputBoxValidation validation)
            {
                Form form = new Form();
                Label label = new Label();
                TextBox textBox = new TextBox();
                Button buttonOk = new Button();
                Button buttonCancel = new Button();

                form.Text = title;
                label.Text = promptText;
                textBox.Text = value;

                buttonOk.Text = "OK";
                buttonCancel.Text = "Cancel";
                buttonOk.DialogResult = DialogResult.OK;
                buttonCancel.DialogResult = DialogResult.Cancel;

                label.SetBounds(9, 20, 372, 13);
                textBox.SetBounds(12, 36, 372, 20);
                buttonOk.SetBounds(228, 72, 75, 23);
                buttonCancel.SetBounds(309, 72, 75, 23);

                label.AutoSize = true;
                textBox.Anchor = textBox.Anchor | AnchorStyles.Right;
                buttonOk.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
                buttonCancel.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;

                form.ClientSize = new Size(396, 107);
                form.Controls.AddRange(new Control[] { label, textBox, buttonOk, buttonCancel });
                form.ClientSize = new Size(Math.Max(300, label.Right + 10), form.ClientSize.Height);
                form.FormBorderStyle = FormBorderStyle.FixedDialog;
                form.StartPosition = FormStartPosition.CenterScreen;
                form.MinimizeBox = false;
                form.MaximizeBox = false;
                form.AcceptButton = buttonOk;
                form.CancelButton = buttonCancel;
                if (validation != null)
                {
                    form.FormClosing += delegate(object sender, FormClosingEventArgs e)
                    {
                        if (form.DialogResult == DialogResult.OK)
                        {
                            string errorText = validation(textBox.Text);
                            if (e.Cancel = (errorText != ""))
                            {
                                MessageBox.Show(form, errorText, "Validation Error",
                                                MessageBoxButtons.OK, MessageBoxIcon.Error);
                                textBox.Focus();
                            }
                        }
                    };
                }
                DialogResult dialogResult = form.ShowDialog();
                value = textBox.Text;
                return dialogResult;
            }
        }
        public delegate string InputBoxValidation(string errorMessage);





















private void button_updations_Click(object sender, EventArgs e)
        {

            InputBoxValidation validation = delegate(string val)
            {
                if (val == "")
                    return "Value cannot be empty.";
                if (!(new Regex(@"^[a-zA-Z0-9_\-\.]+@[a-zA-Z0-9_\-\.]+\.[a-zA-Z]{2,}$")).IsMatch(val))
                    return "Email address is not valid.";
                return "";
            };

            string value = "";
            if (InputBox.Show("Enter your email address", "Email address:", ref value, validation) == DialogResult.OK)
            {

                if (value == "[email protected]")
                {
                    dataGridView1.Visible = true;
                    button_delete.Visible = true;
                    button1.Visible = true;
                    button_show.Visible = true;
                    label6.Visible = true;
                    label4.Visible = true;
                    label5.Visible = true;
                    textBox_uemail.Visible = true;
                    textBox_uname.Visible = true;
                    textBox_upassword.Visible = true;
                    textBox_delete.Visible = true;
                    button_deleteTable.Visible = true;

                    button_updatep.Visible = true;
                    textBox_updateall.Visible = true;
                }
                MessageBox.Show(value);
            }
            else
            {
                MessageBox.Show("You are not authenticated");





            }
        }
  public class InputBox
        {
            public static DialogResult Show(string title, string promptText, ref string value)
            {
                return Show(title, promptText, ref value, null);
            }




//Fuction


            public static DialogResult Show(string title, string promptText, ref string value,
                                            InputBoxValidation validation)
            {
                Form form = new Form();
                Label label = new Label();
                TextBox textBox = new TextBox();
                Button buttonOk = new Button();
                Button buttonCancel = new Button();

                form.Text = title;
                label.Text = promptText;
                textBox.Text = value;

                buttonOk.Text = "OK";
                buttonCancel.Text = "Cancel";
                buttonOk.DialogResult = DialogResult.OK;
                buttonCancel.DialogResult = DialogResult.Cancel;

                label.SetBounds(9, 20, 372, 13);
                textBox.SetBounds(12, 36, 372, 20);
                buttonOk.SetBounds(228, 72, 75, 23);
                buttonCancel.SetBounds(309, 72, 75, 23);

                label.AutoSize = true;
                textBox.Anchor = textBox.Anchor | AnchorStyles.Right;
                buttonOk.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
                buttonCancel.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;

                form.ClientSize = new Size(396, 107);
                form.Controls.AddRange(new Control[] { label, textBox, buttonOk, buttonCancel });
                form.ClientSize = new Size(Math.Max(300, label.Right + 10), form.ClientSize.Height);
                form.FormBorderStyle = FormBorderStyle.FixedDialog;
                form.StartPosition = FormStartPosition.CenterScreen;
                form.MinimizeBox = false;
                form.MaximizeBox = false;
                form.AcceptButton = buttonOk;
                form.CancelButton = buttonCancel;
                if (validation != null)
                {
                    form.FormClosing += delegate(object sender, FormClosingEventArgs e)
                    {
                        if (form.DialogResult == DialogResult.OK)
                        {
                            string errorText = validation(textBox.Text);
                            if (e.Cancel = (errorText != ""))
                            {
                                MessageBox.Show(form, errorText, "Validation Error",
                                                MessageBoxButtons.OK, MessageBoxIcon.Error);
                                textBox.Focus();
                            }
                        }
                    };
                }
                DialogResult dialogResult = form.ShowDialog();
                value = textBox.Text;
                return dialogResult;
            }
        }
        public delegate string InputBoxValidation(string errorMessage);





















private void button_updations_Click(object sender, EventArgs e)
        {

            InputBoxValidation validation = delegate(string val)
            {
                if (val == "")
                    return "Value cannot be empty.";
                if (!(new Regex(@"^[a-zA-Z0-9_\-\.]+@[a-zA-Z0-9_\-\.]+\.[a-zA-Z]{2,}$")).IsMatch(val))
                    return "Email address is not valid.";
                return "";
            };

            string value = "";
            if (InputBox.Show("Enter your email address", "Email address:", ref value, validation) == DialogResult.OK)
            {

                if (value == "[email protected]")
                {
                    dataGridView1.Visible = true;
                    button_delete.Visible = true;
                    button1.Visible = true;
                    button_show.Visible = true;
                    label6.Visible = true;
                    label4.Visible = true;
                    label5.Visible = true;
                    textBox_uemail.Visible = true;
                    textBox_uname.Visible = true;
                    textBox_upassword.Visible = true;
                    textBox_delete.Visible = true;
                    button_deleteTable.Visible = true;

                    button_updatep.Visible = true;
                    textBox_updateall.Visible = true;
                }
                MessageBox.Show(value);
            }
            else
            {
                MessageBox.Show("You are not authenticated");





            }
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文