我有一个中继器控件,它显示图像和一些基本信息,例如日期时间和产品 ID。单击图像时,我需要运行另一个查询并在单独的弹出窗口中显示详细信息。我知道如何执行弹出窗口。 我想知道如何从转发器中获取 DateTime 和 ProductId 并在图像的按钮单击事件中使用它们?
我的中继器代码如下:
<asp:Repeater ID="rptMonitorSummary" runat="server" OnItemDataBound="rptMonitorSummary_OnItemDataBound">
<ItemTemplate>
<asp:Panel ID="Pnl" runat="server">
<li class="ui-widget-content ui-corner-tr">
<h5 class="ui-widget-header">
<%# Eval("Name").ToString().Length > 9 ? (Eval("Name") as string).Substring(0, 9) : Eval("Name")%>
</h5>
<div id="divHover">
<asp:ImageButton Width="80px" ID="btnPerformanceImage" onclick="btnPerformanceImage_Click" runat="server" Height="45px">
</asp:ImageButton>
</div>
<div class="tooltip" style="display: none">
<div style="text-align: center; font-weight: bold;">
<%# Eval("DateTime") %><br />
</div>
<div style="text-align: center; font-weight: normal">
ErrorRatingCalls =
<%# Eval("ProductId")%><br />
</div>
<div style="text-align: center; font-weight: normal">
TotalRatingCalls =
<%# Eval("TotalCalls")%>
<br />
</div>
<div style="text-align: center; font-weight: normal">
SuccessRate =
<%# Eval("PassPercentage") + "%" %>
</div>
</div>
</li>
</asp:Panel>
</ItemTemplate>
</asp:Repeater>
我还有下面的按钮单击事件:
protected void btnPerformanceImage_Click(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript
(this, this.GetType(), "callScriptFunction", "ViewModelPopup1();", true);
}
我想知道的是,当我单击转发器控件内的图像时,如何获取已绑定(bind)到转发器控件的值
最佳答案
您应该使用 Repeater
控件的 ItemCommand
事件,而不是处理按钮本身的点击。作为 CommandArgument
,您可以传递 ProductId
,然后在处理程序内使用它从数据库检索其余信息。或者,您甚至可以将所有需要的值作为单个字符串包含到 CommandArgument
中,然后进行一些解析:
<asp:Repeater ... OnItemCommand="rptMonitorSummary_ItemCommand" ... >
...
<asp:ImageButton ... CommandName="ShowPopup" CommandArgument='<%# Eval("ProductId") + "," + Eval("DateTime") %>'>
</asp:ImageButton>
...
</asp:Repeater>
处理程序可能如下所示:
protected void rptMonitorSummary_ItemCommand(object sender, RepeaterCommandEventArgs e)
{
string[] tokens = e.CommandArgument.ToString().Split(',');
int productId = int.Parse(tokens[0]);
DateTime dateTime = Date.Time.Parse(tokens[1]);
// the rest of the handling here
}
关于c# - 如何从中继器控件检索详细信息以在模式弹出窗口中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16988189/