是否可以将数据表对象作为参数传递给另一个控件对象,如下所示:
protected void DropDownlist2(string model)
{
queryString = "Select modelcar from useritem where typecar=@model";
SqlCommand cmd = new SqlCommand(queryString, useritemConnectionString1);
cmd.Parameters.AddWithValue("@model", model);
useritemConnectionString1.Open();
DataTable dt = new DataTable();
SqlDataAdapter ad = new SqlDataAdapter(cmd);
ad.Fill(dt);
if (dt.Rows.Count > 0)
{
DropDownList2.DataSource = dt;
DropDownList2.DataBind();
}
useritemConnectionString1.Close();
Button1(dt);
}
private void Button1_Click(DataTable dt)
{
SqlDataReader dr = cmd.ExecuteReader();
if(dr.HasRows)
{
while (dr.Read())
{
System.Web.UI.WebControls.Image imgControl = new System.Web.UI.WebControls.Image();
HtmlGenericControl paraContainer = new HtmlGenericControl("p");
paraContainer.Controls.Add(imgControl);
PlaceHolder1.Controls.Add(paraContainer);
//Get Image Information
imgControl.ImageUrl = dr["Image"].ToString();
}
}
else
dr.Close();
}
出于某种原因,我收到此错误
button1_Click(System.data.datatable) 由于其保护级别而无法访问