Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 获取listview中标签的值_C#_Html_Asp.net_Listview - Fatal编程技术网

C# 获取listview中标签的值

C# 获取listview中标签的值,c#,html,asp.net,listview,C#,Html,Asp.net,Listview,我有一个带有不可见标签的listview来存储类别Id。 我想做的是在单击按钮时将标签的文本分配给cookie或会话。 问题是,当我尝试在listview之外显示值时,我的cookie总是空的。 这是我的aspx代码: <asp:ListView runat="server" ID="catListView" DataSourceID="CategoriesDataSource" > <EmptyDataTemplate>No DataFound&

我有一个带有不可见标签的listview来存储类别Id。 我想做的是在单击按钮时将标签的文本分配给cookie或会话。 问题是,当我尝试在listview之外显示值时,我的cookie总是空的。 这是我的aspx代码:

<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>
任何帮助都将不胜感激。 提前thx


Sam

实际上,您正试图在一个键中添加您的
cat
。我的意思是您正在覆盖循环中的值

您应该以以下方式更改代码:

在您的C#代码中:

阅读此cookie

HttpCookie exampleCookie = Request.Cookies["ExampleCookie"];
if (exampleCookie != null)
{
    int tempIndex = 0;
    foreach (ListViewItem item in catListView.Items)
    {
        Response.Write("<br/>" + exampleCookie["cat" + tempIndex.ToString()]);
        tempIndex = tempIndex + 1;
    }
}  
HttpCookie exampleCookie=Request.Cookies[“exampleCookie”];
如果(例如Cookie!=null)
{
int tempIndex=0;
foreach(catListView.Items中的ListViewItem项)
{
Response.Write(“
”+exampleCookie[“cat”+tempIndex.ToString()); tempIndex=tempIndex+1; } }
实际上,您正试图在一个键中添加您的
cat
。我的意思是您正在覆盖循环中的值

您应该以以下方式更改代码:

在您的C#代码中:

阅读此cookie

HttpCookie exampleCookie = Request.Cookies["ExampleCookie"];
if (exampleCookie != null)
{
    int tempIndex = 0;
    foreach (ListViewItem item in catListView.Items)
    {
        Response.Write("<br/>" + exampleCookie["cat" + tempIndex.ToString()]);
        tempIndex = tempIndex + 1;
    }
}  
HttpCookie exampleCookie=Request.Cookies[“exampleCookie”];
如果(例如Cookie!=null)
{
int tempIndex=0;
foreach(catListView.Items中的ListViewItem项)
{
Response.Write(“
”+exampleCookie[“cat”+tempIndex.ToString()); tempIndex=tempIndex+1; } }
调用按钮单击事件时,ListView可能没有数据绑定

我建议您使用。它最适合这种处理方式。更新ListView声明以处理该事件:

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

这还有一个优点,即不循环浏览所有ListView项目,只查看您单击的项目。

调用按钮单击事件时,ListView可能没有数据绑定

我建议您使用。它最适合这种处理方式。更新ListView声明以处理该事件:

<asp:ListView runat="server" ID="catListView" DataSourceID="CategoriesDataSource
    OnItemCommand="catListView_ItemCommand" >
这样做还有一个好处,即不必遍历所有的ListView项目,只需查看您单击的项目即可。

希望这有帮助:)

string[]catvals=新字符串[catListView.Items.Count];
对于(int i=0;i
希望这有帮助:)

string[]catvals=新字符串[catListView.Items.Count];
对于(int i=0;i
如何在
列表视图之外显示它?另外,catLabel.Text是一个字符串,不需要在它上面调用
.ToString()
)@jadarnel27,就像在aspx页面中这样:您如何尝试在
列表视图之外显示它?另外,catLabel.Text是一个字符串,不需要像在aspx页面中这样调用
.ToString()
)@jadarnel27:谢谢你的帮助,我还没有尝试代码,但我有一点意见。我不想在response.write中显示cookie,我将在数据库中的过程中将其用作参数。只想显示以确保它不为空!所以我可以在read cookie中将其分配给标签?谢谢您的帮助,我还没有尝试代码,但我只有一条注释。我不想在response.write中显示cookie,我将在数据库中的过程中将其用作参数。只想显示以确保它不为空!所以我可以把它分配到read cookie中的标签上?谢谢,我在发布问题之前考虑过这个问题。但是我有另一个OnClick事件的代码,我可以将它添加到listview吗?这似乎是个愚蠢的问题,但我以前真的没有使用Itemcommand。@萨米拉,我不明白你的意思。您不需要清除所有OnClick事件。但是如果ListView中有什么东西,您可能应该使用ItemCommand。您说过我应该从按钮中删除Onclick事件处理程序。抱歉,我认为我必须删除与ListView无关的事件代码。谢谢你的帮助,我会试试的:)@Samira哦,我明白了!谢谢你的解释。如果需要,可以从代码隐藏中删除OnClick事件。一旦你从按钮标记中删除了
OnClick=“btnTest\u Click”
,它就不会有任何作用了。谢谢@jadarnel27我只想添加一件小事:我使用了itemcond,没有删除OnClick for Button,没问题。直到现在,用会话替换cookie。Cookies有效,但会话对我更有效再次谢谢,我在发布问题之前考虑过这一点。但是我有另一个OnClick事件的代码,我可以将它添加到listview吗?这似乎是个愚蠢的问题,但我以前真的没有使用Itemcommand。@萨米拉,我不明白你的意思。您不需要清除所有OnClick事件。但是如果ListView中有什么东西,您可能应该使用ItemCommand。您说过我应该从按钮中删除Onclick事件处理程序。抱歉,我认为我必须删除与ListView无关的事件代码。谢谢你的帮助,我会试试的:)@Samira哦,我明白了!谢谢你的解释。如果需要,可以从代码隐藏中删除OnClick事件。一旦你从按钮标记中删除了
OnClick=“btnTest\u Click”
,它就不会有任何作用了。谢谢@jadarnel27我只想添加一件小事:我使用了itemcond,没有删除OnClick for Button,没问题。直到现在,用sessi取代cookie
protected void catListView_ItemCommand(object sender, ListViewCommandEventArgs e)
{
    Label catLabel = (Label)e.Item.FindControl("lblcat");
    Session["currentCatLabelText"] = catLabel.Text;
}
string[] catvals = new string[catListView.Items.Count];

            for (int i =0; i< catListView.Items.Count; i++)
            {
                Label catLabel = (Label)catListView.FindControl("lblcat");
                catvals[i] = catLabel.Text;
            }
            for(int i = 0 ; i < catvals.Length; i++)
            {
                Response.Cookies["cat"+ i.ToString()].Value = catvals[i].ToString();
            }