在 DataList 中找不到编辑模式下的控件
private void BindDataList()
{
int userId = Convert.ToInt32(ProfileInfo.GetUserID());
DataList1.DataSource = CatalogAccess.GetAddressByUserID(userId);
DataList1.DataBind();
foreach (DataListItem item in DataList1.Items)
{
Label lbl = (Label)item.FindControl("lbl");
lbl.Text = "myLabel";
}
}
protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
{
int userId = Convert.ToInt32(ProfileInfo.GetUserID());
DataList1.EditItemIndex = e.Item.ItemIndex;
DataList1.DataSource = CatalogAccess.GetAddressByUserID(userId);
DataList1.DataBind();
Label lbl = (Label)e.Item.FindControl("lbl") as Label;
lbl.Text = "edit mode";
}
<asp:DataList ID="DataList1" runat="server" oneditcommand="DataList1_EditCommand" >
<ItemTemplate>
<asp:Label ID="lblAddressID" runat="server" Text='<%# Bind("addressID") %>'/>
<asp:Label ID="lbl" runat="server" />
<asp:Button runat="Server" ID="cmdEdit" CommandName="Edit" Text="Edit"/>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtAddressID" runat="server" Text='<%# Bind("addressID") %>' BackColor="#FFFF66" />
<asp:Label ID="lbl" runat="server"/>
<asp:Button runat="Server" ID="cmdUpdate" CommandName="Update" Text="Update" />
<asp:Button runat="Server" ID="cmdCancel" CommandName="Cancel" Text="Cancel"/>
</EditItemTemplate>
</asp:DataList>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
第 1 步:在某处绑定数据
第 2 步:处理 OnItemDataBound 事件并在此处找到您的控件,类似于以下内容...
有关此事件的详细信息,请查看 MSDN 示例。 您必须检查 ItemType。 在这种情况下,它处于编辑模式,否则您检查列表项或备用项等。
Step 1: bind data somewhere
Step 2: handle the OnItemDataBound event and find your control here, similar to the following...
For more info on this event take a look at the MSDN example. You have to check for the ItemType. In this case it is in edit mode, otherwise you check for a listitem or alternate item etc.