获取标签的列表视图中的价值 [英] Get the value of Label in listview

查看:93
本文介绍了获取标签的列表视图中的价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有隐形标签列表视图存储类别ID。
我想要做的是标签的文本分配到按钮点击一个cookie或会话。
问题是我的饼干总是空当我尝试显示列表视图之外的价值。
这里是我的aspx code:

I have a listview with invisible label to store the category Id. what I want to do is to assign the text of the label to a cookie or session on button click. the problem is my cookie is always null when I try to display the value outside the listview. here is my aspx code:

<asp:ListView runat="server" ID="catListView" DataSourceID="CategoriesDataSource" >
            <EmptyDataTemplate>No DataFound</EmptyDataTemplate>
            <ItemTemplate>
                <div class="service" style="margin-bottom:10px;width:230px;">               
                     <h4 style="font-family:Corbel;" ><%#Eval("CatName") %></h4>
                    <asp:Label runat="server" Visible="false" ID="lblcat"><%#Eval("CatId") %></asp:Label>
                    <asp:Button runat="server" ID="btnTest" Text="View Items" OnClick="btnTest_Click" />
                </div>
            </ItemTemplate>
        </asp:ListView>

我的C#code:

my C# code:

protected void btnTest_Click(object sender, EventArgs e)
        {
            Response.Cookies["cat"].Value = "test";
            foreach (ListViewItem item in catListView.Items)
            {
                Label catLabel = (Label)item.FindControl("lblcat");
                Response.Cookies["cat"].Value = catLabel.Text.ToString();
            }

        }

任何帮助将AP preciated。
THX提前

any help will be appreciated. thx in advance

山姆

推荐答案

这听起来像时,就是所谓的按钮点击事件的ListView你可能没有数据绑定。

It sounds like your ListView might not be databound when your Button click event is called.

我会建议你使用<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listview.itemcommand%28v=vs.110%29.aspx?cs-save-lang=1&cs-lang=csharp#$c$c-snippet-3\"相对=nofollow> ItemCommand事件处理程序。它是最适合于这种类型的处理。更新您的ListView声明处理该事件:

I would recommend that you use the ItemCommand event handler. It is most appropriate for this type of handling. Update your ListView declaration to handle that event:

<asp:ListView runat="server" ID="catListView" DataSourceID="CategoriesDataSource
    OnItemCommand="catListView_ItemCommand" >

注意:不要忘了从按钮删除的OnClick事件处理程序

,然后写你的,甚至处理code是这样的:

And then write your even handler code like this:

protected void catListView_ItemCommand(object sender, ListViewCommandEventArgs e)
{
    Label catLabel = (Label)e.Item.FindControl("lblcat");
    Session["currentCatLabelText"] = catLabel.Text;
}

这也有通过你所有的ListView项目不循环,只是看你点击的人的优势。

This also has the advantage of not looping through all your ListView items, but just looking at the one you clicked.

这篇关于获取标签的列表视图中的价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆