Subscribe to Subscribers

Forums » .NET » ASP.NET »

Get boundfield value in javascript


Posted Date: 12 Jul 2012      Posted By:: swapnali     Member Level: Bronze    Member Rank: 2222     Points: 5   Responses: 3



want to get bound column values using javascript

<asp:DataGrid ID="grdRemark" runat="server" >
<Columns>
<asp:BoundColumn DataField="Remark Code" HeaderText="Code" >
<asp:TemplateColumn HeaderText="Remark">
<HeaderStyle CssClass="tdtesthead" BackColor="#D5E5FF"></HeaderStyle>
<ItemStyle CssClass="tdtest"></ItemStyle>
<ItemTemplate>
<asp:LinkButton ID="lnkRemarkView" ForeColor="#0045BD" runat="server" CommandName="RemarkDetails" ></asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn Visible="false" DataField="Remark" ></asp:BoundColumn>
</asp:DataGrid>
i want to use this bound field value using javascript
please suggest.....




Responses

#680098    Author: Mohan M Devarinti      Member Level: Gold      Member Rank: 489     Date: 12/Jul/2012   Rating: 2 out of 52 out of 5     Points: 4

Hi,

Check this sample: I am reading all data from gridview through javascript function:

in .aspx:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
function ReadGridviewBoundData() {
var str = '';
var ObjGrid = document.getElementById('<%= GridView2.ClientID %>');
for (var row = 1; row < ObjGrid.rows.length; row++) {
for (var col = 0; col < ObjGrid.rows[row].cells.length; col++) {
if (col == 0)

if (document.all)

str = str + ObjGrid.rows[row].cells[col].innerText;
else
str = str + ObjGrid.rows[row].cells[col].textContent;
else
if (document.all)

str = str + '--' + ObjGrid.rows[row].cells[col].innerText;

else

str = str + '--' + ObjGrid.rows[row].cells[col].textContent;

}
str = str + '\n';

}
alert(str);
return false;

}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>

<asp:GridView ID="GridView2" AutoGenerateColumns="False" runat="server">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Column1" />
<asp:TemplateField HeaderText="Rmark">
<HeaderStyle CssClass="tdtesthead" BackColor="#D5E5FF"></HeaderStyle>
<ItemStyle CssClass="tdtest"></ItemStyle>
<ItemTemplate>
<asp:LinkButton ID="lnkRemarkView" ForeColor="#0045BD" runat="server" OnClientClick="ReadGridviewBoundData();" CommandName="RemarkDetails">Test</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Position" Visible="false" HeaderText="Column2" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>


-------------------------------

in aspx.cs

SqlConnection dbCon = new SqlConnection("Data Source = .; Initial Catalog=ContosoHR; Integrated Security=True");
SqlCommand dbcmd = new SqlCommand("Select * from Employees", dbCon);
DataSet dSet = new DataSet();
SqlDataAdapter dbAdp = new SqlDataAdapter(dbcmd);
dbAdp.Fill(dSet);
GridView2.DataSource = dSet;
GridView2.DataBind();


I hope you can get idea about, how to read gridview databound values through javascript


 
#680110    Author: swapnali      Member Level: Bronze      Member Rank: 2222     Date: 12/Jul/2012   Rating: 2 out of 52 out of 5     Points: 1

<asp:BoundColumn Visible="false" DataField="Remark" ></asp:BoundColumn>
i just want to get above col value using javascript.have used the above function but getting all cols value.please specify to get value of the 2 col boundfield..


 
#680313    Author: Ajatshatru Upadhyay      Member Level: Gold      Member Rank: 17     Date: 13/Jul/2012   Rating: 2 out of 52 out of 5     Points: 4

Hi,

Any control with "Visible" property set to "false" from server side, does not get rendered in HTML.
Since you set "Visible" property of the "BoundField" from server side, the output page which you see in browser would not have that field included. So you will never be able to get the value since the bound field is not there.
The solution is, hide the column using CSS, as:


<style type="text/css">
.hideBoundField
{
display:none;
}
</style>

<asp:BoundColumn DataField="Remark" ItemStyle-CssClass="hideBoundField"
></asp:BoundColumn>


Now your page will have the above column but it will get hide.
Coming to the actual problem, you can read the column value through javaScript as:


<script language="javascript" type="text/javascript">
function read() {
var gridview = document.getElementById('<%= GridView1.ClientID%>');

if (gridview != null) {
var data = gridview.rows[1].cells[1].innerText;
alert(data);
}
return false;
}
</script>

<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="return read()" />


Put the button outside of gridview.
Observer the below line in above code:


var data = gridview.rows[1].cells[1].innerText;


It will read the second row's second column. You need to give the right row and column index number.

Caution: I only check the above code on IE 9. Can't say about other versions of IE or any other browser.

Please let me know if you have any doubts.

Hope it'll help you.
Regards
Ajatshatru





 
Post Reply

 This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.



Next : How to concane two dropdown?
Previous : How to send a one webform listbox value to another webform in asp.net
Return to Discussion Forum
Post New Message
Category:

Related Messages

Awards & Gifts
Talk to Webmaster Tony John
Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
2005 - 2013 All Rights Reserved.
.NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
Articles, tutorials and all other content offered here is for educational purpose only.
We are not associated with Microsoft or its partners.