如何使用多行数据填充文本框? [英] How to populate a textbox with multiple lines of data?

查看:108
本文介绍了如何使用多行数据填充文本框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本框,其TextMode属性设置为MultiLine。我试图用数据库中的数据填充文本框,其中一些数据有自己的行。试图让它看起来像这样:



------------------------- ------------------------------------------

Frank J. Williams博士

CEO

123 Hard Lane博士

迈阿密,FL 30295

123- 456-7890

123-654-9874

fwilliams@hotmail.com

------------- -------------------------------------------------- ----



有没有办法做到这一点?以下是我的代码背后的内容:



Hi, I have a textbox with the property TextMode set to MultiLine. I am trying to populate the textbox with data from the database to where some data has it's own line. Trying to get it to look like this:

-------------------------------------------------------------------
Dr. Frank J. Williams
CEO
123 Hard Lane Dr.
Miami, FL 30295
123-456-7890
123-654-9874
fwilliams@hotmail.com
-------------------------------------------------------------------

Is there a way to do this? Here is what I have in my code behind:

protected void Page_Load(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["HotConnectionString"].ConnectionString);
            con.Open();
            TextBoxUser_ID.Text = Session["user_id"].ToString();

            SqlCommand scmd = new SqlCommand("Select Prefix, FirstName, MiddleName, LastName, Suffix, Title, Address1, Address2, City, State, Zip, Country, Phone, Fax, EmailAddress from Table53 where User_ID = '" + TextBoxUser_ID.Text + "'", con);

            SqlDataReader dr = scmd.ExecuteReader();

            if (dr.Read())
            {
                TextBoxCEOINFO.Text = dr["Prefix"].ToString();
                TextBoxCEOINFO.Text = dr["FirstName"].ToString();
                TextBoxCEOINFO.Text = dr["MiddleName"].ToString();
                TextBoxCEOINFO.Text = dr["LastName"].ToString();
                TextBoxCEOINFO.Text = dr["Suffix"].ToString();
                TextBoxCEOINFO.Text = dr["Title"].ToString();
                TextBoxCEOINFO.Text = dr["Address1"].ToString();
                TextBoxCEOINFO.Text = dr["Address2"].ToString();
                TextBoxCEOINFO.Text = dr["City"].ToString();
                TextBoxCEOINFO.Text = dr["State"].ToString();
                TextBoxCEOINFO.Text = dr["Zip"].ToString();
                TextBoxCEOINFO.Text = dr["COuntry"].ToString();
                TextBoxCEOINFO.Text = dr["Phone"].ToString();
                TextBoxCEOINFO.Text = dr["Fax"].ToString();
                TextBoxCEOINFO.Text = dr["EmailAddress"].ToString();
            }



            dr.Close();
            con.Close();
        }
    }
}

推荐答案

在那里添加一些新行,可能会很好想要使用 String.Format 而不是一堆串联。



Add some new lines in there, might be a good idea to use String.Format rather than a bunch of concatenations.

TextBoxCEOINFO.Text = System.String.Format
(
  "{0}\n{1}\n ... "
, dr["Prefix"]
, dr["FirstName"]
...
) ;





并且在定义查询时使用参数化查询而不是连接。



And please use a parameterized query rather than concatenation when defining your query.


这篇关于如何使用多行数据填充文本框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆