C# 为什么中继器控制命令在这种情况下不起作用?
ItemTemplate中的图像将根据用户名ie lblPostedBy.Text进行更改。我已经用C Asp.net 4.0中转发器控件的onItemCommand编写了代码。但不起作用。请你帮帮我好吗C# 为什么中继器控制命令在这种情况下不起作用?,c#,.net,asp.net,repeater,C#,.net,Asp.net,Repeater,ItemTemplate中的图像将根据用户名ie lblPostedBy.Text进行更改。我已经用C Asp.net 4.0中转发器控件的onItemCommand编写了代码。但不起作用。请你帮帮我好吗 protected void myrepeater_ItemCommand1(object source, RepeaterCommandEventArgs e) { // Actually lblPostedBy is a linkbutton and lblPostedBy.Tex
protected void myrepeater_ItemCommand1(object source, RepeaterCommandEventArgs e)
{
// Actually lblPostedBy is a linkbutton and lblPostedBy.Text is 'username of the commenter'.
LinkButton lblPostedBy = (LinkButton)e.Item.FindControl("lblPostedBy");
con.Open();
cmd.CommandText = "select image from " + lblPostedBy.Text + " where id=1";
// 'image' column in table stores path of image like '~/image/image1.jpg'
cmd.Connection = con;
string imageurl = (string)cmd.ExecuteScalar();
con.Close();
Image Image1 = (Image)e.Item.FindControl("Image1");
Image1.ImageUrl = imageurl;
}
及
提前感谢,
问候,,
Nikhil而不是cmd.CommandText=从+lblPostedBy.Text中选择图像 难道不是吗
cmd.CommandText = "select image from TableName where pby = '" + lblPostedBy.Text + "'";
更好的方法是添加一个参数
cmd.CommandText = "select image from tablename where pby = @pby"
cmd.Parameters.AddnewSalParameter@pby,lblPostedBy.Text 谢谢abatishchev先生的编辑。我估计onItemCommand不会在页面加载时启动,而是在中继器控件内单击按钮时启动。错误消息是什么?您是否使用了从TableName中选择图像,其中pby='+lblPostedBy.Text+'或参数版本?我使用了:cmd.CommandText=从+lblPostedBy.Text+中选择图像,其中id=1//这将是从保存图像路径的表中检索图像列,如“~/image/image1.jpg”,所以每个海报都有一个表?语法是SELECT ColumnName FROM TableName,其中CONDICTION1和CONDICTION2请使用表名和列名发布表定义
cmd.CommandText = "select image from tablename where pby = @pby"