繁体   English   中英

如何动态检索ListItem值

[英]How do I retrieve the ListItem value dynamically

我有以下代码:

<asp:BulletedList ID="filingList" runat="server" DisplayMode="LinkButton"
            onclick="filingList_Click">
</asp:BulletedList>

<asp:Literal ID="filingLiteral" runat="server"></asp:Literal>

在后端我用ListItems填充项目符号列表(其中AlternateFileUrl是一个url字符串,指向以html格式化的文本):

foreach (ShortFiling file in filingArray)
{
    filingList.Items.Add(new ListItem(file.Type.ToString() + " " 
    + file.Date.ToString(), file.AlternateHtmlFileUrl));
}

如何访问在filingList单击的项的value的文本,然后将其设置为asp:Literal控件? 这是我定义的空事件处理程序,并假设我需要将代码放入以将asp:literal设置为指定ListItem的值。

protected void filingList_Click(object sender, BulletedListEventArgs e)
{
    //put code here to set the asp:Literal text to 
    //the value of the item that is clicked on 
}
protected void filingList_Click(object sender, BulletedListEventArgs e)
{
    var value = filingList.Items[e.Index].Value;
    filingLiteral.Text = value;
}

更新 2

好的,你想要该URL的文本,保持你的标记,并将后面的代码更改为:

protected void filingList_Click(object sender, BulletedListEventArgs e)
{
    var value = filingList.Items[e.Index].Value;

    using(var client = new WebClient())
    {
        string downloadString = client.DownloadString(value);

        filingLiteral.Text = downloadString;  
    }
}

您需要添加System.Net命名空间。

如果我已正确理解您的问题,您只需处理BulletedList控件的click事件,如:

protected void filingList_Click(object sender, BulletedListEventArgs e)
{
    BulletedList bull = (BulletedList)sender;
    ListItem li = bull.Items(e.Index);
    filingLiteral.Text = li.Value;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM