获取DataList中所选项的值

时间:2016-02-17 14:34:53

标签: c# asp.net

当我点击按钮时,我正试图从所选项目中获取值。

这是我的代码:

<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;
    }
}

当我点击按钮时,我会获得所有项目的所有名称,但我只想获取所选项目的名称。

2 个答案:

答案 0 :(得分: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 :(得分: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();

  }