我在嵌套网格视图中有一个隐藏控件来确定它是否已展开。我在javascript函数中设置了值。由于我设置的警报,它正在设置正确。在RowDataBound方法中,我想读取隐藏控件以确定是否应该展开网格。当我读取RowDataBound方法中的值时,它始终是' 0'。 我的标记很大,所以这只是嵌套网格,UpdatePanel和hiddencontrol关联:
<asp:UpdatePanel ID="updatePnlNestedGrid" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<div id="div<%# Eval("GroupID") %>" style="display:none">
<asp:GridView ID="GroupMemberGridView" runat="server" AutoGenerateColumns="false" OnRowDeleting="GroupMemberGridView_RowDeleting"
OnRowCommand="GroupMemberGridView_RowCommand" CssClass="grid" ShowFooter="true" AllowPaging="true" PageSize="5"
onpageindexchanging="GroupMemberGridView_PageIndexChanging" onpageindexchanged="GroupMemberGridView_PageIndexChanged">
<Columns>
<asp:TemplateField HeaderText="MemberID">
<ItemTemplate>
<asp:Label ID="mggvLblMemberID" runat="server" Text='<%# Bind("MemberID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Member Name" ItemStyle-Wrap="false">
<ItemTemplate>
<asp:Label ID="mggvLblMemberName" runat="server" Text='<%# Bind("MemberName") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="mggvDDLMemberName" runat="server"
class="chosen-single" data-placeholder="Choose member…">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="ReqValueDDLMemberInsert" runat="server" InitialValue="0"
ControlToValidate="mggvDDLMemberName"
ErrorMessage="Selection required." CssClass="message-error-dropdown">
</asp:RequiredFieldValidator>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Active Device(s)">
<ItemTemplate>
<asp:Label ID="mggvLblActiveDevice" runat="server" Text='<%# (Boolean.Parse(Eval("ActiveDevice").ToString())) ? "Yes" : "No" %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Action" ItemStyle-Wrap="false" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Button ID="mggvDeleteButton" runat="server" CausesValidation="False" CommandName="Delete"
Text="Delete" CssClass="gridActionbutton" OnClientClick="return confirm('Are you sure you want to delete this Group Member?')" >
</asp:Button>
</ItemTemplate>
<FooterTemplate>
<asp:Button ID="mggvAddButton" runat="server" CommandName="Add" Text="Add Member" Width="90%"
CssClass="gridActionbutton"/>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</ContentTemplate>
</asp:UpdatePanel>
<asp:HiddenField ID="mggvIsExpanded" runat="server" ClientIDMode="Static" Value="0" />
这是扩展/折叠网格并设置隐藏值的javascript函数:
function DivExpandCollapse(groupID) {
var div = document.getElementById(groupID);
var img = document.getElementById('img' + groupID);
//alert shows correct hidden value
alert($(div).parent().siblings("input:hidden[id$='mggvIsExpanded']").val());
if (div.style.display == "none") {
div.style.display = "inline";
img.src = "Images/minus.png";
$(div).parent().siblings("input:hidden[id$='mggvIsExpanded']").val("1");
$("#" + div.getAttribute('id') + ".chosen-single").chosen("destroy");
$("#" + div.getAttribute('id') + ".chosen-single").chosen({
search_contains: true,
width: "100%",
placeholder_text_single: "Choose member…",
no_results_text: "Sorry, no match!"
});
}
else {
div.style.display = "none";
img.src = "Images/plus.png";
$(div).parent().siblings("input:hidden[id$='mggvIsExpanded']").val("0");
}
}
这是事件背后的代码。我只显示隐藏的价值代码。:
protected void UserGroupGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int tiGroupID = Convert.ToInt32(UserGroupGridView.DataKeys[e.Row.RowIndex].Value.ToString());
GridView tgvGroupMember = (GridView)e.Row.FindControl("GroupMemberGridView");
HiddenField hiddenExpand = (HiddenField)tgvGroupMember.Parent.FindControl("mggvIsExpanded");
//This value is always "0" - the initial value
String hiddenValue = hiddenExpand.Value.ToString();
}
}
即使隐藏值是在javascript中设置的。在后面的代码中,该值始终为零。
为什么隐藏值永远不会显示为&#34; 1&#34;在网格扩展时的RowDataBound事件中?
更新 我将asp:HiddenField放在UpdatePanel内的ContentTemplate中。但它仍然没有正确读取隐藏值。
答案 0 :(得分:1)
您将隐藏字段视为在GridView的新绑定行中。
只是保持简单而不是引用FindControl(),直接引用控件,如mggvIsExpanded,如:
protected void UserGroupGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//reference mggvIsExpanded without jumping through hoops
String hiddenValue = mggvIsExpanded.Value.ToString();
}
}
如果该值仍然不正确,则可能意味着您在页面生命周期中过早地尝试引用该控件。确保代码在Page.Load之后运行或以其他方式手动从Request.Form [&#34; mggvIsExpanded&#34;]中获取值。
答案 1 :(得分:0)
正在设置隐藏值但是当我重新填充父网格时,隐藏值被重置为0.
我通过调用javascript&#39;展开&#39;功能在&#39; RowCreated&#39;事件。这在Add Command和RowDataBound事件之后发生。
现在工作正常。
感谢所有帮助。