问题描述
我有一个文本框,其TextMode属性设置为MultiLine。我试图用数据库中的数据填充文本框,其中一些数据有自己的行。试图让它看起来像这样:
------------------------- ------------------------------------------
Frank J. Williams博士
CEO
123 Hard Lane博士
迈阿密,FL 30295
123- 456-7890
123-654-9874
[email protected]
------------- -------------------------------------------------- ----
有没有办法做到这一点?以下是我的代码背后的内容:
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
[email protected]
-------------------------------------------------------------------
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();
}
}
}
推荐答案
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.
这篇关于如何使用多行数据填充文本框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!