Need Explanation on Pass DataKeyNames into Code Behind
Need Explanation on Pass DataKeyNames into Code Behind
(OP)
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?
<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?
RE: Need Explanation on Pass DataKeyNames into Code Behind
[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:
https://msdn.microsoft.com/en-us/library/system.we...
RE: Need Explanation on Pass DataKeyNames into Code Behind