[英]Get value of selected item in DataList
I'm trying to get the value from the selected item when I click a button.单击按钮时,我试图从所选项目中获取值。
Here is my code:这是我的代码:
<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;
}
}
When I click the button, I get all of the names of all of the items, but I only want to get the name of the selected item.当我点击按钮时,我得到了所有项目的所有名称,但我只想获得所选项目的名称。
On the ItemCommand event of DataList1, paste the following code:在 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;
}
I believe you need to use SelectedItem.我相信您需要使用 SelectedItem。
https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.datalist.selecteditem(v=vs.110).aspx https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.datalist.selecteditem(v=vs.110).aspx
Something like this:是这样的:
protected void Button1_Click(object sender, EventArgs e)
{
if (DataList1.SelectedItem != null)
{
Label myTempLabel = (Label)DataList1.SelectedItem.FindControl("Label6");
myTempLabel.Visible = true;
}
}
Try changing this:尝试改变这个:
<asp:Button ID="Button1" runat="server" Text="Visa" CommandName="ButtonClick"/>
to This:对此:
<asp:Button ID="Button1" runat="server" Text="Visa" OnClick="Button1_Click"
/>
Another alternative is to remove the code in the button and handle it on the Item_Command:另一种方法是删除按钮中的代码并在 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();
}
If the textbox that gets the value is outside your update panel then try this:如果获取值的文本框在更新面板之外,请尝试以下操作:
if (e.CommandName == "selectitem")
{
DataList1.SelectedIndex = e.Item.ItemIndex;
TextBox2.Text = Label)DataList1.SelectedItem.FindControl("Label1")).Text;
}
If the textbox that gets the value is inside your update panel then try this如果获取值的文本框在您的更新面板内,请尝试此操作
if (e.CommandName == "selectitem")
((Label)e.Item.FindControl("Label1")).Text = e.CommandArgument.ToString();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.