Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Read value from GridView in ASP.Net 1

Status
Not open for further replies.

ksbigfoot

Programmer
Apr 15, 2002
856
CA
I have a gridview on my ASP.NET page.
As I mouse over each row, I change the colour of the row background (this part is working).
What I am trying to do is as I mouse over each row, I am trying to populate a text box with a value from that row.

Here is the code I have so far:
Code:
Dim gridScript3 As String = "<script language='" & "javascript" & "'> 
function SetTextBox(row) { document.getElementById('TextBox1').value = 
document.getElementById('GridView1'); } </script> "

After I move over each row, the TextBox1 gets loaded with [Object].
Just wondering if anyone knows how to read any value out of the row of the gridview?
 
Can you post the client-side code (not the .Net code) for the grid (or the whole page, or a URL to it). Alternatively, you might find a server-side answer in the .Net forums.

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Howdy BillyRayPreachersSon,
I hope this is the code you are asking for
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head><title>
	Untitled Page
</title></head>
<body>
    <form name="form1" method="post" action="Default.aspx" id="form1">
<div>
<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="u2W0O4getjYAGSApvMH0mIjCb9xMO7g2/Ld1AX2o/OqTdMANkg+aQZXus+
65EoxgOpmATyFO+RUe4mrv06be4maN4n7qVqRTzQZ5RIajWVKp+Iitye4f6bbVBbJKQnKfT2X6
Kmwb9250CYBzsXq5YuAfEm6hQ96sbfnunoeEdkrjRHJSGNdu5a9LM+mQ23yF6gmSmkRZP3FuHF
uTJrC/TVR1m3i5rw+XsEzwSk7IBYR/NYB4u59poVI6GZcSn3JALMb725mVR9K5HQJyEWMUpywG
A9EGJ+F4v3LmBhLSfi2cattTnfSKEs2GlqxDjyAsrSCD/XWxgefNnRWcFoAiwhntnVz0JmQWj+
RKg6VWost9Gqvdlqy++PAgV7gQuqRbqEJnjsDNawL/Rdf+jKTdumuvsk3U8J8jaXtfe/uzPjVk
gVc2u8a2OqrIdmU6A/YxMmtj5Fb61oQsMxIIPESprJdS4Kx0H6NZWdw7kqyQ44LbinQG2d5aKH
fttH4CNRY4lmSUFKv0aorC4R95S9gqe3xqGtiiPtqTVMCe6u2dtAl91ucGdkbkTgp6xNLbg7DA
tZvXQ0G/jTci31aiSXZKk1yleK4JFecBKDKeNlu806p4OH/Nwx6MiHYHcrs0XRoDWA17DoOESa
RqW5f9NrQK9IHosrpv1egjuB3qonMcrfgtYGEybJwGljaHQ27Ega2yZa62mg5GIJPp3OQuqvX+
NpEmM/r5Gxf8FJB5Iunk8kc=" 
/>
</div>

<script type="text/javascript">
<!--
var theForm = document.forms['form1'];
if (!theForm) {
    theForm = document.form1;
}
function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
}
// -->
</script>


<script src="/FTP/WebResource.axd?d=JFaGdUNORmAwhwkI41zfHw2&amp;t=633000941
000000000" type="text/javascript"></script>

<script language='javascript'> function Highlight(row) { row.style.backgroundColor='AliceBlue'; } </script> <script language='javascript'> function UnHighlight(row) { row.style.backgroundColor='white'; } </script> <script language='javascript'> function SetTextBox(row) { document.getElementById('TextBox1').value = row.Text; } </script> 
<script src="/FTP/ScriptResource.axd?d=zbWmubs25WhXqcdiCK7wHErerbl1pcnNYfB63W7Bpcj
nUGnUwR3csBZZmMaUYDGWGft4ZoESadcyDM_Zaa5iJbx2aEMhX3NY8AnjH9KgAhM1&amp;t=63
3141630060000000" type="text/javascript"></script>
<script src="/FTP/ScriptResource.axd?d=zbWmubs25WhXqcdiCK7wHErerbl1pcnNYfB63W7Bpcj
nUGnUwR3csBZZmMaUYDGWGft4ZoESadcyDM_Zaa5iJR4ZjCCUV8z5xLEewk8O8h6FCmb0PN402
EdWfvc2tD440&amp;t=633141630060000000" type="text/javascript"></script>
        <script type="text/javascript">
//<![CDATA[
Sys.WebForms.PageRequestManager._initialize('ScriptManager1', document.getElementById('form1'));
Sys.WebForms.PageRequestManager.getInstance()._updateControls([], [], [], 90);
//]]>
</script>

        <div>
            <div>
	<table cellspacing="0" cellpadding="4" border="0" id="GridView1" style="color:#333333;border-collapse:collapse;">
		<tr style="color:White;background-color:#507CD1;font-weight:bold;">
			<th scope="col">&nbsp;</th><th scope="col">ContactID</th><th scope="col">FirstName</th><th scope="col">LastName</th><th scope="col">Phone</th>
		</tr><tr onMouseOver="SetTextBox(this)" onMouseOut="UnHighlight(this)" style="background-color:#EFF3FB;">
			<td><a href="javascript:__doPostBack('GridView1','Select$0')" style="color:#333333;">Select</a></td><td>2</td><td>Karen</td><td>Hilton
</td><td>403-555-5555</td>
		</tr><tr onMouseOver="SetTextBox(this)" onMouseOut="UnHighlight(this)" style="background-color:White;">
			<td><a href="javascript:__doPostBack('GridView1','Select$1')" style="color:#333333;">Select</a></td><td>4</td><td>Hank</td><td>Aaron
</td><td>999-999-9999</td>
		</tr><tr onMouseOver="SetTextBox(this)" onMouseOut="UnHighlight(this)" style="background-color:#EFF3FB;">
			<td><a href="javascript:__doPostBack('GridView1','Select$2')" style="color:#333333;">Select</a></td><td>5</td><td>Timmy</td><td>Allen
</td><td>&nbsp;</td>
		</tr>
	</table>
</div>
            
            &nbsp;
            <br />
            <input name="TextBox1" type="text" id="TextBox1" />
            <br />
            <input name="TextBox2" type="text" id="TextBox2" /></div>
    
<div>

	<input type="hidden" name="__VIEWSTATEENCRYPTED" id="__VIEWSTATEENCRYPTED" value="" />
	<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="WAZ/neahGAmGMSC8cDWF6UsQ1Lkn1gd94SX4S5MLOUNzuZ9NBLy0OuLsYU
nAVxx7" />
</div>

<script type="text/javascript">
<!--
Sys.Application.initialize();
// -->
</script>
</form>
</body>
</html>
 
>function SetTextBox(row) { document.getElementById('TextBox1').value = row.Text; }

You haven't specified which column data you want to reflect in the text box. Let's suppose it is the FirstName, for instance. You can do it at least in two ways.

[1] Table object model
[tt]
function SetTextBox(row) { document.getElementById('TextBox1').value = row.[blue]cells[2].innerHTML[/blue]; }
[/tt]
[2] DOM (in general)
[tt]
function SetTextBox(row) { document.getElementById('TextBox1').value = row.[blue]childNodes[2].innerHTML[/blue]; }
[/tt]
Using TOM would be slightly preferrable as it picks up less noise on the concrete implementations of the table. (It is an accident that the index [2] coincides in both methods.) If the data copied to the textbox contain a combination of cells' data, it suffices to modify the rhs accordingly.
 
Howdy tsuji,
That is exactly what I was looking for.
Star to you.
Thanks again,
ksbigfoot
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top