2011-07-13 46 views
0

DataList控件:問題的一個DataList事件

<asp:DataList ID="DataList2" runat="server" 
        DataSourceID="AllQuestionsResponded" 
        GridLines="Horizontal" DataKeyField="ThreadsID" 
        onselectedindexchanged="DataList2_SelectedIndexChanged"> 
        <ItemTemplate> 

         <asp:LinkButton ID="LinkButton1" runat="server">'<%# Eval("ThreadTitle") %>'</asp:LinkButton> 
         <br /> 
         <br /> 
        </ItemTemplate> 
       </asp:DataList> 

和SQL數據源:

  <asp:SqlDataSource ID="AllQuestionsResponded" runat="server" 
       ConnectionString="<%$ ConnectionStrings:CP_AllQuestionsAnswered %>" SelectCommand="SELECT ThreadTitle, ThreadsID 
FROM Threads 
WHERE ThreadsID IN (SELECT ThreadsID 
FROM Comments 
WHERE [email protected]) 
" onselecting="AllQuestionAskedDataSource_Selecting"> 

事件背後的代碼:

protected void DataList2_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    Server.Transfer("AnswerQuestion.aspx?x=" + DataList2.DataKeyField + "&question=" + DataList2.SelectedValue + "&time=" + DateTime.Now); 
} 

的事件從未被觸發!無論多少次,我按下按鈕鏈接。爲什麼是:(..

回答

0

嘗試指定的CommandName選擇。

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

或者,你可以使用ButtonColumn

<asp:BoundColumn ButtonType="LinkButton" 
        HeaderText="ColumnHeader" 
        DataField="LinkButton" 
        CommandName="Select" /> 
+0

感謝它的工作..但我不能傳遞選定的值..它告訴我,DataList1.SelectedValue.ToString()格式不正確: –

+0

所以這個列可以作爲一個按鈕.cool –

1

嘗試

<asp:LinkButton ID="LinkButton1" runat="server" command="Select" autopostback="True">