I have the following working so far in C# on my Gridview called GridView1. It works when I put it in the onSelectedIndexChanged.
HostTextbox.Text = GridView1.SelectedRow.Cells[0].Text;
but since this posts back to the server I want to avoid it because I will be doing it for cells[0] to cells[10]. So, I looked into Javascript. I googled around and found various solutions and this is the one I have “semi-working” so far.
My C# looks like this:
int myRowIdx = 0; // class variable
protected void OnRowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("ondbclick", "sample('" + myRowIdx.ToString() + "')");
}
myRowIdx++;
}
In my Javascript I inserted alerts to tell me where the problem happpens. It looks like this:
function sample(rowIn) {
alert("A");
var gViewID = '<%= GridView1.ClientID %>';
alert("B");
var gView = getElementById(gViewID);
alert("C");
var gViewRow = gView.rows[rowIn];
alert("D");
var gViewRowColumn = gViewRow.cells[0];
alert("E");
var displayCell = gViewRowColumn.innerText;
alert("F");
alert(displayCell);
}
B is the last alert I see. I can’t seem to figure this out. I looked carefully into it and still no success. Please help.
I don’t understand what you mean with “I want to avoid it because I will be doing it for cells[0] to cells[10]”. You could do that for each cell in the selected row successively. So you only need one Postback.
According to your Javascript problems, you could simply pass the tr(GridViewRow) as js-variable to your sample-function. Therefore you only have to pass
thisas parameter:and in your js-function: