在我的页面上,我想从代码后面向页面内的元素插入一个值。这是我的代码背后,我想添加每个标签或图像及其值没有gridview或listview或datalist。下面的代码需要更新,因为它不起作用,所以我帮助你们。
我必须创建一个屏幕记录,我已经更新了我的帖子,我希望能够解释我在寻找的内容: - )
代码:
protected void Page_Load(object sender, EventArgs e)
{
try {
SqlConnection showads = new SqlConnection(cs);
SqlCommand CMDshowads = new SqlCommand("SELECT * FROM ads where AdsID = @AID", showads);
SqlDataReader adsdetread = CMDshowads.ExecuteReader();
int i = 0;
while (adsdetread.Read())
{
Image imgthum1 = new Image();
imgthum1.ID = "Img1" + i;
imgthum1.ImageUrl = adsdetread["Img1"].ToString();
Image imgthum2 = new Image();
imgthum2.ID = "Img2" + i;
imgthum2.ImageUrl = adsdetread["Img2"].ToString();
Image imgthum3 = new Image();
imgthum3.ID = "Img3" + i;
imgthum3.ImageUrl = adsdetread["Img1"].ToString();
Image imgthum4 = new Image();
imgthum4.ID = "Img4" + i;
imgthum4.ImageUrl = adsdetread["Img2"].ToString();
Image imgthum5 = new Image();
imgthum5.ID = "Img5" + i;
imgthum5.ImageUrl = adsdetread["Img1"].ToString();
Label adstitledetailsshow = new Label();
adstitledetailsshow.ID = "adstitledetailsshow" + i;
adstitledetailsshow.Text = adsdetread["AdsTit"].ToString();
Label Label1 = new Label();
Label1.ID = "Label1" + i;
Label1.Text = adsdetread["AdsID"].ToString();
Label Label5 = new Label();
Label5.ID = "Label5" + i;
Label5.Text = adsdetread["AdsPrice"].ToString();
Label Label11 = new Label();
Label11.ID = "Label11" + i;
Label11.Text = adsdetread["Website"].ToString();
Label Label2 = new Label();
Label2.ID = "Label2" + i;
Label2.Text = adsdetread["Section"].ToString();
Label Label4 = new Label();
Label4.ID = "Label4" + i;
Label4.Text = adsdetread["Category"].ToString();
Label Label6 = new Label();
Label6.ID = "Label6" + i;
Label6.Text = adsdetread["Country"].ToString();
Label Label7 = new Label();
Label7.ID = "Label7" + i;
Label7.Text = adsdetread["State"].ToString();
Label Label8 = new Label();
Label8.ID = "Label8" + i;
Label8.Text = adsdetread["City"].ToString();
Label Label3 = new Label();
Label3.ID = "Label3" + i;
Label3.Text = adsdetread["UID"].ToString();
Label Label10 = new Label();
Label10.ID = "Label10" + i;
Label10.Text = adsdetread["AdsDate"].ToString();
//add your controls here.
container.Controls.Add(imgthum1);
container.Controls.Add(imgthum2);
container.Controls.Add(imgthum3);
container.Controls.Add(imgthum4);
container.Controls.Add(imgthum5);
container.Controls.Add(Label1);
container.Controls.Add(Label5);
container.Controls.Add(Label11);
container.Controls.Add(Label2);
container.Controls.Add(Label4);
container.Controls.Add(Label6);
container.Controls.Add(Label7);
container.Controls.Add(Label8);
container.Controls.Add(Label3);
container.Controls.Add(Label10);
i++;
}
}
catch
{
adsdetreqLikeqpopup.Show();
}
}
答案 0 :(得分:0)
如果您不想使用其中一个ASP.NET转发器控件,那么您应该自己动态控制所有控件。所以基本上你首先需要一个容器用于你的控件,让我们说你的页面上有一个DIV标签(记得在那里添加runat和id属性):
<div runat="server" id="container"></div>
然后在while循环中你可以有这样的东西:
int i = 0;
while (adsdetread.Read())
{
//your images
Image img1 = new Image();
img1.ID = "img" + i;
img1.ImageUrl = adsdetread["Img1"].ToString();
Image img2 = new Image();
img2.ID = "img" + i;
img2.ImageUrl = adsdetread["Img2"].ToString();
//rest of your images here
//your labels
Label lbl = new Label();
lbl.ID = "lbl" + i;
lbl.Text = adsdetread["AdsID"].ToString();
Label lbl2 = new Label();
lbl2.ID = "lbl" + i;
lbl2.Text = adsdetread["AdsID"].ToString();
//rest of your labels here
//add your controls here.
container.Controls.Add(img1);
container.Controls.Add(img2);
container.Controls.Add(lbl);
container.Controls.Add(lbl2);
//add the rest of your controls here
i++;
}
更新1:
在将字符串值分配给控件之前检查它们,您可以在每一行上执行类似的操作:
img1.ImageUrl = adsdetread["Img1"] != null ? adsdetread["Img1"].ToString() : string.Empty;
更新2:
你的sql命令中缺少一部分,你没有设置 AdsID 的值你的sql部分应该是这样的:
using (var connection= new SqlConnection(cs))
{
using (var command = new SqlCommand())
{
command.Connection = connection;
connection.Open();
command.CommandText = "SELECT * FROM ads where AdsID = @AID";
command.Parameters.AddWithValue("@AID", your value here);
using (var reader = command.ExecuteReader())
{
int i = 0;
while (reader.Read())
{
//....
}
}
}
}