Obtaining the datalists row details
I have the following itemTemplate in my datalist
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" CommandName="Select" autopostback="True" runat="server">'<%# Eval("ThreadTitle") %>'</asp:LinkButton>
</ItemTemplate>
This is my dataList mark up:
<asp:DataList ID="DataList1" runat="server"
DataSourceID="AllQuestionAskedDataSource"
GridLines="Horizontal" DataKeyField="ThreadsID" >
This is the sqldatasource:
<asp:SqlDataSource ID="AllQuestionAskedDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:CP_AllQuestionsAnswered %>" SelectCommand="SELECT ThreadTitle, ThreadsID
FROM Threads
WHERE UsersID=@UserID" onselecting="AllQuestionAskedDataSource_Selecting">
I am trying to use one of the following events handlers:
protected void DataList2_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
}
What I want to obtain in one of those event handlers the ThreadID associated with the row selected开发者_如何学运维 (by clicking on the row in the datalist) and get the ThreadTitle parameter from the specific row selected..
How can I achieve that task?
I basically want to retrieve the sql parameters that i feed into the sqldatasource, relating to the specific row clicked!!
Solved it myself:
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
LinkButton link = (LinkButton)e.Item.FindControl("LinkButton1");
string threadTitle = link.Text;
string recordID = (DataList1.DataKeys[e.Item.ItemIndex]).ToString();
string special = "";
}
enter code here
You can do like..
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
// Will give you current data key value
Int32 threadID = Convert.ToInt32(e.CommandArgument);
// if you want value of your control
(ControlTypd)e.Item.FindControl("ControldId")
}
I think, that the following code will work:
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e) {
DataRowView rowView = e.Item.DataItem as DataRowView;
int threadID = Convert.ToInt32(rowView["ThreadsID"]);
}
OR
protected void DataList1_SelectedIndexChanged(object sender, EventArgs e) {
DataRowView rowView = DataList1.SelectedItem.DataItem as DataRowView;
int threadID = Convert.ToInt32(rowView["ThreadsID"]);
}
精彩评论