Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need Explanation on Pass DataKeyNames into Code Behind

Status
Not open for further replies.

koklimabc

Programmer
Jun 17, 2013
49
MY
I've been struggling in half day to strive for understanding "DatakeyNames" obtained from listview where it passed the Entity fields value captured from "EntityDatasource" and stored into DataKeyNames for later use in Code Behind (Example: 'EntityDataSource1_Deleted' events).

<asp:ListView ID="ListView1" runat="server" DataKeyNames="Id,ImageUrl" DataSourceID="EntityDataSource2" OnItemDeleted="ListView1_ItemDeleted" >

-In My Code Behind,

protected void EntityDataSource2_Deleted(object sender, EntityDataSourceChangedEventArgs e)
{
string id_ = this.ListView1.DataKeys[0]["Id"].ToString();

string url_ = this.ListView1.DataKeys[0]["ImageUrl"].ToString();

ScriptManager.RegisterStartupScript(this, GetType(), "Notification", "alert('check ID:" + id_ + "with Url:" + url_ + "')", true);

} //End of ItemDeleted

My Question Here:

1. What is structure "[0]" means where it is used besides "DataKeys".

2. If I change "[0]" to be "[1]", it would obtained the same result as taken from "[0]".

3. If I change "[0]" with "[this.listview1.selectedIndex]", it will end up into unlimited looped or hanging in pages postback.Why?
 
[0] means the first element in an array. This can be used anywhere in your code, etc.
[1] would be the second element. It may not be the same values as [0], and does not refer to [0] in any way. They are 2 different elements
3.) I don't understand what you are saying because you didn't post any code, so I am not sure what is happening.

You code is also wrong in tying to access the DataKeys collection. You would do: string id_ = this.ListView1.DataKeys[0].value;

Have you looked online at all? There is plenty of info out there and examples:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top