'> -6ren">
gpt4 book ai didi

c# - 获取当前正在访问的 "row"中的Repeater Label

转载 作者:太空宇宙 更新时间:2023-11-03 13:37:53 25 4
gpt4 key购买 nike

我有以下中继器:

<asp:Repeater ID="RptLeaveRequests" runat="server" 
onitemdatabound="RptLeaveRequests_ItemDataBound"> <ItemTemplate>
<table id="tableItem" runat="server">
<tr>
<td style="width: 100px;">
<asp:Label ID="lblDate" runat="server" Text='<%#Eval("Date", "{0:dd/M/yyyy}") %>'></asp:Label>
</td>
<td style="width: 100px;">
<asp:Label ID="lblHours" runat="server" Text='<%#Eval("Hours") %>'></asp:Label>
</td>
<td style="width: 200px;">
<asp:Label ID="lblPeriod" runat="server" Text='<%#Eval("AMorPM") %>'></asp:Label>
</td>
<td style="width: 200px; font-size:10px;">
<asp:Label ID="lblNote" runat="server" Text='<%#Eval("Note") %>'></asp:Label>
</td>
<td style="50px">
<asp:RadioButtonList ID="rbtVerified" runat="server" >
<asp:ListItem Value="1">Accept</asp:ListItem>
<asp:ListItem Value="2">Reject</asp:ListItem>
</asp:RadioButtonList>
</td>
<td>
<asp:TextBox ID="txtNotes" runat="server" ></asp:TextBox>
</td>
</tr>
</table>

我正在尝试获取每个标签中的数据(例如:Convert.ToString((Label)item.FindControl("Date")))但它是返回一个空字符串,我做错了什么:

 foreach (RepeaterItem item in RptLeaveRequests.Items)
{
var rdbList = item.FindControl("rbtVerified") as RadioButtonList;
switch (rdbList.SelectedValue)
{
case "1":
if (new LeaveLogic().AddLeaveEmployee(Convert.ToString((Label)item.FindControl("Date")), Convert.ToDouble((Label)item.FindControl("Hours")), Convert.ToString((Label)item.FindControl("AMorPM")), "Vacational Leave", Convert.ToInt32(Context.User.Identity.Name), Convert.ToString((Label)item.FindControl("Note")))
{
Response.Redirect(Request.RawUrl);
}
break;

最佳答案

我认为它不起作用是因为您没有找到控件。如果找不到控件,FindControl 将返回 null,如果对象值为 null,Convert.ToString 将返回一个空字符串。

据我所知,您正在搜索错误的字符串 ID。所以不是 Date,它应该是 lblDate

如果您处于调试构建模式,请记住 ASP.NET 喜欢在运行时更改您的控件名称,因此“lblDate”控件实际上可能不是“lblDate”。因此,您可以尝试在浏览器上进行调试并检查元素的 ID 以获取其实际 ID。

此外,如果您想要标签的实际数据(注意 .Text),您可能想要这样做:

((Label)item.FindControl("lblDate")).Text

关于c# - 获取当前正在访问的 "row"中的Repeater Label,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18105132/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com