gpt4 book ai didi

javascript - 在后面的代码中读取时隐藏控制值不正确

转载 作者:行者123 更新时间:2023-12-03 10:10:36 24 4
gpt4 key购买 nike

我在嵌套 GridView 中有一个隐藏控件来确定它是否展开。我在 javascript 函数中设置了该值。由于我设置了警报,它的设置正确。在 RowDataBound 方法中,我想读取隐藏控件以确定是否应该扩展网格。当我读取 RowDataBound 方法中的值时,它始终为“0”。我的标记很大,所以这只是嵌套网格、UpdatePanel 和隐藏控件关联:

    <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 中设置的。在后面的代码中该值始终为零,即初始值。

为什么当网格展开时,隐藏值在 RowDataBound 事件中永远不会显示为“1”?

更新我将 asp:HiddenField 放入 UpdatePanel 内的 ContentTemplate 内。但它仍然没有正确读取隐藏值。

最佳答案

您将隐藏字段视为位于 GridView 的新绑定(bind)行中。

只要保持简单,不要引用 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["mggvIsExpanded"] 获取值。

关于javascript - 在后面的代码中读取时隐藏控制值不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30132187/

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