【问题标题】:Access Repeater controls propeties访问中继器控件属性
【发布时间】:2015-05-29 01:14:55
【问题描述】:

我需要使用Response.Redirect() 重定向到带有查询字符串值的页面,因为我无法设置此值。 我有一个包含ImageButtonLabel 的转发器,标签的文本是从数据库中检索的,我需要响应带有 URL"ShowCourse.aspx?coursename=value" 的页面,其中 value 是标签的值。

中继器代码:

<asp:Repeater ID="CoursesListRepeater" runat="server" DataSourceID="FetchCourse">
                    <ItemTemplate>
                        <table>
                            <tr>
                                <td>
                                    <asp:ImageButton ID="CourseImage" runat="server" Height="90" Width="90" ImageUrl='<%# Eval("CourseImage")%>' CssClass="imageButtons" OnClick="CourseImage_Click" />
                                </td>
                                <td>
                                    <asp:Label runat="server" ID="CourseNameLabel"><a href='<%#String.Format("AboutCourse.aspx?coursename={0}",Eval("CourseName")) %>' style="font-family: 'Buxton Sketch'; font-size: 40px;"><%#Eval("CourseName") %></a></asp:Label>
                                    <br />
                                    <asp:Label ID="CategoryLabel" runat="server" Font-Size="22px" ForeColor="Gray" Text='<%#Eval("CourseCategory") %>' />
                                    <br />
                                    <asp:Label ID="DescriptionLabel" Font-Size="13px" runat="server" Text='<%#Eval("CourseDescription") %>' ForeColor="DarkRed" />
                                </td>
                            </tr>
                            <hr style="margin-top: 30px; border-radius: 5px;" />
                        </table>
                    </ItemTemplate>
                </asp:Repeater>

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    您可以像这样在 ImageButton 上使用 CommandName 和 CommandArgument:

    模板:

    <asp:Repeater ID="CoursesListRepeater" runat="server" DataSourceID="FetchCourse" 
                onitemcommand="CoursesListRepeater_ItemCommand"></asp:Repeater>
        ....
        <asp:ImageButton ID="CourseImage" runat="server" Height="90" Width="90" ImageUrl='<%# Eval("CourseImage")%>' CssClass="imageButtons" 
            CommandName="ShowCourse" CommandArgument='AboutCourse.aspx?coursename=<%# Eval("CourseName") %>' />
    

    后面的代码:

    protected void CoursesListRepeater_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "ShowCourse")
            {
                Response.Redirect(e.CommandArgument.ToString());
            }
        }
    

    【讨论】:

    • 是的,但我要求在单击 ImageButton 后重定向到另一个页面,查询字符串值等于图像旁边的标签
    • 很好,它工作正常,但浏览器中的 url 与 html 代码中的相同:"localhost:4302/AboutCourse.aspx?coursename=%3C%#Eval("CourseName") %>"
    【解决方案2】:

    最好在你的类中添加一个名为Url的属性并使用上述方法并设置CommandArgument='&lt;%# Eval("Url")%&gt;'
    请记住,您必须从我们后面的代码中创建重定向 url,并将其设置为您新添加的中继器数据项的 Url 属性

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-07-01
      • 1970-01-01
      • 2021-11-12
      • 2010-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多