gpt4 book ai didi

c# - 将 javascript 更新限制为仅 asp :UpdatePanel

转载 作者:行者123 更新时间:2023-11-28 02:24:35 25 4
gpt4 key购买 nike

我在 UpdatePanel 中有一个 GridView ,并使用 javascript 在用户单击展开按钮时切换行的可见性。 GridView 上方有相当多的信息,但我只想更新相应 UpdatePanel 中的信息,以便屏幕保持原样,但 JavaScript 调用导致整个屏幕刷新并且窗口跳回顶部。

我用于 GridView 的代码实际上来自 CodeProject ( http://www.codeproject.com/Articles/12299/ExtGridView ) 上的 ExtGridView 控件 - 它变成了最后一个 asp:TemplateField GridView 到其他项目下方的新行中。

我对 JavaScript 很陌生,对 ASP.NET 也很陌生,所以我可能会错过一些简单的东西。有没有办法让 JavaScript 刷新到导致它的相应 UpdatePanel

这是 JavaScript 代码:

<script type="text/javascript">
//<![CDATA[
function TglRow(ctl)
{
var row = ctl.parentNode.parentNode;
var tbl = row.parentNode;
var crow = tbl.rows[row.rowIndex + 1];
var ihExp = ctl.parentNode.getElementsByTagName('input').item(0);

tbl = tbl.parentNode;

var expandClass = tbl.attributes.getNamedItem('expandClass').value;
var collapseClass = tbl.attributes.getNamedItem('collapseClass').value;
var expandText = tbl.attributes.getNamedItem('expandText').value;
var collapseText = tbl.attributes.getNamedItem('collapseText').value;

if (crow.style.display == 'none')
{
crow.style.display = '';
ctl.innerHTML = collapseText;
ctl.className = collapseClass;
ihExp.value = '1';
}
else
{
crow.style.display = 'none';
ctl.innerHTML = expandText;
ctl.className = expandClass;
ihExp.value = '';
}
}//]]>
</script>

这是 GridView 的摘录:

    <asp:UpdatePanel ID="UpdatePanelChapter11" runat="server" 
ChildrenAsTriggers="false" UpdateMode="Conditional">
<ContentTemplate>
<cc1:ExtGridView ID="gvChapter11" runat="server" AutoGenerateColumns="False" DataSourceID="odsChapter11"
DataKeyNames="pkChapter11ID" ShowFooter="True" SkinID="GridViewSKin" Width="85%"
onrowcommand="gvChapter11_RowCommand"
onrowdatabound="gvChapter11_RowDataBound"
onrowupdating="gvChapter11_RowUpdating"
CollapseButtonCssClass="GridCollapseButton"
ExpandButtonCssClass="GridExpandButton" CollapseButtonText=""
ExpandButtonText="" onrowcreated="gvChapter11_RowCreated">
<Columns>
<asp:TemplateField HeaderText="Name of<br/>Party" SortExpression="Name">
<EditItemTemplate>
<asp:TextBox ID="tbName" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
<br /><asp:RequiredFieldValidator ID="tbNameValidator" runat="server" ErrorMessage="*Name Required" ControlToValidate="tbName"
Display="Dynamic" CssClass="Error" ValidationGroup="SaveChapter11Validation"></asp:RequiredFieldValidator>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="tbName" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
<br /><asp:RequiredFieldValidator ID="tbNameValidator" runat="server" ErrorMessage="*Name Required" ControlToValidate="tbName"
Display="Dynamic" CssClass="Error" ValidationGroup="NewChapter11Validation"></asp:RequiredFieldValidator>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<EditItemTemplate>
<asp:LinkButton CssClass="Button" ID="SaveLink" runat="server" CommandName="Update" Text="Save" ValidationGroup="SaveChapter11Validation"></asp:LinkButton>
<asp:LinkButton CssClass="Button" ID="CancelLink" runat="server" CommandName="Cancel" Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton CssClass="Button" ID="EditLink" runat="server" CommandName="Edit" Text="Edit"></asp:LinkButton>
<asp:LinkButton CssClass="Button" ID="DeleteLink" runat="server" CommandName="Delete" Text="Delete"
OnClientClick="return confirm('Are you sure you want to delete this entry?');" ></asp:LinkButton>
</ItemTemplate>
<FooterTemplate>
<asp:LinkButton CssClass="Button" ID="AddLink" runat="server" CommandName="Insert" Text="<<< Add" ValidationGroup="NewChapter11Validation"></asp:LinkButton>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Plan">
<EditItemTemplate>
<div class="ExtGridRow" style="vertical-align:top;">Plan: </div>
<asp:TextBox ID="tbPlan" runat="server" Text='<%# Bind("Plan") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<div class="ExtGridRow" style="vertical-align:top;">Plan: </div>
<asp:Label ID="Label9" runat="server" Text='<%# Eval("Plan") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<div class="ExtGridRow" style="vertical-align:top;">Plan: </div>
<asp:TextBox ID="tbPlan" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</cc1:ExtGridView>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="gvChapter11" EventName="DataBound" />
</Triggers>
</asp:UpdatePanel>

最佳答案

好吧……再用一下ExtGridView代码,我就明白了。调用 JavaScript 的展开/切换按钮也分配了一个 HRef,充当链接,因此当单击它时,它不仅会调用 JavaScript,而且还会像单击链接一样运行,从而导致页面刷新。

来自 ExtGridView 的罪魁祸首代码:

       // ...
else if (RowType == DataControlRowType.DataRow || RowType == DataControlRowType.Footer)
{
_expCell = new TableCell();

_ctlExpand = new HtmlAnchor();
//_ctlExpand.HRef = "#"; -- Commenting this out worked!
_ctlExpand.Attributes["onclick"] = "TglRow(this);";

_ihExp = new HtmlInputHidden();
_ihExp.ID = "e" + this.DataItemIndex.ToString();

_expCell.Controls.Add(_ctlExpand);
_expCell.Controls.Add(_ihExp);
}

if (_expCell != null)
{
_expCell.Width = Unit.Pixel(20);
Cells.AddAt(0, _expCell);
}

关于c# - 将 javascript 更新限制为仅 asp :UpdatePanel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15143316/

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