如何在Datalist中检查HTML控件属性?

时间:2013-10-29 14:38:05

标签: asp.net html5 datalist

我的代码找到了 HTML 控件"的来源"我想检查一下" Src "属性是否有价值,我试图添加"内心文字"但 它返回 NULL

<asp:DataList ID="DL_Media" runat="server"  onitemdatabound="DL_Media_ItemDataBound">
    <ItemTemplate>
        <video width="215" height="160" runat="server"  id="vd" controls>
        <source src='<%# Eval("Media_File")%>' type="video/ogg" 
            runat="server" id="source"></source>
        </video>
    </ItemTemplate>
</asp:DataList>


protected void DL_Media_ItemDataBound(object sender, DataListItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        HtmlGenericControl video = e.Item.FindControl("vd") as HtmlGenericControl;
        HtmlGenericControl source = e.Item.FindControl("source") as HtmlGenericControl;
        if (source != null)
        {
            string x = "~/";
            string y = "";
            if (source.InnerText == x)
            {
                video.InnerText.Replace(x, y);

                DL_Media.DataBind();


            }
        }
    }

1 个答案:

答案 0 :(得分:1)

试试这个:

HtmlGenericControl source= e.Item.FindControl("source") as HtmlGenericControl;
string src = source.Attributes["src"].ToString();

来源:How to: Read HTML Attributes for Controls in Web Forms Pages