Hi,
How can i make a bound field as the select button for selecting the row? For example when i click on the company name which is a field in gridview the respective row should get selected.. How to do that and even the selected row color should change.. How to do that as i do not want to autogenerate the select linkbutton .
Loading


NarayanRank #8,477 · 221 reputationPosted Oct 13, 2011, 7:54 PM EDT
TulasiRank #2,544 · 549 reputationPosted Oct 12, 2011, 12:46 AM EDT
chk this i used checkbox to change color
//Javascript
function ChangeRowColor(obj) {
var rowobj = getParentRow(obj);
if (obj.checked)
{
rowobj.style.backgroundColor = '#cccccc';
}
else
{
rowobj.style.backgroundColor = 'white';
}
}
Gridview :
Please do not forget to mark "Accpeted Answer".
B M SuchitraRank #2,922 · 492 reputationPosted Oct 12, 2011, 1:32 AM EDT
Your code does not work for me. I have a link button in gridview on clicking it row color should change.
TulasiRank #2,544 · 549 reputationPosted Oct 12, 2011, 1:41 AM EDT
Could u tel me whether u wan to change color by using javascript or codebehind ?
B M SuchitraRank #2,922 · 492 reputationPosted Oct 12, 2011, 1:51 AM EDT
Either ways is ok for me
TulasiRank #2,544 · 549 reputationPosted Oct 12, 2011, 2:15 AM EDT
have a look at these links
http://www.geekzilla.co.uk/view9FC28EE6-ACB0-4F51-BFE4-38B0B10134D5.htm
http://www.daniweb.com/web-development/aspnet/threads/202708
Please do not forget to mark "Accpeted Answer".
TulasiRank #2,544 · 549 reputationPosted Oct 12, 2011, 2:26 AM EDT
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { int rowIndex = Convert.ToInt32(e.CommandArgument); foreach (GridViewRow row in GridView1.Rows) { row.BackColor = row.RowIndex.Equals(rowIndex) ? System.Drawing.Color.Red : System.Drawing.Color.White; } }B M SuchitraRank #2,922 · 492 reputationPosted Oct 12, 2011, 10:33 PM EDT
Can we change the row backcolor at the gridview linkbutton click event? How to do that? Please help
B M SuchitraRank #2,922 · 492 reputationPosted Oct 12, 2011, 11:02 PM EDT
This link:
http://www.daniweb.com/web-development/aspnet/threads/202708 Allows for multiple row selection... I want user to make only 1 row selection at a time
TulasiRank #2,544 · 549 reputationPosted Oct 12, 2011, 11:07 PM EDT
i done one sample example for u , chk this
Gridview source code
Link button click event
Please do not forget to mark "Accpeted Answer".
TulasiRank #2,544 · 549 reputationPosted Oct 12, 2011, 11:15 PM EDT
B M SuchitraRank #2,922 · 492 reputationPosted Oct 12, 2011, 11:16 PM EDT
Thats ok but that is not my requirement.. Actually that javascript which is below :
"javascript" type="text/javascript"> document.body.style.cursor = 'pointer'; var oldColor = ''; function ChangeRowColor(rowID) { var color = document.getElementById(rowID).style.backgroundColor; if(color != 'yellow') oldColor = color; if(color == 'yellow') document.getElementById(rowID).style.backgroundColor = oldColor; else document.getElementById(rowID).style.backgroundColor = 'yellow'; }Prabhu RajaRank #861 · 2.6K reputationPosted Oct 13, 2011, 1:45 AM EDT
B M SuchitraRank #2,922 · 492 reputationPosted Oct 13, 2011, 2:23 AM EDT
I get the below error:
An object reference is required for the non-static field, method, or property 'System.Web.UI.Control.NamingContainer.get'
Source Error:
Line 52: { Line 53: LinkButton btn = (LinkButton)sender; Line 54: GridViewRow gvr = (GridViewRow)LinkButton.NamingContainer; Line 55: int rowindex = gvr.RowIndex; Line 56: GVSearch.Rows[rowindex].Attributes.Add("onclick", "javascript:ChangeRowColor('" & GVSearch.Rows[rowindex].ClientID & "')");Prabhu RajaRank #861 · 2.6K reputationPosted Oct 13, 2011, 3:41 AM EDT
B M SuchitraRank #2,922 · 492 reputationPosted Oct 13, 2011, 7:38 PM EDT
It is still not working as it has to work[I mean color is not changing at linkbutton click].. I hope you are understanding my problem. I want row backcolor to change at linkbutton click and allow only 1 row selection at a time.. How can i do that please help..
B M SuchitraRank #2,922 · 492 reputationPosted Oct 13, 2011, 8:21 PM EDT
I do have the knowledge about that select but i do not want to do it like that. I have a templated field which is having a linkbutton.. At the click event of that linkbutton i want to change the color of the row. I also want to allow user to make only 1 selection at a time. How to do that?
My requirement is that i do not want the select button
NarayanRank #8,477 · 221 reputationPosted Oct 13, 2011, 9:52 PM EDT
Please go through the aspx and .cs file.
Prabhu RajaRank #861 · 2.6K reputationPosted Oct 13, 2011, 9:58 PM EDT
He used LinkButton inside of Template Field. the Text of link button is only "Select" but it actually a link button. and he used linkbutton event handler.
Go through the Code in .aspx. and .cs files
B M SuchitraRank #2,922 · 492 reputationPosted Oct 13, 2011, 10:44 PM EDT
I am sorry. Your answer is correct. Actually i got confused because of linkbutton text which you have given as select.. That made me think that it is autogenerated select button.. thanks for the answer
Prabhu RajaRank #861 · 2.6K reputationPosted Oct 13, 2011, 11:16 PM EDT
B M SuchitraRank #2,922 · 492 reputationPosted Nov 27, 2011, 7:36 PM EST
I have one more doubt. In the solution attachment what you have provided..
If i want to fetch the Emp Name when i select a row how do i get that?