string name = ((TextBox)grid.Rows[e.RowIndex].➥
FindControl("nameTextBox")).Text;
in the above line
what does this mean?
((TextBox)grid.Rows[e.RowIndex]
string name = ((TextBox)grid.Rows[e.RowIndex].➥
FindControl("nameTextBox")).Text;
in the above line
what does this mean?
((TextBox)grid.Rows[e.RowIndex]
That is an explicit cast. Basically you are forcing the cpu to consider the control that is found on the grid to be a textbox. This is because the FindControl() function returns an object of type Control. A textbox is derived from a Control so the cast works, and is required since not every Control contains a .Text member. Hope this helps a bit :)
i didnt understand a bit :(
Are you familiar with polymorhpism, .net, or casting?
i know casting ...changing int to string
polymorphism ....overiding
Ok. I will break it down for you
grid.Rows is a collection of row objects
these objects can be referenced by their index: [e.RowIndex]
a grid row can contain a control
the control can be referenced via the FindControl(string control_name) function
the Control class is the base class for all controls, and since TextBox is a control it inherits the Control class. Because of this, the textbox can be explicitly cast out of the control referenced by the FindControl function. In C# explicit casting is done by placing the object type that you would like to cast in brackets beside the object to be casted. That is what the (TextBox) is doing. It may be easier to think about if it is spaced out into individual lines (since quite a bit is actually ocurring on that one line)
//Variable to hold the name
string name;
//Reference to the control to extract from the gridview row
Control tempControl;
//Reference to the textbox to cast from the control that is extracted from the gridview row
TextBox tempTextBox;
//Reference to the gridview row itself
GridViewRow tempRow;
//Get the row (assume e.RowIndex is defined - probably an event arg)
tempRow = grid.Rows[e.RowIndex];
//Find the control called "nameTextBox" in that gridview row
//Note that a Control class does not have a .Text property, so it must be cast into
//a textbox (since that is what the control actually is)
tempControl = tempRow.FindControl("nameTextBox");
//cast the control into a textbox explicitly
tempTextBox = (TextBox)tempControl;
//Get the name out of the textbox's text property
name = tempTextBox.Text;
It's a bit late for me to try and teach a coding lesson. Hope this helps you though.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.