row.Cells[4].Text returns nothing in GridView1_SelectedIndexChanged?

后端 未结 3 1221
难免孤独
难免孤独 2021-01-22 02:05

I have a GridView in my page :



        
相关标签:
3条回答
  • 2021-01-22 02:22

    There is no Text as your data is stored in controls. You need to search for the control you want and pull the Text out of.

    Eg:

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
        GridViewRow row = GridView1.SelectedRow; 
        string temp = row.Cells[4].FindControl('LabelGridViewBankName').Text; 
        LabelResult.Text = temp; 
    }
    

    Optionally you could could key off the DataKey property if you are using it and then use it to search your datasource for the value you want.

    0 讨论(0)
  • 2021-01-22 02:26
    //this will give you control over the textbox object
    var field = (TextBox)(GridView1.Rows[e.RowIndex].FindControl("your_control_ID"));
    //and here you can access the text
    string temp = field.Text;
    
    0 讨论(0)
  • 2021-01-22 02:48

    If it's a TemplateField, you have to use the FindControl("control ID") option, not the text fo the cell. I count cell 4 as being a templatefield...

    0 讨论(0)
提交回复
热议问题