C# 如何从gridview模板字段获取标签控件值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10903833/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 15:42:53  来源:igfitidea点击:

How to get label control value from gridview templatefield

c#asp.net

提问by Jax

I have a gridview and sqldatasource.

我有一个 gridview 和 sqldatasource。

I have a templatefield column and below the textbox of edit item template I have a simple label .

我有一个模板字段列,在编辑项模板的文本框下方有一个简单的标签。

Is there any way of getting the label value in ROW UPDATING event?

有没有办法在 ROW UPDATING 事件中获取标签值?

I've tried : String llbl = ((Label)row.Cells[6].FindControl("label1")).Text;Hmm I think it shouldnt be cells[6]since I want to get the label .

我试过:String llbl = ((Label)row.Cells[6].FindControl("label1")).Text;嗯,我认为不应该,cells[6]因为我想获得标签。

Thanks

谢谢

采纳答案by Limey

I think this is what you are looking for:

我认为这就是你要找的:

DataRow row = ((DataRowView)e.Row.DataItem).Row;

Label Mylabel = (Label)e.Row.FindControl("lblName");

Edit:

编辑:

OK, try this then:

好的,然后试试这个:

GridViewRow row = MyGrid.Rows[e.RowIndex];
Label MyLabel = (Label)row.FindControl("MyLabel");

回答by k0stya

You can use DataGridItemEventArgs to find the label. Here is the code for UpdateCommand:

您可以使用 DataGridItemEventArgs 来查找标签。这是 UpdateCommand 的代码:

 private void grid_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
          Label label1 = (Label) e.Item.FindControl("label1");
    }

回答by San

This worked for me in RowEditing:

这在 RowEditing 中对我有用:

Label label1 = (Label)GridViewID.Rows[e.NewEditIndex].FindControl("label1");
string label1val = label1.Text;

I believe this is what you want for RowUpdating:

我相信这就是你想要的 RowUpdating:

Label label1 = (Label)GridViewID.Rows[e.RowIndex].FindControl("label1");
string label1val = label1.Text;

回答by bhargav

Try this It's always works for me

试试这个它总是对我有用

string lbl =((Label)gvEmployee.Rows[rowNo].Cells[0].FindControl("lblEmpid")).Text;