简体   繁体   English

获取ASP.net C#中Gridview中的列的值/文本以显示在标签中

[英]Get the value/text of a column in a Gridview in ASP.net C# to display in a label

I have a gridview that has a list of Salesman names. 我有一个gridview ,其中有一个推销员姓名列表。 When a Salesman is selected using A ButtonFieldTemplate in the Grid . 当使用GridButtonFieldTemplate选择推销员时。 I need the name of selected Salesman to appear in a Label . 我需要选定的业务员的姓名出现在Label

So far I have this but it is not working: 到目前为止,我有这个,但是它不起作用:

protected void gvSalesmanByManager_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    lblSalesmanCustomers.Text = gvSalesmanByManager.SelectedValue + "'s Customers"; 
} 

It's not throwing any errors. 它不会引发任何错误。 Not even red squiggly lines in Visual Studio. 在Visual Studio中甚至没有红色的波浪线。 It's just plain not working. 简直是行不通。

How can I accomplish this in ASP.net and C# 4.0. 如何在ASP.net和C#4.0中完成此操作。

Regarding to the documentation SelectedValue gets the data key value of the selected row in a GridView control. 关于文档, SelectedValue获取GridView控件中所选行的数据键值。 Does your GridView contain a column with the key and did you set the dataKeyNamesProperty (such as datakeynames="myID")? 您的GridView是否包含带有键的列,并且您是否设置了dataKeyNamesProperty(例如datakeynames =“ myID”)?

EDIT: To access a value of a column you can use SelectedRow: 编辑:要访问列的值,可以使用SelectedRow:

GridViewRow row = GridView1.SelectedRow;
lblSalesmanCustomers.Text = row.Cells[2].Text;

EDIT2: There are two options when you want to read the template field. EDIT2:当您要阅读模板字段时,有两个选项。 Either you store your value in an additional invisible column or you access the controls inside the template field. 您可以将值存储在其他不可见列中,也可以访问模板字段中的控件。 Something like this should work: 这样的事情应该起作用:

lblSalesmanCustomers.Text = ((TextBox)row.Cells[2].FindContol("tbxName")).Text;

Make sure you are not assigning empty text on page load event. 确保您没有在页面加载事件中分配空文本。 or you need to place those initialization in if(!Page.IsPostback) block 或者您需要将这些初始化放置在if(!Page.IsPostback)块中

My code so far : protected void gvSalesmanByManager_SelectedIndexChanged(object sender, EventArgs e) { lblSalesmanCustomers.Text = gvSalesmanByManager.SelectedValue + "'s Customers"; 到目前为止,我的代码是:protected void gvSalesmanByManager_SelectedIndexChanged(object sender,EventArgs e){lblSalesmanCustomers.Text = gvSalesmanByManager.SelectedValue +“'s Customers”; } – } –

try in the event "gvSalesmanByManager_SelectedIndexChanged" following code: 请尝试在事件“ gvSalesmanByManager_SelectedIndexChanged”中输入以下代码:

lblSalesmanCustomers.Text = gvSalesmanByManager.SelectedRow.Cells[0].Text; lblSalesmanCustomers.Text = gvSalesmanByManager.SelectedRow.Cells [0] .Text;

You said something about a "ButtonFieldTemplate" please show me some code from your ascx-file this will help us much i think. 您说过有关“ ButtonFieldTemplate”的内容,请向我展示您的ascx文件中的一些代码,这对我有很大帮助。

*Edit: "Controls[1]" has not to be right. *编辑:“ Controls [1]”不一定正确。 But 0 is a literal. 但是0是一个文字。 you could also debug your code and set breakpoints. 您还可以调试代码并设置断点。 would be much easier. 会容易得多。 try this with Linkbutton: LinkButton lnk1 = gvSalesmanByManager.SelectedRow.Cells[0].Controls[1] as LinkButton; 尝试使用Linkbutton:LinkBut​​ton lnk1 = gvSalesmanByManager.SelectedRow.Cells [0] .Controls [1]作为LinkBut​​ton; lblSalesmanCustomers.Text = lnk1.Text; lblSalesmanCustomers.Text = lnk1.Text;

best regards, noone 最好的问候,没人

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

相关问题 在RowUpdating(ASP.NET GridView)C#中获取GridView中的旧标签值 - Get old value of label inside GridView during RowUpdating (ASP.NET GridView) C# 在GridView(ASP.NET/C#)中的DataBinding中设置标签文本 - Setting label text in DataBinding in a GridView (ASP.NET/C#) 使用asp.net C#中的javascript从具有单选按钮的gridview列中获取值 - Get value from gridview column that has radio button using javascript in asp.net c# 尝试从C#(ASP.NET gridview)中的文本框获取文本值时出错 - Error while trying to get text value from textbox in C# (ASP.NET gridview) 如何检索SQL Server表的列值并将其存储在label.C#ASP.Net中的文本 - How to retrieve column value of sql server table and store it in label.Text of c# ASP.Net 如何对 GridView 中的一列求和,然后在 ASP.Net 中显示到 label? - How to sum a column in GridView then display to label in ASP.Net? Asp.net C# 高亮匹配 Gridview label text from textbox multiple words - Asp.net C# Highlight matching Gridview label text from textbox multiple words ASP.NET如何使用C#代码中的类在GridView中向标签添加文本? - ASP.NET how to add text to label in gridview using a class in C# code? 从GridView ASP.NET C#外部的按钮更改标签文本 - Change label text from the button outside GridView asp.net c# 下拉菜单显示文本作为值的变化asp.net c# - dropdown display text as variation of value asp.net c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM