2013-03-18 71 views
0

呼叫列表项中的BulletedList目前在CS文件

我在我的aspx文件下面的BulletedList

<asp:BulletedList ID="DocumentList" runat="server" 
DisplayMode="LinkButton" onclick="DocumentList_Click"> 
    <asp:ListItem Value="Documents/testpage.pdf" Text="testpage.pdf" >test page</asp:ListItem> 
    <asp:ListItem Value="Documents/testpage2.pdf" Text="testpage2.pdf">test page 2</asp:ListItem> 
</asp:BulletedList> 

我想要做的就是在我的CS文件,我想assing以下

Sting filepath = // here i want to get the ListItem Value 
Sting filename = // here i want to get the file name present in Listitem text. 

如何在下面的按钮单击事件中获得上述两个值。

protected void DocumentList_Click(object sender, BulletedListEventArgs e) 
{ 

} 

回答

1
protected void DocumentList_Click(object sender, BulletedListEventArgs e) 
{ 
    ListItem li = DocumentList.Items[e.Index]; 
    Sting filepath = li.Value; 
    Sting filename = li.Text; 

} 

但你并不需要指定文本字段来保存文件名的值,你可以从路径得到它。

<asp:BulletedList ID="DocumentList" runat="server" 
DisplayMode="LinkButton" onclick="DocumentList_Click"> 
    <asp:ListItem Value="Documents/testpage.pdf" >test page</asp:ListItem> 
    <asp:ListItem Value="Documents/testpage2.pdf" >test page 2</asp:ListItem> 
</asp:BulletedList> 

然后

protected void DocumentList_Click(object sender, BulletedListEventArgs e) 
{ 
    ListItem li = DocumentList.Items[e.Index]; 
    Sting filepath = li.Value; 
    Sting filename = System.IO.Path.GetFileName(li.Value); 

} 
0
protected void DocumentList_Click(object sender, BulletedListEventArgs e) 
{ 
    var item = DocumentList.Items[e.Index]; 
    String filepath = item.Value; 
    String filename = item.Text; 
}