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

G

Guest

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
 
G

Guest

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 "SelectedIndexChanged" when different events
are selected by the user. In codebehind, you can write a function to be
called on this event:

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

The EditItem function can in turn read a property "GridView1.SelectedValue"
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.SelectedValue);
Contentitems ci = new Contentitems();
ci.LoadByPrimaryKey(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_Click(object sender, EventArgs e)
{
int idToEdit = Convert.ToInt32(GridView1.SelectedValue);
Contentitems ci = new Contentitems();
ci.LoadByPrimaryKey(idToEdit);
ci.Con_Title = editTitle.Text;
....
ci.Save();
mv_MiddleBar.SetActiveView(v_SpreadsheetView);
GridView1.DataBind();

}
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
 
N

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top