[英]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
. 当使用
Grid
的ButtonFieldTemplate
选择推销员时。 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:LinkButton lnk1 = gvSalesmanByManager.SelectedRow.Cells [0] .Controls [1]作为LinkButton; lblSalesmanCustomers.Text = lnk1.Text;
lblSalesmanCustomers.Text = lnk1.Text;
best regards, noone 最好的问候,没人
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.