我无法弄清楚如何在UpdatePanel
中使用SqlDataSource.Update()来避免页面刷新。这是应用程序结构:
Div
点击事件调用ASP:Button
> ASP:Button
OnClick事件调用后面的SqlDataSource.Update()代码>数据库列已更新
我试图在不刷新页面的情况下完成所有这些操作。这是我的代码:
Default.aspx的:
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" EnablePartialRendering="true"/>
<asp:Timer ID="Timer1" runat="server" Interval="10000" OnTick="Timer1_Tick" Enabled="True" />
<div class="square" id="myId" runat="server">
<div class="content">
<div class="table">
<div class="table-cell">
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="true">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
<asp:AsyncPostBackTrigger ControlID="myBtn" EventName="Click" />
</Triggers>
<ContentTemplate>
<asp:Button ID="myBtn" runat="server" Text="Button" OnClick="my_Click" />
<p id="MyText" class="p">Unavailable for Dispatch</p>
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" DataSourceID="SqlDataSource1" CssClass="Grid" RowStyle-HorizontalAlign="Center" DataKeyNames="PhysicalAddress">
<Fields>
<asp:BoundField DataField="OwnerName" HeaderText="OwnerName" ShowHeader="False" SortExpression="OwnerName">
<ControlStyle BorderStyle="None" />
<ItemStyle BorderStyle="None" />
</asp:BoundField>
<asp:BoundField DataField="Building" HeaderText="Building" ShowHeader="False" SortExpression="Building" ItemStyle-CssClass="building">
<ControlStyle BorderStyle="None" />
<ItemStyle BorderStyle="None" />
</asp:BoundField>
<asp:BoundField DataField="TimeOn" HeaderText="TimeOn" ShowHeader="False" SortExpression="TimeOn">
<ControlStyle BorderStyle="None" />
<ItemStyle BorderStyle="None" />
</asp:BoundField>
<asp:BoundField DataField="IPAddress" HeaderText="IPAddress" ShowHeader="False" SortExpression="IPAddress" ItemStyle-CssClass="hide">
<ControlStyle BorderStyle="None" />
<ItemStyle BorderStyle="None" />
</asp:BoundField>
<asp:BoundField DataField="PhysicalAddress" HeaderText="PhysicalAddress" ShowHeader="False" ReadOnly="True" SortExpression="PhysicalAddress" ItemStyle-CssClass="hide">
<ItemStyle CssClass="hide" />
</asp:BoundField>
<asp:BoundField DataField="Available" HeaderText="Available" ShowHeader="False" SortExpression="Available" ItemStyle-CssClass="hide">
<ItemStyle CssClass="hide" />
</asp:BoundField>
</Fields>
<RowStyle HorizontalAlign="Center" />
</asp:DetailsView>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</div>
</div>
</div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:MyDbConnectionString %>" SelectCommand="SELECT [OwnerName], [Building], [TimeOn], [IPAddress], [PhysicalAddress], [Available] FROM [MyDb] WHERE ([OwnerName] = @OwnerName)" UpdateCommand="UPDATE [MyDb] SET [Available] = CASE WHEN Available = '0' THEN '1' ELSE '0' END WHERE ([OwnerName] = @OwnerName)"> <SelectParameters> <asp:Parameter DefaultValue="My Name" Name="OwnerName" Type="String" /> </SelectParameters> <UpdateParameters> <asp:Parameter Name="OwnerName" /> </UpdateParameters> </asp:SqlDataSource>
jQuery Div
点击事件:
$('#myId').on('click', (function (clickEvent) {
document.getElementById("myBtn").click();
})
)
Default.aspx.cs
protected void my_Click(object sender, EventArgs e)
{
SqlDataSource1.UpdateParameters["OwnerName"].DefaultValue = "My Name";
SqlDataSource1.Update();
Response.Redirect(Request.Url.AbsoluteUri);
}
请记住,所有这些都可行,但在ADO.NET事件之后不能刷新整个页面。这是事件输出:
感谢您的任何建议。
答案 0 :(得分:1)
我查看了上面发布的事件输出并回答了我自己的问题。
我将代码隐藏更改为:
protected void my_Click(object sender, EventArgs e) {
SqlDataSource1.UpdateParameters["OwnerName"].DefaultValue = "My Name";
SqlDataSource1.Update();}
...当然我不再称为Response.Redirect。它现在按预期工作。
谢谢!