问题描述
用户控件:
私人字符串的lastName;
公共字符串姓氏
{
得到{lastName的; }
组
{
lastName的=价值;
lastNameTextBox.Text =价值;
}
}
形式:
使用(SqlConnection的myDatabaseConnection =新的SqlConnection(myConnectionString.ConnectionString))
{
myDatabaseConnection.Open();使用
(的SqlCommand的SqlCommand =新的SqlCommand(选择LasatName从雇员,myDatabaseConnection))
{
INT I = 0;
SqlDataReader的DR1 = SqlCommand.ExecuteReader();
,而(DR1.Read())
{
I ++;
UserControl2用户控件=新UserControl2();
usercontrol.Tag = I;
usercontrol.LastName =(字符串)DR1 [姓氏];
usercontrol.Click + =新的EventHandler(usercontrol_Click);
flowLayoutPanel1.Controls.Add(用户控件);
}
}
}
的形式加载从记录数据库并显示在动态创建的每个用户控件文本框,每个姓氏。 ?
如何显示产生额外的信息,如窗体的文本地址时,一个动态用户控件点击
尝试:
私人无效usercontrol_Click(对象发件人,EventArgs五)
{$ b $使用(SqlConnection的myDatabaseConnection =新的SqlConnection(myConnectionString.ConnectionString))
b {
myDatabaseConnection.Open();
使用(的SqlCommand的MySqlCommand =新的SqlCommand(,myDatabaseConnection),从员工那里名字= @LastName选择地址)
{
UserControl2用户控件=新UserControl2();
mySqlCommand.Parameters.AddWithValue(@姓,usercontrol.LastName;
SqlDataReader的= SQLReader的mySqlCommand.ExecuteReader();
如果(sqlreader.Read())
{
textBox1.Text =(字符串)SQLReader的[地址];
}
}
}
}
首先,在你的第一个代码片段中,执行从选择ID .. ,而是希望找到场姓氏的读者 - 行不通的。
其次如果你知道你需要从<$ C的更多信息。 $ C>员工表,我建议你把它存储在同一用户控件
,你把姓
。执行从...选择*,另一个字段和属性添加到 UserControl2
,并在读$它分配C $ C>循环:
usercontrol.Address =(字符串)DR1 [地址];
第三。在你的第二个代码段,用
UserControl2用户控件=(UserControl2)发送;
而不是
UserControl2用户控件=新UserControl2();
由于新创建的用户控件将不会有任何姓$ C $ 。C>分配
然后:
私人无效usercontrol_Click(对象发件人,EventArgs五)
{
UserControl2用户控件=(UserControl2)发送;
textBox1.Text = usercontrol.Address;
}
UserControl:
private string lastName;
public string LastName
{
get { return lastName; }
set
{
lastName = value;
lastNameTextBox.Text = value;
}
}
Form:
using (SqlConnection myDatabaseConnection = new SqlConnection(myConnectionString.ConnectionString))
{
myDatabaseConnection.Open();
using (SqlCommand SqlCommand = new SqlCommand("Select LasatName from Employee", myDatabaseConnection))
{
int i = 0;
SqlDataReader DR1 = SqlCommand.ExecuteReader();
while (DR1.Read())
{
i++;
UserControl2 usercontrol = new UserControl2();
usercontrol.Tag = i;
usercontrol.LastName = (string)DR1["LastName"];
usercontrol.Click += new EventHandler(usercontrol_Click);
flowLayoutPanel1.Controls.Add(usercontrol);
}
}
}
The form loads the records from database and display each LastName in each usercontrols' textbox that created dynamically.How to show additonal information such as address in form's textbox when a dynamic-usercontrol is click?
Attempt:
private void usercontrol_Click(object sender, EventArgs e)
{
using (SqlConnection myDatabaseConnection = new SqlConnection(myConnectionString.ConnectionString))
{
myDatabaseConnection.Open();
using (SqlCommand mySqlCommand = new SqlCommand("Select Address from Employee where LastName = @LastName ", myDatabaseConnection))
{
UserControl2 usercontrol = new UserControl2();
mySqlCommand.Parameters.AddWithValue("@LastName", usercontrol.LastName;
SqlDataReader sqlreader = mySqlCommand.ExecuteReader();
if (sqlreader.Read())
{
textBox1.Text = (string)sqlreader["Address"];
}
}
}
}
First. In your first code fragment, you execute "Select ID from ...", but expect to find field "LastName" in the reader - not going to work.
Second. If you know that you will need more information from Employee
table, I suggest that you store it in the same UserControl
where you put the LastName
. Execute "Select * from ...", add another field and property to UserControl2
and assign it in the Read
loop:
usercontrol.Address = (string)DR1["Address"];
Third. In your second code fragment, use
UserControl2 usercontrol = (UserControl2)sender;
instead of
UserControl2 usercontrol = new UserControl2();
because newly created user control will not have any LastName
assigned.
Then:
private void usercontrol_Click(object sender, EventArgs e)
{
UserControl2 usercontrol = (UserControl2)sender;
textBox1.Text = usercontrol.Address;
}
这篇关于动态用户控件的点击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!