473,385 Members | 1,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Button in a GridView row

8
Hi.
I have read a previous post in the subject, but could not find an answer, so my question is how do I find the SelectedIndex of a row in a gridview row where there is a command button ?

The set up is as follows :

I have a gridview with a title column and a button column (template column). I want the button click event to do something when the title column in the row of the button that is clicked is not empty.

Many Thanks ...
Oct 17 '07 #1
5 4376
Plater
7,872 Expert 4TB
Do the buttons get their own unique id? (like "button1", "button2", "button3")?
If so, in the event function there should be a "sneder" object, you should be able to use that to determine which button was clicked. And possibly determine the row that has that button?
i.e. if button0 is clicked you know row 0 is the row you want.
Oct 17 '07 #2
obs
8
Do the buttons get their own unique id? (like "button1", "button2", "button3")?
If so, in the event function there should be a "sneder" object, you should be able to use that to determine which button was clicked. And possibly determine the row that has that button?
i.e. if button0 is clicked you know row 0 is the row you want.
the buttons do not have a unique id. it is set as a template, where each button has the same id : "addButton". how do I give a button in a gridview row a unique id ? is it even possible ?
Oct 17 '07 #3
nateraaaa
663 Expert 512MB
You can loop through the rows of the gridview and use the FindControl method to find the button you want to alter using the id you set in the TemplateField.

for (int i = 0; i < gvResults.Rows.Count; i++)
{
Button btn = (Button)GridView1.Rows[i].FindControl("btnAdd");
//Change the property settings for the button in your TemplateField
btn.Visible = false;
}

Hope this helps.

Nathan
Oct 17 '07 #4
obs
8
You can loop through the rows of the gridview and use the FindControl method to find the button you want to alter using the id you set in the TemplateField.

for (int i = 0; i < gvResults.Rows.Count; i++)
{
Button btn = (Button)GridView1.Rows[i].FindControl("btnAdd");
//Change the property settings for the button in your TemplateField
btn.Visible = false;
}

Hope this helps.

Nathan
Dear Nathan,
thanks for the reply, but how can I use your code to perform certain actions when the button is clicked ?
The "btnAdd" ID is the same for all the buttons in all the rows, since I'm using a template.
I need is to use a certain Select property (like SelectedIndex) to retrieve the row of the clicked button, and to put all the code in the button on click event.
But the following code, which I put in the on click event, does not work :

Dim grd As GridView = FormView1.FindControl("myGridView")
Dim row As GridViewRow = grd.Rows(grd.SelectedIndex)
Dim str As String = row.cells(column_index).Text.ToString()

What's wrong with it ?
Oct 17 '07 #5
nateraaaa
663 Expert 512MB
Dear Nathan,
thanks for the reply, but how can I use your code to perform certain actions when the button is clicked ?
The "btnAdd" ID is the same for all the buttons in all the rows, since I'm using a template.
I need is to use a certain Select property (like SelectedIndex) to retrieve the row of the clicked button, and to put all the code in the button on click event.
But the following code, which I put in the on click event, does not work :

Dim grd As GridView = FormView1.FindControl("myGridView")
Dim row As GridViewRow = grd.Rows(grd.SelectedIndex)
Dim str As String = row.cells(column_index).Text.ToString()

What's wrong with it ?[/quote]

Try this.
Add a CommandName to your button like CommandName = "Add".
Then in the RowCommand event use an if statement to see if the CommandName is = to "Add".
If it is use your code above

Like this
Expand|Select|Wrap|Line Numbers
  1. protected void myGridView_RowCommand(object sender, GridViewCommandEventArgs e)
  2. {
  3. if(e.CommandName == "Add")
  4. {
  5. GridView grd = FormView1.FindControl("myGridView");
  6. GridViewRow row = grd.Rows[grd.SelectedIndex];
  7. string str = row.cells[column_index].Text.ToString();
  8. }
  9. }
Let me know if this works for you. If you need a conversion tool you can use this site.

Nathan
Oct 17 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: NateDawg | last post by:
I'm reposting this. I'm kinda in a bind untill i get this figured out, so if anyone has some input it would sure help me out. Ok, I’ve noticed a few gridview problems floating around the forum....
1
by: jobs | last post by:
Given the Gridview below: When I select the button "Rate" and fall into this code. How can I reference "carriercode" and others . Apparently selecting the button does not select the row. I've...
3
by: =?Utf-8?B?c2xlbWVu?= | last post by:
Hi, My GridView is bound to a DataTable and the GridView properties are set as follows: AutoGenerateColumns="true" AutoGenerateEditButton="true" AutoGenerateDeleteButton="true" The Edit and...
0
by: Eraser | last post by:
Hi to all .NET guru guys... I have a problem in my delete button inside gridview. How to avoid postback on when i select cancel on confirmation message? But postback is okay on Ok confirmation....
4
by: glbdev | last post by:
Hi, I posted this question yesterday but didn't get the answer I needed. I am DESPERATE to get this working so I'm re-posting it because I don't think I worded it correctly. I have a GridView...
2
by: shapper | last post by:
Hello, I am working with a ListView but I suppose that with a GridView might be the same. Instead of having an Insert Button on each GridView row I would like to have only one Insert button,...
4
by: mohaaron | last post by:
This seems like it should be simple to do but for some reason I have been unable to make it work. I would like to databind a SqlDataSource to a GridView during the click event of a button. This...
0
by: ADN | last post by:
Hi, I am currently extending the GridView control and would like to add a button to the GridView so that it will automatically render one button at the top of the grid. I have a click event for...
4
by: Ken Fine | last post by:
I'm making an administrative interface that lists records in a GridView. For *each* row in the gridview, I would there to be two interface elements in addition to some information associated with...
23
by: shashi shekhar singh | last post by:
Dear sir, I have a word document file contains text and images, now i have saved it as a web page and wants to display it on browser , using , string str=directory.getfiles("");...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.