在没有列表视图的情况下很难绑定元素值
本文关键字:很难 绑定 元素 情况下 列表 视图 | 更新日期: 2023-09-27 18:12:07
在我的页面上,我想从后面的代码中插入一个值到页面内的元素。这是我的代码后面,我想添加每个标签或图像,它的值没有gridview或listview或数据。下面的代码需要更新,因为它不工作,所以我从你们的帮助。
我必须创建一个屏幕记录,我已经更新了我上面的帖子,我希望这将解释我正在寻找的东西:-)
输入链接描述
代码: 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();
}
}
如果您不希望使用ASP. html. 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())
{
//....
}
}
}
}