Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么中继器控制命令在这种情况下不起作用?_C#_.net_Asp.net_Repeater - Fatal编程技术网

C# 为什么中继器控制命令在这种情况下不起作用?

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

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.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"