473,769 Members | 2,106 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

GridView: firing a function and passing a data value to it

I want to know how to make a clickable button or Command field on a
GridView, and have the user's action a) fire a function and b) pass a data
value from one of the GridView's columns to that function.

Long version: ASP.NET's usual paradigm with master-detail editing is to have
you define a commandfield, and on your detail view to define a second
datasource that uses the SelectedValue of the GridView to determine what the
detail record is.

I'm using an ORM framework and for whatever reason I would simply like to a)
grab the ContentID of the record selected on the GridView and b) use that
value to grab my detail view without going through SqlDataSource.

I could hack this up by just passing a QueryString parameter and posting
back to my form, but I'd prefer to know a more direct and less hack-y
approach.

Thanks for any help you can offer.

-KF
Jul 15 '06 #1
2 2445
Here's an answer to anyone that follows this thread.

You can indeed grab the values associated with GridView command functions
such as "edit" and "update" and "select", and you can handle those events
with your own custom code.

GridViews raise an event called "SelectedIndexC hanged" when different events
are selected by the user. In codebehind, you can write a function to be
called on this event:

protected void GridView1_Selec tedIndexChanged (object sender, EventArgs e)
{
EditItem();
}

The EditItem function can in turn read a property "GridView1.Sele ctedValue"
that is helpfully provided by the ASP.NET gridview, and in my case, I can
use that value to do the ORM stuff I want to do:

public void EditItem()
{
int idToEdit = Convert.ToInt32 (GridView1.Sele ctedValue);
Contentitems ci = new Contentitems();
ci.LoadByPrimar yKey(idToEdit);

.... etc.

The last step to making this work well is to make sure to rebind the data
grid after you're done doing whatever you're doing:

protected void SaveChanges_Cli ck(object sender, EventArgs e)
{
int idToEdit = Convert.ToInt32 (GridView1.Sele ctedValue);
Contentitems ci = new Contentitems();
ci.LoadByPrimar yKey(idToEdit);
ci.Con_Title = editTitle.Text;
....
ci.Save();
mv_MiddleBar.Se tActiveView(v_S preadsheetView) ;
GridView1.DataB ind();

}
Aside: I'm finishing the first really comprehensive/complicated app that
I've tried to build in ASP.NET 2, and my conclusion is that it is a very
capable, very reusuable, and very cool framework that demands a fairly high
level of skill and knowledge to use well. ASP was nice for programming
novices because you could sort of klutz around with it until it sort-of did
what you needed it to do. ASP.NET doesn't really have an effective "dummy
mode" as ASP did, and you need to be a modestly competant programmer to use
it well, especially if you want to go beyond pre-baked behaviors.

-KF
<ke*****@nospam .nospamwrote in message
news:%2******** *******@TK2MSFT NGP03.phx.gbl.. .
>I want to know how to make a clickable button or Command field on a
GridView, and have the user's action a) fire a function and b) pass a data
value from one of the GridView's columns to that function.

Long version: ASP.NET's usual paradigm with master-detail editing is to
have you define a commandfield, and on your detail view to define a second
datasource that uses the SelectedValue of the GridView to determine what
the detail record is.

I'm using an ORM framework and for whatever reason I would simply like to
a) grab the ContentID of the record selected on the GridView and b) use
that value to grab my detail view without going through SqlDataSource.

I could hack this up by just passing a QueryString parameter and posting
back to my form, but I'd prefer to know a more direct and less hack-y
approach.

Thanks for any help you can offer.

-KF

Jul 15 '06 #2
Neo
Datakeys somtime makes things easier, so you don't need to look the
racord back from primary key.
Also, if you have proper, insert, edit and update command you don't
need to write code for saving.
-Neo
ke*****@nospam. nospam wrote:
Here's an answer to anyone that follows this thread.

You can indeed grab the values associated with GridView command functions
such as "edit" and "update" and "select", and you can handle those events
with your own custom code.

GridViews raise an event called "SelectedIndexC hanged" when different events
are selected by the user. In codebehind, you can write a function to be
called on this event:

protected void GridView1_Selec tedIndexChanged (object sender, EventArgs e)
{
EditItem();
}

The EditItem function can in turn read a property "GridView1.Sele ctedValue"
that is helpfully provided by the ASP.NET gridview, and in my case, I can
use that value to do the ORM stuff I want to do:

public void EditItem()
{
int idToEdit = Convert.ToInt32 (GridView1.Sele ctedValue);
Contentitems ci = new Contentitems();
ci.LoadByPrimar yKey(idToEdit);

... etc.

The last step to making this work well is to make sure to rebind the data
grid after you're done doing whatever you're doing:

protected void SaveChanges_Cli ck(object sender, EventArgs e)
{
int idToEdit = Convert.ToInt32 (GridView1.Sele ctedValue);
Contentitems ci = new Contentitems();
ci.LoadByPrimar yKey(idToEdit);
ci.Con_Title = editTitle.Text;
...
ci.Save();
mv_MiddleBar.Se tActiveView(v_S preadsheetView) ;
GridView1.DataB ind();

}
Aside: I'm finishing the first really comprehensive/complicated app that
I've tried to build in ASP.NET 2, and my conclusion is that it is a very
capable, very reusuable, and very cool framework that demands a fairly high
level of skill and knowledge to use well. ASP was nice for programming
novices because you could sort of klutz around with it until it sort-of did
what you needed it to do. ASP.NET doesn't really have an effective "dummy
mode" as ASP did, and you need to be a modestly competant programmer to use
it well, especially if you want to go beyond pre-baked behaviors.

-KF
<ke*****@nospam .nospamwrote in message
news:%2******** *******@TK2MSFT NGP03.phx.gbl.. .
I want to know how to make a clickable button or Command field on a
GridView, and have the user's action a) fire a function and b) pass a data
value from one of the GridView's columns to that function.

Long version: ASP.NET's usual paradigm with master-detail editing is to
have you define a commandfield, and on your detail view to define a second
datasource that uses the SelectedValue of the GridView to determine what
the detail record is.

I'm using an ORM framework and for whatever reason I would simply like to
a) grab the ContentID of the record selected on the GridView and b) use
that value to grab my detail view without going through SqlDataSource.

I could hack this up by just passing a QueryString parameter and posting
back to my form, but I'd prefer to know a more direct and less hack-y
approach.

Thanks for any help you can offer.

-KF
Jul 15 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
7810
by: Stephen | last post by:
My primary GridView is firing an OnDataBound after Selecting a row (which triggers SelectedIndexChanged). This only happens in the following scenario: - User enters a textbox search value and hits "GO" - Gridview is filled with results (4 grids in all, only the "primary" is filled at this point) - User Selects a row from the gridview. - User clicks an "Add" button which calls a javascript window.showmodaldialog
28
10275
by: Tim_Mac | last post by:
hi, i'm new to .net 2.0, and am just starting to get to grips with the gridview. my page has autoEventWireUp set to true, which i gather is supposed to figure out which handlers to invoke when appropriate based on your method names . the GridView has OnRowCommand="GridView1_RowCommand" in the aspx. my problem is that the RowCommand event is firing twice (95% of the time) on the page. the other 5% it only fires once. there's no
7
5471
by: Ken | last post by:
Hi All - I have a filtered GridView. This GridView has a check box in the first column. This check box is used to identify specific rows for delete operations. On the button click event I loop through the filtered GridView to identify the selected rows and assemble some XML to be sent to a stored proc. The problem I have is that when looping through the GridView, it doesn't
0
2138
by: dawg1998 | last post by:
I am able to populate a DropDownList control within multiple rows of a GridView with the following code: `````````````````````````````````````````````````````` <asp:GridView id="gvGridView" runat="server" AutoGenerateColumns="False" DataKeyNames="idRecordID"> <Columns> <asp:TemplateField> <ItemTemplate>
6
28169
by: Kevin Attard | last post by:
I am using a GridView inside a UserControl which has a template column for deleting the rows. Before databinding the gridview i am attaching the RowCommand and RowDataBound event. I am using the RowDataBound event to set the commandargument of the delete button. The event is being fired and works fine. When I press the delete button, the RowCommand event is not firing! and neither is the RowDeleting (the button's commandName is...
13
42064
by: AG | last post by:
I have a gridview that I bind to a List(of Type) at runtime. Not using a datasource control. The gridview has a template column with an imagebutton whose commandname is set to 'Delete'. The footer template has an commandbutton with the commandname set to 'Insert'. Both buttons cause postback, however the RowCommand event does not fire. How can I get the rowcommand to fire?
8
18089
by: Greg Lyles | last post by:
Hi all, I'm trying to develop an ASP.NET 2.0 website and am running into some real problems with what I thought would be a relatively simple thing to do. In a nutshell, I'm stuck on trying to display data in a "GridView" which is tied to an "ObjectDataSource". In turn, this ObjectDatasource gets it's data from a strongly-typed business object within my code.
0
6500
by: Larry Bud | last post by:
After spending two days looking at the options in adding a row to a gridview, I came up with my own that I'd like to share. I only can share pseudo-code right now, but maybe in the future I'll write a full fledged article. I didn't like the options, such as putting your controls (really, DUPLICATING your controls) in the Footer for a variety of reasons. First, I don't like the idea of having to do things twice, including any...
3
3899
by: Dave | last post by:
I have an GridView on one page that will open a popup for the selected record. I've been told that there is a way that when the popup record is updated the GridView can be refreshed or Databind. I've been looking for the answer with no luck. I'm probably just not asking the question correctly. Thanks for any assistance.
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9997
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9865
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8873
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3565
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.