Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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
}

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.

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 has a TextBox control that does just this. All items with runat="server" are accessible through server-side code.

  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");





            }
        }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top