Getting value from a cell from a gridview on RowDataBound event

前端 未结 12 1033
[愿得一人]
[愿得一人] 2020-12-23 16:48
string percentage = e.Row.Cells[7].Text;

I am trying to do some dynamic stuff with my GridView, so I have wired up some code to the RowDataBound ev

12条回答
  •  抹茶落季
    2020-12-23 17:33

    When you use a TemplateField and bind literal text to it like you are doing, asp.net will actually insert a control FOR YOU! It gets put into a DataBoundLiteralControl. You can see this if you look in the debugger near your line of code that is getting the empty text.

    So, to access the information without changing your template to use a control, you would cast like this:

    string percentage = ((DataBoundLiteralControl)e.Row.Cells[7].Controls[0]).Text;
    

    That will get you your text!

提交回复
热议问题