我有Repeater
,其中包含标有"更改会员资格"的链接。点击后会打开一个包含radiobuttonlist
和button
的灯箱。当点击灯箱中的按钮时,我有一个回调事件,我需要先找到radiobutton列表的选定值,这里是转发器:
<script language="JavaScript" type="text/javascript">
function CreateBox(id) {
$(document).ready(function () {
$("#lnk" + id).fancybox({
'closeBtn': true,
helpers: {
overlay: { closeClick: false }
}
});
});
}
</script>
<asp:Repeater ID="repProspects" runat="server" OnItemDataBound="repProspects_ItemDataBound">
<ItemTemplate>
<asp:HiddenField ID="hfRequestID" runat="server" Value='<%# DataBinder.Eval(Container, "DataItem.RequestID") %>' />
<asp:HiddenField ID="hfRecruiterNumber" runat="server" Value='<%# DataBinder.Eval(Container, "DataItem.RecruiterCardNumber") %>' />
<asp:HiddenField ID="hfCompanyID" runat="server" Value='<%# DataBinder.Eval(Container, "DataItem.CompanyID") %>' />
<asp:HiddenField ID="hfMemberType" runat="server" Value='<%# DataBinder.Eval(Container, "DataItem.MemberType") %>' />
<asp:HiddenField ID="hfLifeDuesAmount" runat="server" Value='<%# DataBinder.Eval(Container, "DataItem.Dues") %>' />
<asp:HiddenField ID="hfDerivedAnnualDues" runat="server" />
<asp:HiddenField ID="hfDerivedInstallments" runat="server" />
<asp:HiddenField ID="hfRblSelectedValue" runat="server" />
<asp:HiddenField ID="hfSetMemberType" ClientIDMode="Static" runat="server" />
<asp:HiddenField ID="hfState" runat="server" Value='<%# DataBinder.Eval(Container, "DataItem.HomeState") %>' />
<asp:HiddenField ID="hfCountry" runat="server" Value='<%# DataBinder.Eval(Container, "DataItem.HomeCountry") %>' />
<asp:HiddenField ID="hfBirthday" runat="server" Value='<%# DataBinder.Eval(Container, "DataItem.Birthday") %>' />
<div id='h<%# DataBinder.Eval(Container, "DataItem.ID") %>' class="header" onclick='ToggleDisplay(<%# DataBinder.Eval(Container, "DataItem.ID") %>);'>
<img id="img<%# DataBinder.Eval(Container, "DataItem.ID") %>" alt="" src="../images/plusIconSmaller.png" />
<%# DataBinder.Eval(Container, "DataItem.FirstName")%>
<% if (DataBinder.GetDataItem("DataItem.MiddleName") != "")
{ %>
<%# DataBinder.Eval(Container, "DataItem.MiddleName")%>
<% } %>
<%# DataBinder.Eval(Container, "DataItem.LastName")%>
<% if (DataBinder.GetDataItem("DataItem.Suffix") != "")
{ %>
<%# DataBinder.Eval(Container, "DataItem.Suffix")%>
<% } %>
(<%# DataBinder.Eval(Container, "DataItem.CurrentStatus")%>, <%# DataBinder.Eval(Container, "DataItem.BranchOfService")%>)
<asp:Label ID="lblRecruitedBy" runat="server"></asp:Label>
<%# Convert.ToDateTime(DataBinder.Eval(Container, "DataItem.DateCreated")).ToShortDateString()%>
</div>
<div id='reqid<%# DataBinder.Eval(Container, "DataItem.RequestID") %>'></div>
<div id='d<%# DataBinder.Eval(Container, "DataItem.ID") %>' class="details">
<table width="100%">
<tr>
<td valign="top" width="25%"><u><b>Address</b></u><br />
<%# DataBinder.Eval(Container, "DataItem.HomeAddressLine1")%><br />
<%# DataBinder.Eval(Container, "DataItem.HomeCity")%>, <%# DataBinder.Eval(Container, "DataItem.HomeState")%> <%# DataBinder.Eval(Container, "DataItem.HomeZipCode")%><br />
<%# DataBinder.Eval(Container, "DataItem.HomeCountry")%></td>
<td valign="top" width="20%"><u><b>Qualifying Service</b></u><br />
<asp:Label ID="lblServiceInfo" runat="server"></asp:Label></td>
<td valign="top" width="20%"><u><b>Contact Info</b></u><br />
<% if (DataBinder.GetDataItem("DataItem.Phone") != "")
{ %>
<%# FormatPhone(DataBinder.Eval(Container, "DataItem.Phone").ToString()) %>
<% } %>
<asp:Label ID="lblMemberPhone" runat="server"></asp:Label>
<%# DataBinder.Eval(Container, "DataItem.Email")%><br />
Birthday: <%# Convert.ToDateTime(DataBinder.Eval(Container, "DataItem.Birthday")).ToShortDateString()%></td>
<td valign="top" width="20%"><u><b>Membership</b></u><br />
<%# DataBinder.Eval(Container, "DataItem.MemberType")%><br />
$<asp:Label ID="lblDuesAmount" runat="server"></asp:Label>
<br />
<a href='#ChgMemType<%# DataBinder.Eval(Container, "DataItem.ID") %>' onclick='CreateBox(<%# DataBinder.Eval(Container, "DataItem.ID") %>);' id='lnk<%# DataBinder.Eval(Container, "DataItem.ID") %>' >Change Membership</a>
</td>
<td valign="top" align="center">
<asp:Button ID="lnkApprove" Style="border: 1px solid black; border-radius: 7px; padding: 5px; cursor: pointer; background-color: #990000; width: 130px; color: white; font-weight: bold" Text="Approve & Pay" runat="server" CommandArgument='<%# DataBinder.Eval(Container, "DataItem.ID") %>' OnClientClick="return confirm('Are you sure you want to approve this member application?');" OnCommand="lnkApprove_Click"></asp:Button><br />
<br />
<asp:Button ID="lnkReject" Style="border: 1px solid black; border-radius: 7px; padding: 5px; cursor: pointer; background-color: #990000; width: 130px; color: white; font-weight: bold" Text="Reject" runat="server" CommandArgument='<%# DataBinder.Eval(Container, "DataItem.ID") %>' OnClientClick="return confirm('Are you sure you want to reject this member applictation?');" OnCommand="lnkReject_Click"></asp:Button></td>
</tr>
</table>
</div>
<div id='ChgMemType<%# DataBinder.Eval(Container, "DataItem.ID") %>' style="display: none; width:400px; text-align: left">
<h3>Change Membership Type </h3>
<p>Please select the membership type below:</p>
<input id='hfChangedMemberType' value="<%# DataBinder.Eval(Container, "DataItem.ID") %>" type="hidden" />
<div id="RadioDiv">
<asp:RadioButtonList ID="_rblMemberTypes" runat="server">
<asp:ListItem Text="Annual" Value="Annual">Annual</asp:ListItem>
<asp:ListItem Text="Life" Value="Life">Life</asp:ListItem>
<asp:ListItem Text="Installment" Value="Installment">Installment</asp:ListItem>
</asp:RadioButtonList>
</div>
<asp:LinkButton ID="lbSetMemType" EnableViewState="true" CommandArgument='<%# DataBinder.Eval(Container, "DataItem.RequestID") %>' OnCommand="lbSetMemType_Command" CssClass="button" runat="server">Save</asp:LinkButton>
</div>
</ItemTemplate>
</asp:Repeater>
下一步是单击“保存”按钮时的代码隐藏事件:
protected void lbSetMemType_Command(object sender, CommandEventArgs e)
{
decimal dDuesAmount = 0;
bool bSuccess = false;
int iRequestID = Convert.ToInt32(e.CommandArgument);
string sMemType = "";
HiddenField hfDerivedAnnualDues;
HiddenField hfDerivedInstallments;
HiddenField hfLifeDuesAmount;
HiddenField hfSetMemberType;
foreach (RepeaterItem item in repProspects.Items)
{
// Checking the item is a data item
if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
{
var reqid = item.FindControl("hfRequestID") as HiddenField;
if (Convert.ToInt32(reqid.Value) == iRequestID) {
var rdbList = item.FindControl("_rblMemberTypes") as RadioButtonList;
if (rdbList != null)
{
foreach (ListItem li in rdbList.Items)
{
if (li.Selected == true)
{
sMemType = li.Text;
}
}
}
// Get the selected value
hfSetMemberType = item.FindControl("hfRblSelectedValue") as HiddenField;
sMemType = rdbList.SelectedValue;
// sMemType = hfSetMemberType.Value;
hfDerivedAnnualDues = item.FindControl("hfDerivedAnnualDues") as HiddenField;
hfDerivedInstallments = item.FindControl("hfDerivedInstallments") as HiddenField;
hfLifeDuesAmount = item.FindControl("hfLifeDuesAmount") as HiddenField;
}
}
}
switch (sMemType)
{
case "Annual":
{
//dDuesAmount = Convert.ToDecimal(hfDerivedAnnualDues.Value);
break;
}
case "Life":
{
//dDuesAmount = Convert.ToDecimal(hfLifeDuesAmount.Value);
break;
}
case "Installments":
{
// dDuesAmount = Convert.ToDecimal(hfDerivedInstallments.Value);
break;
}
default:
{
//dDuesAmount = Convert.ToDecimal(hfDerivedAnnualDues.Value);
break;
}
}
bSuccess = logicManager.UpdateNewMemberAppMemType(iRequestID, sMemType, dDuesAmount);
}
我可以设置断点,我可以看到隐藏字段中的值是正确的但是我无法在此声明中获得正确的值:
sMemType = rdbList.SelectedValue;
我得到初始值但不是用户点击的值?
答案 0 :(得分:1)
首先,您在实际代码中缺少OnItemCommand="lbSetMemType_Command"
。
此外,手动迭代转发器会给你带来糟糕的性能结果,特别是如果你只是寻找无线电按钮的值。
现在,让我们一起做一个MCVE示例。根据您的代码,您尝试在RadioButtonList
内构建Repeater
,LinkButton
使用广播的按钮值。在我们的例子中,让我们在标签中打印所选择按钮的值,证明我们的情况。
<强> MCVE.aspx 强>
<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
<asp:Repeater ID="rptTest" runat="server" OnItemCommand="rptTest_ItemCommand">
<ItemTemplate>
<asp:RadioButtonList ID="rdlTest" runat="server">
<asp:ListItem Text="Annual" Value="Annual"></asp:ListItem>
<asp:ListItem Text="Life" Value="Life"></asp:ListItem>
<asp:ListItem Text="Installment" Value="Installment"></asp:ListItem>
</asp:RadioButtonList>
<asp:LinkButton runat="server" ID="lbValidationTest" OnClick="lbValidationTest_Click" runat="server" >Fetch Value</asp:LinkButton>
</ItemTemplate>
</asp:Repeater>
<asp:Label runat="server" ID="lblViewResult"></asp:Label>
</asp:Content>
我在VS上创建新项目时使用默认母版页。这真的无关紧要
代码隐藏
<强> MCVE.aspx.cs 强>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication
{
public partial class MCVE: Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
List<int> uselessData = new List<int>(new int[] { 1, 2 });
this.rptTest.DataSource = uselessData;
this.rptTest.DataBind();
}
}
protected void rptTest_ItemCommand(object source, RepeaterCommandEventArgs e)
{
RadioButtonList list = (RadioButtonList)e.Item.FindControl("rdlTest");
this.lblViewResult.Text = list.SelectedValue;
}
}
}
经过一些尝试,我们清楚地看到这不符合预期。实际上Repeater
和RadioButtonList
之间有一些known issues。 CommandArgument
禁止我们这样做。
那么,现在该怎么办?我们有很多解决方案,例如我们可以使用javascript来获取并在隐藏字段中设置我们的值;我们可以迭代整个转发器树并检查每个按钮,甚至可以使用RadioButtonList
找到<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
<asp:Repeater ID="rptTest" runat="server" OnItemCommand="rptTest_ItemCommand">
<ItemTemplate>
<div>
<asp:HiddenField runat="server" ID="hfSelectedValue" />
<asp:RadioButtonList ID="rdlTest" runat="server" >
<asp:ListItem Text="Annual" Value="Annual" onclick="QuickAndDirtyHiddenSetDontUseItInProd(this)"></asp:ListItem>
<asp:ListItem Text="Life" Value="Life" onclick="QuickAndDirtyHiddenSetDontUseItInProd(this)"></asp:ListItem>
<asp:ListItem Text="Installment" Value="Installment" onclick="QuickAndDirtyHiddenSetDontUseItInProd(this)"></asp:ListItem>
</asp:RadioButtonList>
<asp:LinkButton runat="server" ID="lbValidationTest" runat="server" UserSubmitBehavior="true" >Fetch Value</asp:LinkButton>
</div>
</ItemTemplate>
</asp:Repeater>
<asp:Label runat="server" ID="lblViewResult"></asp:Label>
<script>
function QuickAndDirtyHiddenSetDontUseItInProd(data) {
$(data).parent().parent().parent().parent().siblings("input[name*=hfSelectedValue]").val(data.value);
}
</script>
</asp:Content>
索引并在代码隐藏中获取它。
让我们实现javascript解决方案。我们将在转发器中存在的隐藏字段中注入索引的值。 我认为更好地对待这种行为客户端而不是服务器端,但我可能会弄错,并且会喜欢这方面的输出。
因此,让我们在视图中添加一个非常脏的javascript代码
MCVE.aspx新版本
RepeaterItem
我们刚改变了3件事。我们将为我们要创建的每个radiobuttonlist跟踪一个隐藏字段,我们添加了一个脚本来更改所述隐藏字段的值,并将两者与onclic相关联。
现在,在我们的代码隐藏中,我们只需跟踪与ButtonLink上的点击分组的隐藏字段(即,在同一个using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication
{
public partial class MCVE: Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
List<int> uselessData = new List<int>(new int[] { 1, 2 });
this.rptTest.DataSource = uselessData;
this.rptTest.DataBind();
}
}
protected void rptTest_ItemCommand(object source, RepeaterCommandEventArgs e)
{
HiddenField hiddenField = (HiddenField)e.Item.FindControl("hfSelectedValue");
this.lblViewResult.Text = hiddenField.Value;
}
}
}
中),我们就可以了。
MCVE.aspx.cs新版本
jmethodID midGetPackageName = (*env).GetMethodID(Context_class, "getPackageName", "(V)Ljava/lang/String;");
相当不言自明,我们只是在转发器中找到了隐藏的字段,我们用它来填充标签。