如何访问中继器中的标签
How Can Access Label In Repeater
我尝试访问中继器中的标签以获取值并在同一标签中设置另一个值
<ul class="mail-list">
<asp:Repeater ID="repMsg" runat="server" DataSourceID="EDSrepMSg">
<ItemTemplate>
<li>
<asp:LinkButton ID="lnkMsgDisplay" runat="server" CausesValidation="false" CommandName="Display_Msg" CommandArgument='<%# Bind("message_id") %>'>
<span class="mail-sender">
<asp:Label ID="lblEmail" runat="server" Text='<%#Eval("message_from") %>'></asp:Label></span>
<span class="mail-subject">
<asp:Label ID="lblSubject" runat="server" Text='<%#Eval("message_title") %>'></asp:Label></span>
<span class="mail-message-preview">
<asp:Label ID="lblMsg" runat="server" Text='<%#If (Eval("message_description").ToString.Length > 10,Eval("message_description").ToString.Substring(0,10).TrimEnd() & "..",Eval("message_description"))%>'></asp:Label></span>
</asp:LinkButton>
</li>
</ItemTemplate>
</asp:Repeater>
<asp:EntityDataSource ID="EDSrepMSg" runat="server" ConnectionString="name=BWJO_dbEntities" DefaultContainerName="BWJO_dbEntities" EnableFlattening="False" EntitySetName="tbl_message"
Where="it.message_to=@message_to and it.message_type=2 or it.message_type=4 " OrderBy="it.message_date desc">
<WhereParameters>
<asp:SessionParameter SessionField="user_id" Name="message_to" Type="Int32" DefaultValue="0" />
</WhereParameters>
</asp:EntityDataSource>
输出:
注:
Theres no relationship btween tbl_msg,tbl_user,tbl_emp
我只想知道如何从
中获取价值
<asp:Label ID="lblEmail" runat="server" Text='<%#Eval("message_from") %>'></asp:Label>
然后将值设置到 lblEmail(编辑标签)
你可以先获取标签,然后获取它的值,像这样..
例如你有标签:
<asp:Label ID="lblval" runat="server" Text="this is test"></asp:Label>
然后在后端您可以获得如下文本:
protected void rptrMaylike_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Label lblv = (Label)e.Item.FindControl("lblval");
lblv.Text = "Value from code behind";
}
}
感谢 Usman Mirza 给我提示,我是这样解决的:
*在中继器中创建新标签以读取message_id
<asp:Label ID="lblMsgID" runat="server" Text='<%#Eval("message_id") %>' Visible="false"></asp:Label>
<asp:Label ID="lblEmail" runat="server" Text=""></asp:Label></span>
后面的代码:
Protected Sub repMsg_ItemDataBound(sender As Object, e As RepeaterItemEventArgs) Handles repMsg.ItemDataBound
If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
Dim MsgID As Integer = Integer.Parse(TryCast(e.Item.FindControl("lblMsgID"), Label).Text)
Using context As New BWJO_dbModel.BWJO_dbEntities
Dim obj = context.tbl_message.FirstOrDefault(Function(u) u.message_id = MsgID)
Dim fromID = obj.message_from
Dim ToID = obj.message_to
Dim msgtype = obj.message_type
If msgtype = 2 Then
Dim userObj = context.tbl_user.FirstOrDefault(Function(u) u.user_id = fromID)
Dim userEmail = userObj.user_email
Dim lblEmail = TryCast(e.Item.FindControl("lblEmail"), Label)
lblEmail.Text = userEmail
ElseIf msgtype = 4 Then
Dim empObj = context.tbl_employee.FirstOrDefault(Function(u) u.emp_id = fromID)
Dim empEmail = empObj.emp_email
Dim lblEmail = TryCast(e.Item.FindControl("lblEmail"), Label)
lblEmail.Text = empEmail
End If
End Using
End If
注:
message_type :
1)user-user
2)user-role
3)role-user
4)role-role
我尝试访问中继器中的标签以获取值并在同一标签中设置另一个值
<ul class="mail-list">
<asp:Repeater ID="repMsg" runat="server" DataSourceID="EDSrepMSg">
<ItemTemplate>
<li>
<asp:LinkButton ID="lnkMsgDisplay" runat="server" CausesValidation="false" CommandName="Display_Msg" CommandArgument='<%# Bind("message_id") %>'>
<span class="mail-sender">
<asp:Label ID="lblEmail" runat="server" Text='<%#Eval("message_from") %>'></asp:Label></span>
<span class="mail-subject">
<asp:Label ID="lblSubject" runat="server" Text='<%#Eval("message_title") %>'></asp:Label></span>
<span class="mail-message-preview">
<asp:Label ID="lblMsg" runat="server" Text='<%#If (Eval("message_description").ToString.Length > 10,Eval("message_description").ToString.Substring(0,10).TrimEnd() & "..",Eval("message_description"))%>'></asp:Label></span>
</asp:LinkButton>
</li>
</ItemTemplate>
</asp:Repeater>
<asp:EntityDataSource ID="EDSrepMSg" runat="server" ConnectionString="name=BWJO_dbEntities" DefaultContainerName="BWJO_dbEntities" EnableFlattening="False" EntitySetName="tbl_message"
Where="it.message_to=@message_to and it.message_type=2 or it.message_type=4 " OrderBy="it.message_date desc">
<WhereParameters>
<asp:SessionParameter SessionField="user_id" Name="message_to" Type="Int32" DefaultValue="0" />
</WhereParameters>
</asp:EntityDataSource>
输出:
注:
Theres no relationship btween tbl_msg,tbl_user,tbl_emp
我只想知道如何从
中获取价值<asp:Label ID="lblEmail" runat="server" Text='<%#Eval("message_from") %>'></asp:Label>
然后将值设置到 lblEmail(编辑标签)
你可以先获取标签,然后获取它的值,像这样.. 例如你有标签:
<asp:Label ID="lblval" runat="server" Text="this is test"></asp:Label>
然后在后端您可以获得如下文本:
protected void rptrMaylike_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Label lblv = (Label)e.Item.FindControl("lblval");
lblv.Text = "Value from code behind";
}
}
感谢 Usman Mirza 给我提示,我是这样解决的:
*在中继器中创建新标签以读取message_id
<asp:Label ID="lblMsgID" runat="server" Text='<%#Eval("message_id") %>' Visible="false"></asp:Label>
<asp:Label ID="lblEmail" runat="server" Text=""></asp:Label></span>
后面的代码:
Protected Sub repMsg_ItemDataBound(sender As Object, e As RepeaterItemEventArgs) Handles repMsg.ItemDataBound
If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
Dim MsgID As Integer = Integer.Parse(TryCast(e.Item.FindControl("lblMsgID"), Label).Text)
Using context As New BWJO_dbModel.BWJO_dbEntities
Dim obj = context.tbl_message.FirstOrDefault(Function(u) u.message_id = MsgID)
Dim fromID = obj.message_from
Dim ToID = obj.message_to
Dim msgtype = obj.message_type
If msgtype = 2 Then
Dim userObj = context.tbl_user.FirstOrDefault(Function(u) u.user_id = fromID)
Dim userEmail = userObj.user_email
Dim lblEmail = TryCast(e.Item.FindControl("lblEmail"), Label)
lblEmail.Text = userEmail
ElseIf msgtype = 4 Then
Dim empObj = context.tbl_employee.FirstOrDefault(Function(u) u.emp_id = fromID)
Dim empEmail = empObj.emp_email
Dim lblEmail = TryCast(e.Item.FindControl("lblEmail"), Label)
lblEmail.Text = empEmail
End If
End Using
End If
注:
message_type :
1)user-user
2)user-role
3)role-user
4)role-role