简体   繁体   English

文本框不返回值

[英]Textbox doesn't return the value

I have this class ( Class1 ) that retrieves value from a query. 我有这个类( Class1 )从查询中检索值。 Now I would like to make it object oriented thats why I wrote it that way. 现在我想让它面向对象,这就是为什么我这样写的。 Now the problem is, The TextBox ( TicketNum ) wont return the value and display it in the form. 现在的问题是, TextBoxTicketNum )不会返回值并在表单中显示它。 I dont know why. 我不知道为什么。 Help. 救命。

 public partial class Main : System.Web.UI.Page
{
   private String _TicketNum;

    public String TicketNum
    {
        get { return _TicketNum; }
        set { _TicketNum = value; }
    }
}

  public class Class1
{
    Main main = new Main();
    public void SelectTop1()
    {
        using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
        {

            using (SqlCommand com_retrieve = new SqlCommand("usp_SelectTop1Ticket", con))
            {
                com_retrieve.CommandType = CommandType.StoredProcedure;
                con.Open();
               try
                {                     
                    main.TicketNum = com_retrieve.ExecuteScalar().ToString();
                    MessageBox.Show("Ticket Has been saved. Your Ticket Number: " + com_retrieve.ExecuteScalar().ToString(), "Ticket Filed");
                }
                catch (SqlException)
                {
                    MessageBox.Show("The database has encountered an error");
                }
                catch (Exception)
                {
                    MessageBox.Show("The server has encountered an error");
                }
            }
        }
    }
}

  protected void btn_Save_Click(object sender, EventArgs e)
    {
         Class1 selectTop1 = new Class1();
            selectTop1.SelectTop1();
    }

Your string TicketNum will always be empty because you have not assigned it a value. 您的字符串TicketNum将始终为空,因为您尚未为其分配值。

Fortunately strings do not connect to textboxes automatically, this would be very frustrating. 幸运的是,字符串不会自动连接到文本框,这将非常令人沮丧。

To assign the string to the text box then: 要将字符串分配给文本框,请:

string TicketNum = txtbox.Text;

By the sounds of your question then this may not be the case, it sounds like you want a textbox to appear in your form at this point. 根据您的问题的声音,情况可能并非如此,听起来您希望文本框此时显示在您的表单中。 So to do this you need to create a new textbox for your form. 因此,要执行此操作,您需要为表单创建一个新的文本框。 You can do this in the toolbox, or dynamically. 您可以在工具箱中或动态地执行此操作。 I will show you how to do it dynamically: 我将向您展示如何动态地执行此操作:

TextBox txtbox = new TextBox();
txtbox.ID = "textBox1";
form1.Controls.Add(txtbox);

Ofcourse you would then name these textboxes to a more suited name. 然后,您可以将这些文本框命名为更合适的名称。 Then you would then go back to your string ticketnum and update it to call the name textbox ID. 然后,您将返回到您的字符串ticketnum并更新它以调用名称文本框ID。

Edit 编辑

I have answered what your question asked, but from looking at your comments I can see your question was of poor quality. 我已经回答了你的问题,但是通过查看你的评论,我发现你的问题质量很差。 It sounds more like you want to pass the information from a txtbox in the main class to this class. 听起来更像是要将主要类中的txtbox中的信息传递给此类。 OOP makes this pretty simple. OOP使这非常简单。 in the main class you need to store the txtbox into a string. 在主类中,您需要将txtbox存储到字符串中。 Then you need to create a constructor in your new class, then simply pass the string parameter to your new class constructor. 然后,您需要在新类中创建构造函数,然后将字符串参数传递给新的类构造函数。

If you are unsure on this, I would advise looking into Constructors , and for an over all of OOP look into this msdn article . 如果您对此不确定,我建议您查看构造函数 ,并查看这篇msdn文章的全部OOP。

Edit2 EDIT2

Before SO becomes google, I belive you should read up on OOP again, you have not understood it. 在SO成为谷歌之前,我相信你应该再次阅读OOP,你还没有理解它。 In this class you could use inheritance rather than calling the main class in this class, not sure why you would do this anyway. 在这个类中,您可以使用继承而不是在此类中调用主类,但不确定为什么要这样做。 If you read the articles I gave you I would have thought you would have saw your errors by now and fixed this issue. 如果您阅读我给你的文章我会认为你现在已经看到了你的错误并解决了这个问题。

Take a look at the following: 看看以下内容:

public partial class Main : System.Web.UI.Page
{
    public String TicketNum
    {
        get { return textbox.Text; }
        set { textbox.Text = value; }
    }

    protected void btn_Save_Click(object sender, EventArgs e)
    {
        Class1 selectTop1 = new Class1();
        TicketNum = selectTop1.SelectTop1();
    }
}

public class Class1
{
    public string SelectTop1()
    {
        // assign the value from SQL to the response variable
        string response = "test";
        return response;
    }
}

Note the following: 请注意以下事项:

  • The TicketNum property sets / gets the value to / from the textbox.Text property TicketNum从属性设置/获取价值/ textbox.Text财产
  • There is not need (please read wrong / why would you? ) to create an instance of the Main class inside the Class1 class 没有必要(请读错/为什么会这样? )在Class1类中创建Main类的实例
  • The SelectTop1 method returns a string value, which is used in the Main class (maybe you could even make it static, so you don't need an instance of the Class1 class to call it) SelectTop1方法返回一个字符串值,该值在Main类中使用(也许您甚至可以将其设置为静态,因此您不需要Class1类的实例来调用它)
  • The MessageBox class is used in WinForms, maybe you should use some JavaScript for alerts MessageBox类在WinForms中使用,也许您应该使用一些JavaScript进行警报

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM