我需要在C#的网站上创建一个简单的输入框。 应该弹出,当我把它的代码一样,asp.net中的输入框c#
String input = InputBox("Name the file");
,然后我需要使用字符串的用户在后面的代码 在.NET应用程序进入,这是很容易完成,但我怎样才能使它在Web应用程序中工作?我认为应该可以使用ajax,但对于这样的(看似)微不足道的事情来说似乎相当复杂。 有没有任何一种库或框架,我可以立即使用它?
在此先感谢
我需要在C#的网站上创建一个简单的输入框。 应该弹出,当我把它的代码一样,asp.net中的输入框c#
String input = InputBox("Name the file");
,然后我需要使用字符串的用户在后面的代码 在.NET应用程序进入,这是很容易完成,但我怎样才能使它在Web应用程序中工作?我认为应该可以使用ajax,但对于这样的(看似)微不足道的事情来说似乎相当复杂。 有没有任何一种库或框架,我可以立即使用它?
在此先感谢
这听起来像你寻找的行为是让用户输入一个值并点击确定的文本框的弹出窗口。是对的吗?
你说得对,它是一个更复杂的Web应用程序。在Windows应用程序中,无论何时运行C#代码,无论发生什么,都会发生在那一刻。这非常简单。但是,在Web应用程序中,所有C#都在页面甚至在浏览器中呈现之前运行。因此,C#在web窗体中不能弹出一个窗口真的。
为了得到一个弹出窗口,你需要用JavaScript来做到这一点。弹出窗口中的文本框应该是<asp:Textbox>
控件。如果您最适合使用.NET控件,则可以使用Ajax Control Toolkit。如果您可以使用jQuery进行兼容,则应该查看jQuery UI。
ASP.NET有一个TextBox控制,不只是这个。所有runat =“server”的项都可以通过服务器端代码访问。
我假设你使用webforms。
最简单的事情就是制作一个带有一个输入框的网络表格(<asp:textbox runat="server" id="inputfield" />
)。添加一个按钮,一个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 Dialog作为实际弹出窗口。然后在其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_\-\.][email protected][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");
}
}
好说杰夫先生 – 2015-05-13 11:19:40