簡體   English   中英

為什么在這種情況下中繼器控制的onitemcommand不起作用?

[英]Why onitemcommand of repeater control not working in this case?

我在ItemTemplate中有圖像,該圖像將根據用戶名(即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;
}

<asp:Repeater ID="myrepeater" runat="server" 
    onitemcommand="myrepeater_ItemCommand1">
        <HeaderTemplate>
            <table width="100%" style="font: 8pt verdana">
                <tr style="background-color:#3C78C3">
                    <th>NEWS FEED</th>
                </tr>

        </HeaderTemplate>
        <ItemTemplate>
            <tr>
                <td valign="top">
                   <asp:Image ID="Image1" runat="server" Height="50px" Width="50px" />
                   <asp:LinkButton ID="lblPostedBy" runat="server" onclick="lblPostedBy_Click" Text='<%#DataBinder.Eval(Container,"DataItem.postedby") %>' /> &nbsp says : <br />
                   <asp:TextBox id="txtPost" runat="server"  Width="100%" textmode="multiline" text='<%#DataBinder.Eval(Container,"DataItem.scraps")%>' ReadOnly="True" BackColor="#EFF3FB" BorderStyle="None"></asp:TextBox> 
                   <%#DataBinder.Eval(Container,"DataItem.scraptime") %>
                   <br />
                   <asp:TextBox ID="TextBox2" runat="server" Visible="False" Width="80%"></asp:TextBox>
                   <asp:Button ID="btnDoneComment" runat="server" Text="Done" Visible="False" onclick="btnDoneComment_Click"/>
                   <br />
                   <hr style="border:dashed 1px blue" />
                </td>
           </tr>
        </ItemTemplate>
        <FooterTemplate>
            </table>
        </FooterTemplate>
    </asp:Repeater>

提前致謝,

此致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.Add(new SalParameter("@pby", lblPostedBy.Text);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM