【问题标题】:How to access ItemTemplate control from ItemCommand event using Repeater如何使用 Repeater 从 ItemCommand 事件访问 ItemTemplate 控件
【发布时间】:2010-11-26 20:10:18
【问题描述】:

我的中继器:

<asp:Repeater ID="rptrContacts" runat="server" OnItemCommand="rptrContact_ItemCommand" >

<div ID="itemTemplate>
<ItemTemplate>
<%# Eval("Name") %>
<%# Eval("Email") %>
<asp:LinkButton ID="lbtnEditContact" runat="server" CommandName="Edit"  Text="Edit"   CommandArgument='<%# Eval("ContactID") %>' />
<asp:Label ID="lblUpdateConfirm" runat="server" Text="Update Confirmed" Visible="false" />
</ItemTemplate>
</div>

<div ID="editTemplate runat="server" visibility="false">
Update your Info:<br>
Name: <asp:TextBox ID="txtName" runat="server Text="<%# Eval("Name") %>"/> <br>
Email:  <asp:TextBox ID="txtEmail" runat="server Text="<%# Eval("Email") %>"/><br>
<asp:LinkButton ID="lbtnUpdateContact" CommandArgument='<%# Eval("ContactID") %>'   CommandName="UpdateContact" runat="server" >Update</asp:LinkButton>
</div> 

</asp:Repeater

ItemCommand 的代码:

switch(e.CommandName)
{
case "Edit":
//make editTemplate div visible
HtmlGenericControl divEditContact = (HtmlGenericControl)e.Item.FindControl ("divEditContact");
divEditContact.Visible = true;
break;

case "Update":
Employee updateEmployee = new Employee
       {
           employeeName = txtName.Text:
           employeeEmail = txtEmail.Text:
       }

updateEmployee = API.UpdateEmployee(updateEmployee);

          //display lblUpdateConfirm visible to True
         // so user sees this confirm messge in the newly updated ItemTemplate

}

如何访问我的 lblUpdateConfirm 并将其 Text 状态从 ItemCommand 内部变为可见,以便当用户看到新更新的 ITemTemplate 时,标签显示“更新确认”消息?

【问题讨论】:

    标签: c# asp.net repeater itemtemplate itemcommand


    【解决方案1】:

    VB:

    CType(e.Item.FindControl("lblUpdateConfirm"), Label).Visible = True;
    

    C#:

    Label lblToMakeVisible = (Label)e.Item.FindControl("lblUpdateConfirm");
    lblToMakeVisible.Visible = True;
    

    【讨论】:

    • 嗯。不工作。 VS2010 说“无法解析符号'CType'。我已将您的代码行放在 ItemCommand 事件中更新之后。任何其他想法都值得赞赏。
    • 感谢 C# 版本,仍然无法正常工作:“无法将表达式 ot type 'bool' 转换为 type 'Label'”。
    • 您在哪一行代码中得到该错误?第一行在该特定转发器项目中找到标签,然后将其转换为标签。下一行采用该转换标签并将可见性设置为 false。您在代码示例中使用 DivEditContact 执行相同的操作。
    • 好吧,我的错 - 有打字机。所以现在没有抛出错误。记录得到更新,但标签在返回 ItemTemplate 时保持隐藏状态。谢谢...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多