为什么中继器控制的 onitemcommand 在这种情况下不起作用?

发布于 2024-12-03 09:14:47 字数 2472 浏览 3 评论 0原文

我在 ItemTemplate 中有图像,该图像将根据用户名(即 lblPostedBy.Text)进行更改。我在C# Asp.net 4.0中的repeater控件的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>

提前致谢,

, 尼基尔

I have Image in ItemTemplate that will be changing according to Username (ie lblPostedBy.Text). I have written code in onItemCommand of repeater control in C# Asp.net 4.0. But not working. Will you please help me.

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;
}

and

<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") %>' />   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>

Thanks in Advance,

Regards,
Nikhil

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

勿忘初心 2024-12-10 09:14:47

而不是“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);

Instead of "cmd.CommandText = "select image from " + lblPostedBy.Text;"

Shouldn't it be

cmd.CommandText = "select image from TableName where pby = '" + lblPostedBy.Text + "'";

Better yet, add a parameter

cmd.CommandText = "select image from tablename where pby = @pby"

cmd.Parameters.Add(new SalParameter("@pby", lblPostedBy.Text);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文