Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > How can I find the datakey of currently selected row?

Reply
Thread Tools

How can I find the datakey of currently selected row?

 
 
needin4mation@gmail.com
Guest
Posts: n/a
 
      10-16-2006
I searched for this and found an answer, but it did not help. In my
GridView there is a fileupload and a button click event to save the
file upload. I am naming the file uploaded after the primary key field
of my table, so, if I have employeeid 4, then my file name is 4.jpg.

What I cannot do is reference that current employeeid. If click edit,
my edittemplate show and my fileupload and button are visible. In that
buttons event, I need the employee id of that row.

I'm not sure how to get that.

Thank you for any help.

 
Reply With Quote
 
 
 
 
Mark E. Hansen
Guest
Posts: n/a
 
      10-16-2006
On 10/16/06 14:32, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> I searched for this and found an answer, but it did not help. In my
> GridView there is a fileupload and a button click event to save the
> file upload. I am naming the file uploaded after the primary key field
> of my table, so, if I have employeeid 4, then my file name is 4.jpg.
>
> What I cannot do is reference that current employeeid. If click edit,
> my edittemplate show and my fileupload and button are visible. In that
> buttons event, I need the employee id of that row.
>
> I'm not sure how to get that.
>
> Thank you for any help.
>


If you have the current selected index (GridView1.SelectedIndex) you can
do this:

GridView1.Rows[GridView1.SelectedIndex].Cells[1].Text;

Where '1' is the 0-based index of the grid view cell that contains your
column.

There may be a way to access the desired column by column name, but
I'm new to ASP.NET and haven't found a way to do that yet. If anyone
knows how, I would sure like to know
 
Reply With Quote
 
 
 
 
Daniel Yafac
Guest
Posts: n/a
 
      10-16-2006
Hi from Lima - Perú

Dim D1 As DataKey, CadSQL As String

D1 = GridView1.SelectedDataKey

CadSQL = D1("employeeid")

good luck

<(E-Mail Removed)> escribió en el mensaje
news:(E-Mail Removed) oups.com...
>I searched for this and found an answer, but it did not help. In my
> GridView there is a fileupload and a button click event to save the
> file upload. I am naming the file uploaded after the primary key field
> of my table, so, if I have employeeid 4, then my file name is 4.jpg.
>
> What I cannot do is reference that current employeeid. If click edit,
> my edittemplate show and my fileupload and button are visible. In that
> buttons event, I need the employee id of that row.
>
> I'm not sure how to get that.
>
> Thank you for any help.
>



 
Reply With Quote
 
needin4mation@gmail.com
Guest
Posts: n/a
 
      10-17-2006
I actually tried this yesterday before my post, but I keep getting an
object reference error:

DataKey d1 = null;
string empID = null;

d1 = GridView1.SelectedDataKey;
empID = Convert.ToString(d1["EmployeeID"]);

Response.Write("Empid: " + empID);

EmployeeID is indeed one of the datakeynames in the grid. I cannot
figure out why I get

Object reference not set to an instance of an object.

Thanks for any help.

Daniel Yafac wrote:
> Hi from Lima - Perú
>
> Dim D1 As DataKey, CadSQL As String
>
> D1 = GridView1.SelectedDataKey
>
> CadSQL = D1("employeeid")
>
> good luck
>
> <(E-Mail Removed)> escribió en el mensaje
> news:(E-Mail Removed) oups.com...
> >I searched for this and found an answer, but it did not help. In my
> > GridView there is a fileupload and a button click event to save the
> > file upload. I am naming the file uploaded after the primary key field
> > of my table, so, if I have employeeid 4, then my file name is 4.jpg.
> >
> > What I cannot do is reference that current employeeid. If click edit,
> > my edittemplate show and my fileupload and button are visible. In that
> > buttons event, I need the employee id of that row.
> >
> > I'm not sure how to get that.
> >
> > Thank you for any help.
> >


 
Reply With Quote
 
needin4mation@gmail.com
Guest
Posts: n/a
 
      10-17-2006

(E-Mail Removed) wrote:
> I searched for this and found an answer, but it did not help. In my
> GridView there is a fileupload and a button click event to save the
> file upload. I am naming the file uploaded after the primary key field
> of my table, so, if I have employeeid 4, then my file name is 4.jpg.
>
> What I cannot do is reference that current employeeid. If click edit,
> my edittemplate show and my fileupload and button are visible. In that
> buttons event, I need the employee id of that row.
>
> I'm not sure how to get that.
>
> Thank you for any help.


After much searching here is what I did:

if (e.CommandName == "addImage")
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row =
(GridViewRow)((Control)e.CommandSource).Parent.Par ent;
FileUpload fileUpload = (FileUpload)
GridView1.Rows[row.RowIndex].FindControl("FileUpload1");

if (fileUpload.HasFile)
{
fileUpload.SaveAs(Server.MapPath(".\\images\\") +
e.CommandArgument + ".jpg");//get employeeid
}
else
{
Response.Write("No File Uploaded.");
}



}

Where e.CommandArgument is assigned when the control is built by
CommandArgument='<%# Eval("employeeid")%>'

The commandargument holds the data for my file and the commandsource
parent(s) hold the row value so that I know what row clicked the upload
button.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How To Find Currently Selected Tile.Notebook Tab? bcwhite@pobox.com Python 3 04-21-2007 03:45 PM
Find ItemIndex given DataKey in DataGrid Rod ASP .Net 2 03-09-2007 02:25 PM
How to make a newly created record currently selected jason2k ASP .Net 0 07-27-2006 03:23 PM
Persisting the currently selected row on a GridView =?Utf-8?B?RGljaw==?= ASP .Net 5 01-23-2006 02:22 PM



Advertisments