2016-02-17 172 views
-1

我試圖從單擊按鈕時所選項目中獲取值。獲取DataList中所選項目的值

這裏是我的代碼:

<asp:DataList ID="DataList1" runat="server" RepeatColumns="3" RepeatDirection="Horizontal" Width="100%"> 
    <ItemTemplate> 
     <p class="own"><asp:Image ID="Image1" runat="server" ImageUrl='<%# "GetImageDatafromDB.aspx?id=" + System.Convert.ToString(Eval("ID")) %>' Width="230" Height="250"/> 
      <br /> 
      <span class="own1" style="width:230px;"><br /><asp:Label ID="Label1" runat="server" Text='<%# Eval("Name") %>' Font-Bold="True" Font-Size="1.2em" ForeColor="White"/> 
      <br /> 
      <asp:Label ID="Label5" runat="server" Text="Direktor :" style="color:#06D85F; float:left"></asp:Label><asp:Label ID="Label2" runat="server" CssClass="InFo" Text='<%# Eval("fattare") %>' Font-Italic="true"/> 
      <br /> 
      <asp:Label ID="Label4" runat="server" Text="Year : " style="color:#06D85F; float:left;"></asp:Label><asp:Label ID="Label3" CssClass="InFo" runat="server" Text=' <%# (Eval("Ar")) %>' /> 
      </br> 
      <asp:Button ID="Button1" runat="server" Text="Visa" CommandName="ButtonClick"/> 
      </span> 
      <asp:Label ID="Label6" runat="server" Visible="false" Text='<%# Eval("Name") %>'></asp:Label> 
     </p> 
    </ItemTemplate> 
</asp:DataList> 
protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!Page.IsPostBack) 
    { 
     string connectionString = ConfigurationManager.ConnectionStrings["Hemsida"].ConnectionString; 
     DataTable dt = new DataTable(); 
     SqlConnection conn = new SqlConnection(connectionString); 
     using (conn) 
     { 
      SqlDataAdapter ad = new SqlDataAdapter("SELECT * FROM Movies", conn); 
      ad.Fill(dt); 
     } 
     DataList1.DataSource = dt; 
     DataList1.DataBind(); 
    } 
} 

protected void Button1_Click(object sender, EventArgs e) 
{ 
    foreach (DataListItem item in DataList1.Items) 
    { 
     Label myTempLabel = (Label)item.FindControl("Label6"); 

     myTempLabel.Visible = true; 
    } 
} 

當我按一下按鈕,我得到的所有的所有項目的名稱,但我只想要得到所選的名稱項目。

+0

不應該有與傳遞所需參數的時名where子句 –

回答

1

在DataList1的ItemCommand事件,粘貼下面的代碼:

protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e) 
{ 
    DataList1.SelectedIndex = e.Item.ItemIndex; 
    myTempLabel.Text = "You selected: " + ((Label)DataList1.SelectedItem.FindControl("Label1")).Text; 
    myTempLabel.Visible = true; 
} 
+1

能否請您解釋一下參數的where子句中的select語句如何使用你的代碼 –

+0

上面的代碼現在應該工作,因爲我也驗證了。 –

0

我相信你需要使用的SelectedItem。

https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.datalist.selecteditem(v=vs.110).aspx

事情是這樣的:

protected void Button1_Click(object sender, EventArgs e) 
{ 
    if (DataList1.SelectedItem != null) 
    { 
     Label myTempLabel = (Label)DataList1.SelectedItem.FindControl("Label6"); 
     myTempLabel.Visible = true; 
    } 
} 

嘗試改變這一點:

<asp:Button ID="Button1" runat="server" Text="Visa" CommandName="ButtonClick"/> 

這樣:

<asp:Button ID="Button1" runat="server" Text="Visa" OnClick="Button1_Click" 
/> 

另一種方法是在按鈕刪除代碼和處理它的Item_Command:

//Add item command to DataList 
<asp:DataList ID="DataList1" runat="server" RepeatColumns="3" RepeatDirection="Horizontal" Width="100%" OnItemCommand="Item_Command"> 


    void Item_Command(Object sender, DataListCommandEventArgs e) 
    { 

    // Set the SelectedIndex property to select an item in the DataList. 
    ItemsList.SelectedIndex = e.Item.ItemIndex; 
    //Get the selected Item 
    DataListItem selectedItem = DataList1.DataKeys[DataListList1.SelectedIndex]; 
    //Get the label control 
    Label myTempLabel = (Label)selectedItem.FindControl("Label6"); 
    myTempLabel.Visible = true; 

    // Not sure if this is needed. 
    //ItemsList.DataSource = CreateDataSource(); 
    //ItemsList.DataBind(); 

    } 
+0

我試過我們的代碼,但仍然無法正常工作 –