ASP.NET:显示来自 C# 代码隐藏的警报
我有一个带有 ac# 代码隐藏的 asp.net 页面。如果 gridview 对象的选定索引发生更改而没有选择“确认”或“取消”,我试图让代码隐藏显示“警报”。用于检测是否选择了确认或取消的代码正在运行,但是我的消息从未显示。 “Alert.Show”代码借自: http://archive.devnewz.com/devnewz-3-20061129JavaScriptAlertShowmessagefromASPNETCodebehind .html
测试时,Alert.show 工作得很好 。例如 page_load(),但不在我的 selected_index_changed 方法中。知道为什么吗?也许与 Alert.Show() 的实现方式有关
if (ChangeAttemptedId && !IsSavedId)
{
Alert.Show("Dispatch assignment saved, but you forgot to click Confirm or Cancel!)");
}
:
<asp:Table ID="Table1" runat="server" CssClass="DefaultTable">
<asp:TableRow runat="server">
<asp:TableCell runat="server" Width="50%" VerticalAlign="Top" HorizontalAlign="Left">
<asp:UpdatePanel ID="detailsUP" runat="server" UpdateMode="Always" ChildrenAsTriggers="True">
<ContentTemplate>
<!--
<asp:Label ID="label1" runat="server" Text="Car To Dispatch: " CssClass="DefaultLabel"></asp:Label>
<asp:DropDownList ID="CarsDDL" runat="server" DataSourceID="VehiclesEDS" DataMember="CarNum" DataTextField="CarNum" AppendDataBoundItems="True" Font-Bold="True">
<asp:ListItem Selected="True" Text="-"></asp:ListItem>
</asp:DropDownList>
-->
<asp:DetailsView ID="RideToAssignDV" runat="server" Height="400px"
Width="400px" AutoGenerateRows="False"
BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px"
CellPadding="3" GridLines="Vertical">
<AlternatingRowStyle BackColor="#DCDCDC" />
<EditRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
<Fields>
<asp:BoundField DataField="AssignedCar" HeaderText="Car"
SortExpression="AssignedCar" NullDisplayText="---" />
<asp:BoundField DataField="Name" HeaderText="Name"
SortExpression="Name" NullDisplayText="---" />
<asp:BoundField DataField="Phone" HeaderText="Phone"
SortExpression="Phone" NullDisplayText="---" />
<asp:BoundField DataField="NumPatrons" HeaderText="Size"
SortExpression="NumPatrons" NullDisplayText="---" />
<asp:BoundField DataField="PickupAddress" HeaderText="Pickup Address"
SortExpression="PickupAddress" NullDisplayText="---" />
<asp:BoundField DataField="DropoffAddress" HeaderText="Drop-Off Address"
SortExpression="DropoffAddress" NullDisplayText="---" />
<asp:BoundField DataField="CreatedBy" HeaderText="Created By"
SortExpression="CreatedBy" NullDisplayText="---" />
<asp:BoundField DataField="TimeOfCall" HeaderText="Call Time"
SortExpression="TimeOfCall" ReadOnly="True" NullDisplayText="---" />
</Fields>
<FooterStyle BackColor="#CCCCCC" ForeColor="Black" BorderStyle="Inset" BorderColor="#C6940D" HorizontalAlign="Center" Height="25px" />
<FooterTemplate>
<asp:Button ID="confirmButton" runat="server" Text="Confirm" ForeColor="Green" HorizontalAlign="Center" OnClick="confirmButton_Click"/>
<asp:Button ID="cancelButton" runat="server" Text="Cancel" ForeColor="Red" HorizontalAlign="Center"
OnClick="cancelButton_Click" OnClientClick="displayTopTen();" />
</FooterTemplate>
<HeaderStyle BackColor="#004812" Font-Bold="True" />
<PagerStyle BackColor="#999999" ForeColor="Black" />
<RowStyle BackColor="#EEEEEE" ForeColor="Black" />
</asp:DetailsView>
</ContentTemplate>
</asp:UpdatePanel>
</asp:TableCell>
<asp:TableCell runat="server" Width="50%">
<asp:UpdatePanel ID="mapUP" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<div id="map_canvas" style="height: 400px; width:400px;"></div>
</ContentTemplate>
</asp:UpdatePanel>
</asp:TableCell>
</asp:TableRow>
</asp:Table>
<br />
<asp:Label ID="GV_Label1" runat="server" Text="Car To Dispatch: " CssClass="DefaultLabel"></asp:Label>
<asp:UpdatePanel ID="SelectCarUP" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:GridView ID="VehiclesGridView" runat="server" AllowPaging="True"
AllowSorting="True" DataSourceID="VehiclesEDS" AutoGenerateColumns="False"
onselectedindexchanged="VehiclesGridView_SelectedIndexChanged"
BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px"
CellPadding="3" GridLines="Vertical" ShowHeaderWhenEmpty="True" AutoPostBack="True">
<AlternatingRowStyle BackColor="#DCDCDC" />
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="GVSelectButton" runat="server" CausesValidation="False"
CommandName="Select" Text="Select"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="CarNum" HeaderText="Car" ReadOnly="True"
SortExpression="CarNum" />
<asp:BoundField DataField="CurrPassengers" HeaderText="Passengers"
ReadOnly="True" SortExpression="CurrPassengers" />
<asp:BoundField DataField="MaxPassengers" HeaderText="Capacity" ReadOnly="True"
SortExpression="MaxPassengers" />
<asp:BoundField DataField="Status" HeaderText="Status" ReadOnly="True"
SortExpression="Status" />
<asp:BoundField DataField="StartAdd" HeaderText="Pick-Up Address"
ReadOnly="True" SortExpression="StartAdd" />
<asp:BoundField DataField="EndAdd" HeaderText="Drop-Off Address"
ReadOnly="True" SortExpression="EndAdd" />
<asp:BoundField DataField="AvgRideTime" HeaderText="Avg. Ride Time"
ReadOnly="True" SortExpression="AvgRideTime" />
</Columns>
<FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
<HeaderStyle BackColor="#004812" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<RowStyle BackColor="#EEEEEE" ForeColor="Black" />
<SelectedRowStyle BackColor="#C6940D" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#C6940D" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#9F770B" />
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
I have an asp.net page with a c# code-behind. I am trying to have the code-behind display an 'alert' if the selected-index of a gridview object is changed without selecting 'confirm' or 'cancel'. The code for detecting if confirm or cancel was selected is working, however my message is never displayed. The 'Alert.Show" code was borrowed from: http://archive.devnewz.com/devnewz-3-20061129JavaScriptAlertShowmessagefromASPNETCodebehind.html .
Alert.show works just fine when tested from the page_load(), for example, but not in my selected_index_changed method. Any idea why? Perhaps having to do with how Alert.Show() is implemented?
if (ChangeAttemptedId && !IsSavedId)
{
Alert.Show("Dispatch assignment saved, but you forgot to click Confirm or Cancel!)");
}
ASP.NET CODE:
<asp:Table ID="Table1" runat="server" CssClass="DefaultTable">
<asp:TableRow runat="server">
<asp:TableCell runat="server" Width="50%" VerticalAlign="Top" HorizontalAlign="Left">
<asp:UpdatePanel ID="detailsUP" runat="server" UpdateMode="Always" ChildrenAsTriggers="True">
<ContentTemplate>
<!--
<asp:Label ID="label1" runat="server" Text="Car To Dispatch: " CssClass="DefaultLabel"></asp:Label>
<asp:DropDownList ID="CarsDDL" runat="server" DataSourceID="VehiclesEDS" DataMember="CarNum" DataTextField="CarNum" AppendDataBoundItems="True" Font-Bold="True">
<asp:ListItem Selected="True" Text="-"></asp:ListItem>
</asp:DropDownList>
-->
<asp:DetailsView ID="RideToAssignDV" runat="server" Height="400px"
Width="400px" AutoGenerateRows="False"
BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px"
CellPadding="3" GridLines="Vertical">
<AlternatingRowStyle BackColor="#DCDCDC" />
<EditRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
<Fields>
<asp:BoundField DataField="AssignedCar" HeaderText="Car"
SortExpression="AssignedCar" NullDisplayText="---" />
<asp:BoundField DataField="Name" HeaderText="Name"
SortExpression="Name" NullDisplayText="---" />
<asp:BoundField DataField="Phone" HeaderText="Phone"
SortExpression="Phone" NullDisplayText="---" />
<asp:BoundField DataField="NumPatrons" HeaderText="Size"
SortExpression="NumPatrons" NullDisplayText="---" />
<asp:BoundField DataField="PickupAddress" HeaderText="Pickup Address"
SortExpression="PickupAddress" NullDisplayText="---" />
<asp:BoundField DataField="DropoffAddress" HeaderText="Drop-Off Address"
SortExpression="DropoffAddress" NullDisplayText="---" />
<asp:BoundField DataField="CreatedBy" HeaderText="Created By"
SortExpression="CreatedBy" NullDisplayText="---" />
<asp:BoundField DataField="TimeOfCall" HeaderText="Call Time"
SortExpression="TimeOfCall" ReadOnly="True" NullDisplayText="---" />
</Fields>
<FooterStyle BackColor="#CCCCCC" ForeColor="Black" BorderStyle="Inset" BorderColor="#C6940D" HorizontalAlign="Center" Height="25px" />
<FooterTemplate>
<asp:Button ID="confirmButton" runat="server" Text="Confirm" ForeColor="Green" HorizontalAlign="Center" OnClick="confirmButton_Click"/>
<asp:Button ID="cancelButton" runat="server" Text="Cancel" ForeColor="Red" HorizontalAlign="Center"
OnClick="cancelButton_Click" OnClientClick="displayTopTen();" />
</FooterTemplate>
<HeaderStyle BackColor="#004812" Font-Bold="True" />
<PagerStyle BackColor="#999999" ForeColor="Black" />
<RowStyle BackColor="#EEEEEE" ForeColor="Black" />
</asp:DetailsView>
</ContentTemplate>
</asp:UpdatePanel>
</asp:TableCell>
<asp:TableCell runat="server" Width="50%">
<asp:UpdatePanel ID="mapUP" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<div id="map_canvas" style="height: 400px; width:400px;"></div>
</ContentTemplate>
</asp:UpdatePanel>
</asp:TableCell>
</asp:TableRow>
</asp:Table>
<br />
<asp:Label ID="GV_Label1" runat="server" Text="Car To Dispatch: " CssClass="DefaultLabel"></asp:Label>
<asp:UpdatePanel ID="SelectCarUP" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:GridView ID="VehiclesGridView" runat="server" AllowPaging="True"
AllowSorting="True" DataSourceID="VehiclesEDS" AutoGenerateColumns="False"
onselectedindexchanged="VehiclesGridView_SelectedIndexChanged"
BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px"
CellPadding="3" GridLines="Vertical" ShowHeaderWhenEmpty="True" AutoPostBack="True">
<AlternatingRowStyle BackColor="#DCDCDC" />
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="GVSelectButton" runat="server" CausesValidation="False"
CommandName="Select" Text="Select"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="CarNum" HeaderText="Car" ReadOnly="True"
SortExpression="CarNum" />
<asp:BoundField DataField="CurrPassengers" HeaderText="Passengers"
ReadOnly="True" SortExpression="CurrPassengers" />
<asp:BoundField DataField="MaxPassengers" HeaderText="Capacity" ReadOnly="True"
SortExpression="MaxPassengers" />
<asp:BoundField DataField="Status" HeaderText="Status" ReadOnly="True"
SortExpression="Status" />
<asp:BoundField DataField="StartAdd" HeaderText="Pick-Up Address"
ReadOnly="True" SortExpression="StartAdd" />
<asp:BoundField DataField="EndAdd" HeaderText="Drop-Off Address"
ReadOnly="True" SortExpression="EndAdd" />
<asp:BoundField DataField="AvgRideTime" HeaderText="Avg. Ride Time"
ReadOnly="True" SortExpression="AvgRideTime" />
</Columns>
<FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
<HeaderStyle BackColor="#004812" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<RowStyle BackColor="#EEEEEE" ForeColor="Black" />
<SelectedRowStyle BackColor="#C6940D" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#C6940D" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#9F770B" />
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
描述
假设我理解你的问题。
您可以使用
ScriptManager
显示javascript
警报消息。示例
详细信息
Description
Assuming i understand your question.
You can use the
ScriptManager
to show ajavascript
alert message.Sample
More Information
它可能有用:
http://www.codeproject .com/Questions/311503/如何使用-javascript-alert-message-in-code-behind
It can be useful:
http://www.codeproject.com/Questions/311503/How-to-use-javascript-alert-message-in-code-behind
如果您希望在页面加载时运行一段客户端 JavaScript,您可以在代码隐藏中注册一个启动脚本:
ASP.NET 将负责放置
在 HTML 中并在页面加载时调用它。
If you want a single piece of client-side JavaScript to run when the page loads, you can register a startup script in your code-behind:
ASP.NET will take care of putting the
<script>
in your HTML and calling it when the page is loaded.如果应通过更改索引引发
selected_index_changed
事件,则您的asp:ListBox
必须具有AutoPostBack="True"
。例如
Your
asp:ListBox
must haveAutoPostBack="True"
if theselected_index_changed
event should be raised by changing index.for example