在asp标记标记内添加代码块

时间:2014-10-22 08:47:45

标签: c# asp.net

这是我的代码:

 <ul class="slides">

                <%
                    int count = 0;
// tb is the DataTable i fetched  
                    if (tb != null)
                    {
                        for (int i = 0; i < tb.Rows.Count; i++)
                        {

                %>

                <div class="ft-item"  >

                                 <%       
                                   Image1.ImageUrl = tb.Rows[i]["timg"].ToString();
                                   Image1.ID = "img" + tb.Rows[i]["xtid"];
                                   LinkButton1.ID = tb.Rows[i]["xtid"].ToString();
                                %>

                    <asp:UpdatePanel ID="UpdatePanel2" runat="server">
                        <ContentTemplate>
                            <span class="ft-image">
                                <asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">

// I WANT TO  ADD THE CODE BLOCK : IF(tb.Rows[i]["ALERT"].TOSTRING() == "YES") RESPONE.WRITE("STH");

                                    <asp:Image ID="Image1" runat="server" />
                                </asp:LinkButton>
                            </span>
                        </ContentTemplate>
                    </asp:UpdatePanel>



                <%

                                count++;

                        }
                    }
                    else
                    {
                        Response.Write("<h2 class='t-heading text-upper' style='color:#bd6767'> No Data </h2>");
                    }

                %>
            </ul>

我想在asp标记标记内添加代码块,但我不能(在上面留下的评论中)。我做了很多搜索所有要求我做数据绑定,但我要求另一种方式,如你所见,这样我们不能做数据绑定。 我确实尝试在代码上方添加if(tb.Rows[i]["ALERT"].TOSTRING())LinkButton1.Text = "STH",但它会删除我的图片代码

1 个答案:

答案 0 :(得分:0)

Label

中添加LinkButton控件
<asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">
    <asp:Label runat="server" ID="Label1" />
    <asp:Image ID="Image1" runat="server" />
</asp:LinkButton>

然后将以下内容添加到现有代码块中。

<%       
   Image1.ImageUrl = tb.Rows[i]["timg"].ToString();
   Image1.ID = "img" + tb.Rows[i]["xtid"];
   LinkButton1.ID = tb.Rows[i]["xtid"].ToString();

   // set the label text
   if (row["ALERT"].ToString() == "YES")
   {
       Label1.Text = "STH";
   }
%>

但是你应该重新考虑重构代码,以便数据绑定能够正常工作,因为你会发现最终会有更多的灵活性。然后,运行页面的代码也可以与页面/用户控件的标记分开,这样可以促进关注点的分离,这也将具有更好的代码可读性的另一个好处。

我创建的以下示例ASPX页面应演示如何使用ListView控件上的数据绑定实现相同的结果。

<%@ Page Language="C#" AutoEventWireup="true" %>

<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        // Create some test data to play with.
        System.Data.DataTable table = new System.Data.DataTable();
        table.Columns.Add("xtid", typeof(int));
        table.Columns.Add("timg", typeof(string));
        table.Columns.Add("ALERT", typeof(string));

        table.Rows.Add(1, "/some_img.jpg", "YES");
        table.Rows.Add(2, "/some_img.jpg", "NO");
        table.Rows.Add(3, "/some_img.jpg", "NO");
        table.Rows.Add(4, "/some_img.jpg", "YES");

        MyListView.DataSource = table;
        MyListView.DataBind();
    }

    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        // output the id of the button clicked.
        ButtonClicked.Text = ((Control)sender).ID;
    }

    protected void MyListView_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        var row = (System.Data.DataRowView)e.Item.DataItem;

        // Find the link button on the item's template.
        var linkButton = e.Item.FindControl("LinkButton1") as LinkButton;
        if (linkButton != null)
        {
            linkButton.ID = row["xtid"].ToString();

            var buttonText = linkButton.FindControl("ButtonText") as Label;
            if (buttonText != null)
            {
                if (row["ALERT"].ToString() == "YES")
                {
                    buttonText.Text = "STH";
                }
            }

            var image1 = linkButton.FindControl("Image1") as Image;
            if (image1 != null)
            {
                image1.ImageUrl = row["timg"].ToString();
                image1.ID = "img" + row["xtid"];
            }
        }
    }
</script>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>

    <form id="form1" runat="server">
        <asp:ScriptManager runat="server" ID="ScriptManager1" />
        <div>
            <asp:Label runat="server" ID="ButtonClicked" />

            <asp:UpdatePanel ID="UpdatePanel2" runat="server">
                <ContentTemplate>
                    <asp:ListView runat="server" ID="MyListView" OnItemDataBound="MyListView_ItemDataBound">
                        <LayoutTemplate>
                            <ul class="slides">
                                <asp:PlaceHolder runat="server" ID="ItemPlaceHolder" />
                            </ul>
                        </LayoutTemplate>
                        <ItemTemplate>
                            <%-- The item template is used to render each of the rows of the DataTable that has been binded to the ListView control. --%>
                            <div class="ft-item">
                                <span class="ft-image">
                                    <asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">
                                        <asp:Label runat="server" ID="ButtonText" />
                                        <asp:Image ID="Image1" runat="server" />
                                    </asp:LinkButton>
                                </span>
                            </div>
                        </ItemTemplate>
                    </asp:ListView>
                    </span>
                </ContentTemplate>
            </asp:UpdatePanel>
        </div>
    </form>
</body>
</html>