0
我試圖更新數據列表中按鈕上的一個標籤。然而,我似乎無法弄清楚如何選擇出現的每個值,所以如果我點擊列表中第三個項目上的按鈕,它應該在Label1中顯示該值,或者如果我單擊它應該顯示的第五個項目作爲Label1。我只能從DataList1.Items [0] .FindControl中直接獲取值。如何通過單擊按鈕獲取列表中每個單獨項目的值。 (我也試圖填補以及添加一個文本框),下面是我的代碼:`DataList中的動態值作爲標籤
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:ListBox ID="ListBox1" runat="server"></asp:ListBox>
<asp:DataList ID="DataList1" runat="server" DataKeyField="TempID" DataSourceID="SqlDataSource1">
<ItemTemplate>
User:
<asp:Label ID="UserLabel" runat="server" Text='<%# Eval("User") %>' />
<br />
Time:
<asp:Label ID="TimeLabel" runat="server" Text='<%# Eval("Time") %>' />
<br />
ActualTime:
<asp:Label ID="ActualTimeLabel" runat="server" Text='<%# Eval("ActualTime") %>' />
<br />
TempID:
<asp:Label ID="Label2" runat="server" Text='<%# Eval("TempID") %>' />
<br />
`
這裏是代碼背後:
protected void Page_Load(object sender, EventArgs e)
{
}
void BindList()
{
DataList1.DataSource = DataList1;
DataList1.DataBind();
}
public void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
DataList1.SelectedIndex = e.Item.ItemIndex;
BindList();
Label1.Text = "You selected: " +
((Label)DataList1.SelectedItem.FindControl("ActualTimeLabel")).Text;
}
protected void Button1_Click(object sender, EventArgs e)
{
int count = DataList1.Items.Count;
for (int i = 0; i < count; i++)
{
Label lbl = DataList1.Items[0].FindControl("ActualTimeLabel") as Label;
string labeltext = lbl.Text;
TextBox1.Text = labeltext;
}
}
protected void Button2_Click(object sender, EventArgs e)
{
}
}
它是一個[標籤:的WinForms]或[標籤:網絡表單],然後在那裏是按鈕的聲明? – AsifAli72090
這是一個webform,按鈕位於DataList中 – Marcus