获取数据列表行详细信息

时间:2011-07-13 14:10:11

标签: c# asp.net datalist

我的datalist中有以下itemTemplate

          <ItemTemplate>
 <asp:LinkButton ID="LinkButton1" CommandName="Select" autopostback="True" runat="server">'<%# Eval("ThreadTitle") %>'</asp:LinkButton>

            </ItemTemplate>

这是我的dataList标记:

   <asp:DataList ID="DataList1" runat="server" 
            DataSourceID="AllQuestionAskedDataSource"
            GridLines="Horizontal" DataKeyField="ThreadsID" >

这是sqldatasource:

<asp:SqlDataSource ID="AllQuestionAskedDataSource" runat="server" 
    ConnectionString="<%$ ConnectionStrings:CP_AllQuestionsAnswered %>" SelectCommand="SELECT ThreadTitle, ThreadsID
    FROM Threads
WHERE UsersID=@UserID" onselecting="AllQuestionAskedDataSource_Selecting">

我正在尝试使用以下事件处理程序之一:

protected void DataList2_SelectedIndexChanged(object sender, EventArgs e)
{

}
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{

}

我希望在其中一个事件处理程序中获取与所选行相关联的ThreadID(通过单击datalist中的行)并从所选的特定行中获取ThreadTitle参数。

我怎样才能完成这项任务?

我基本上想要检索我提供给sqldatasource的sql参数,这些参数与点击的特定行有关!!

自己解决了:

    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

2 个答案:

答案 0 :(得分:1)

你可以这样做..

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")
}

答案 1 :(得分:-1)

我认为,以下代码将起作用:

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"]);
    }