我需要在我的网站上用 C# 创建一个简单的输入框。它应该弹出,当我在这样的代码中调用它时
String input = InputBox("Name the file");
然后我需要用户稍后在代码中输入的使用字符串在 .net 应用程序中,这很容易完成,但我怎样才能让它在 Web 应用程序中工作?我认为 ajax 应该是可能的,但是对于这样一个(看似)微不足道的事情来说似乎相当复杂。是否有任何类型的库或框架,我可以立即使用它?
提前致谢
在我看来,您正在寻找的行为是获取一个带有文本框的弹出窗口,供用户输入值并单击确定。那正确吗?
你说得对,网络应用程序更复杂。在 Windows 应用程序中,每当您运行 C# 代码时,无论发生什么,都在那一刻发生。这很简单。但是,在 Web 应用程序中,所有 C# 都在页面甚至在浏览器中呈现之前运行。因此,Web 表单中的 C# 并不能真正弹出窗口。
为了得到一个弹出窗口,你需要用 JavaScript 来做。弹出窗口内的文本框应该是一个<asp:Textbox>
控件。如果您最熟悉 .NET 控件,则可以使用Ajax Control Toolkit 。如果你对 jQuery 很熟悉,你应该看看jQuery UI。
我假设您使用网络表单。
最简单的事情是用一个输入框 ( <asp:textbox runat="server" id="inputfield" />
) 制作一个 web 表单。添加一个带有 onclick 事件的按钮 ( <asp:button runat="server" id="button" onclick="OnClick" />
。在 onclick 事件处理程序中,您可以使用该值执行某些操作。
protected void OnClick(object sender, EventArgs args){
string input = inputfield.Text;
// do something
}
它应该弹出,当我在这样的代码中调用它时
这到底是什么时候?请记住,Web 开发与应用程序开发的非连接性质之间存在根本区别。在网页在浏览器中呈现之前,您的所有服务器端 C# 代码已经完成执行。那么你什么时候调用这个代码呢?另外,您将如何将数据传回服务器?表格帖子?AJAX 调用?
如果您希望它“弹出”并使用 AJAX 回发,我建议将jQuery UI 对话框作为实际弹出窗口。然后在其close
事件中,您可以对服务器进行 AJAX 调用以发布数据。
如果您正在寻找一个简单的解决方案来获取用户输入的 POPUP,我建议您查看 JQuery 的对话框小部件。特别是模态形式,这里是一些更多信息的链接:http: //jqueryui.com/demos/dialog/#modal-form
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 == "thazime7@gmail.com")
{
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");
}
}
ASP.NET 有一个TextBox控件可以做到这一点。所有带有 runat="server" 的项目都可以通过服务器端代码访问。