ASP.NET:显示从C#code-背后警报 [英] ASP.NET : Displaying an alert from C# code-behind

查看:92
本文介绍了ASP.NET:显示从C#code-背后警报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C#code-背后一个asp.net页面。我想有code-背后显示,如果一个GridView对象的选择,指数没有选择确认或取消改变的警报。在code表示,如果确认或选择取消正在检测,但永远不会显示我的消息。在'Alert.Showcode,从借来的:<一href=\"http://archive.devnewz.com/devnewz-3-20061129JavaScriptAlertShowmessagefromASPNET$c$cbehind.html\">http://archive.devnewz.com/devnewz-3-20061129JavaScriptAlertShowmessagefromASPNET$c$cbehind.html

从在Page_Load(测试时

Alert.show工作得很好),例如,而不是在我的selected_index_changed方法。知道为什么吗?或许具有Alert.Show()是如何实现的呢?

 如果(ChangeAttemptedId&安培;&安培;!IsSavedId)
{
 Alert.Show(调度作业保存,但你忘了点击确认或取消!));
}

ASP.NET code:

 &LT; ASP:表ID =表1=服务器的CssClass =DefaultTable&GT;
    &LT; ASP:的TableRow =服务器&GT;
        &LT; ASP:TableCell的=服务器WIDTH =50%VerticalAlign =评出的Horizo​​ntalAlign =左&GT;
            &LT; ASP:的UpdatePanel ID =detailsUP=服务器的UpdateMode =始终ChildrenAsTriggers =真&GT;
                &LT;&的ContentTemplate GT;
                    &LT;! -
                    &LT; ASP:标签ID =LABEL1=服务器文本=汽车调度的CssClass =DefaultLabel&GT;&LT; / ASP:标签&gt;
                    &LT; ASP:DropDownList的ID =CarsDDL=服务器的DataSourceID =VehiclesEDS的DataMember =CarNumDataTextField =CarNumAppendDataBoundItems =真字体粗体=真&GT;
                        &LT; ASP:列表项选择=真正的文本= - &GT;&LT; / ASP:ListItem的&GT;
                    &LT; / ASP:DropDownList的&GT;
                     - &GT;
                    &LT; ASP:DetailsView控件ID =RideToAssignDV=服务器HEIGHT =400像素
                        WIDTH =400像素AutoGenerateRows =FALSE
                        背景色=白BORDERCOLOR =#999999边框=无边框宽度=1像素
                        CELLPADDING =3网格=垂直&GT;
                        &LT; AlternatingRowStyle背景色=#DCDCDC/&GT;
                        &LT; EditRowStyle背景色=#008A8CFONT-粗体=真前景色=白/&GT;
                        LT&;&领域GT;
                            &LT; ASP:BoundField的数据字段=AssignedCar的HeaderText =汽车
                                SORTEX pression =AssignedCarNullDisplayText =---/&GT;
                            &LT; ASP:BoundField的数据字段=名称的HeaderText =姓名
                                SORTEX pression =名称NullDisplayText =---/&GT;
                            &LT; ASP:BoundField的数据字段=手机的HeaderText =手机
                                SORTEX pression =电话NullDisplayText =---/&GT;
                            &LT; ASP:BoundField的数据字段=NumPatrons的HeaderText =大小
                                SORTEX pression =NumPatronsNullDisplayText =---/&GT;
                            &LT; ASP:BoundField的数据字段=PickupAddress的HeaderText =取件地址
                                SORTEX pression =PickupAddressNullDisplayText =---/&GT;
                            &LT; ASP:BoundField的数据字段=DropoffAddress的HeaderText =下车地址
                                SORTEX pression =DropoffAddressNullDisplayText =---/&GT;
                            &LT; ASP:BoundField的数据字段=CreatedBy的HeaderText =创建者
                                SORTEX pression =CreatedByNullDisplayText =---/&GT;
                            &LT; ASP:BoundField的数据字段=TIMEOFCALL的HeaderText =通话时间
                                SORTEX pression =TIMEOFCALL只读=真NullDisplayText =---/&GT;
                        &LT; /场&GT;
                        &LT; FooterStyle背景色=#CCCCCC前景色=黑色边框=插图BORDERCOLOR =#C6940DHorizo​​ntalAlign =中心HEIGHT =25像素/&GT;
                        &LT; FooterTemplate&GT;
                            &LT; ASP:按钮的ID =confirmButton=服务器文本=确认前景色=绿色Horizo​​ntalAlign =中心的OnClick =confirmButton_Click/&GT;
                            &LT; ASP:按钮的ID =cancelButton=服务器文本=取消前景色=红Horizo​​ntalAlign =中心
                                的OnClick =cancelButton_Click的OnClientClick =displayTopTen(); /&GT;
                        &LT; / FooterTemplate&GT;
                        &LT; HeaderStyle背景色=#004812FONT-粗体=真/&GT;
                        &LT; PagerStyle背景色=#999999前景色=黑/&GT;
                        &LT; RowStyle的BackColor =#EEEEEE前景色=黑/&GT;
                    &LT; / ASP:DetailsView控件&GT;
                &LT; /&的ContentTemplate GT;
            &LT; / ASP:的UpdatePanel&GT;
        &LT; / ASP:TableCell的&GT;        &LT; ASP:TableCell的=服务器WIDTH =50%&GT;
            &LT; ASP:的UpdatePanel ID =MapUp活动=服务器的UpdateMode =条件&GT;
                &LT;&的ContentTemplate GT;
                    &LT; D​​IV ID =map_canvas的风格=高度:400像素;宽度:400像素;&GT;&LT; / DIV&GT;
                &LT; /&的ContentTemplate GT;
            &LT; / ASP:的UpdatePanel&GT;
        &LT; / ASP:TableCell的&GT;
    &LT; / ASP:&的TableRow GT;
&LT; / ASP:表&gt;&LT; BR /&GT;
&LT; ASP:标签ID =GV_Label1=服务器文本=汽车调度的CssClass =DefaultLabel&GT;&LT; / ASP:标签&gt;&LT; ASP:的UpdatePanel ID =SelectCarUP=服务器的UpdateMode =条件&GT;
    &LT;&的ContentTemplate GT;
        &LT; ASP:GridView控件ID =VehiclesGridView=服务器AllowPaging =真
            AllowSorting =真的DataSourceID =VehiclesEDS的AutoGenerateColumns =FALSE
            onselectedindexchanged =VehiclesGridView_SelectedIndexChanged
            背景色=白BORDERCOLOR =#999999边框=无边框宽度=1像素
            CELLPADDING =3网格=垂直ShowHeaderWhenEmpty =真的AutoPostBack =真&GT;
            &LT; AlternatingRowStyle背景色=#DCDCDC/&GT;
            &LT;柱体和GT;
                &LT; ASP:的TemplateField ShowHeader =FALSE&GT;
                    &LT;&ItemTemplate中GT;
                        &LT; ASP:LinkBut​​ton的ID =GVSelectButton=服务器的CausesValidation =FALSE
                            的CommandName =选择文本=选择&GT;&LT; / ASP:LinkBut​​ton的&GT;
                    &LT; / ItemTemplate中&GT;
                &LT; / ASP:的TemplateField&GT;
                &LT; ASP:BoundField的数据字段=CarNum的HeaderText =汽车只读=真
                    SORTEX pression =CarNum/&GT;
                &LT; ASP:BoundField的数据字段=CurrPassengers的HeaderText =乘客
                    只读=真SORTEX pression =CurrPassengers/&GT;
                &LT; ASP:BoundField的数据字段=MaxPassengers的HeaderText =容量只读=真
                    SORTEX pression =MaxPassengers/&GT;
                &LT; ASP:BoundField的数据字段=状态的HeaderText =状态只读=真
                    SORTEX pression =状态/&GT;
                &LT; ASP:BoundField的数据字段=StartAdd的HeaderText =提货地址
                    只读=真SORTEX pression =StartAdd/&GT;
                &LT; ASP:BoundField的数据字段=EndAdd的HeaderText =下车地址
                    只读=真SORTEX pression =EndAdd/&GT;
                &LT; ASP:BoundField的数据字段=AvgRideTime的HeaderText =平均换乘时间
                    只读=真SORTEX pression =AvgRideTime/&GT;
            &LT; /专栏&GT;
            &LT; FooterStyle背景色=#CCCCCC前景色=黑/&GT;
            &LT; HeaderStyle背景色=#004812FONT-粗体=真前景色=白/&GT;
            &LT; PagerStyle背景色=#999999前景色=黑Horizo​​ntalAlign =中心/&GT;
            &LT; RowStyle的BackColor =#EEEEEE前景色=黑/&GT;
            &LT; SelectedRowStyle背景色=#C6940DFONT-粗体=真前景色=白/&GT;
            &LT; SortedAscendingCellStyle背景色=#F1F1F1/&GT;
            &LT; SortedAscendingHeaderStyle背景色=#C6940D/&GT;
            &LT; SortedDescendingCellStyle背景色=#CAC9C9/&GT;
            &LT; SortedDescendingHeaderStyle背景色=#9F770B/&GT;
        &LT; / ASP:GridView的&GT;
    &LT; /&的ContentTemplate GT;
&LT; / ASP:的UpdatePanel&GT;


解决方案

说明

假设我明白你的问题。

您可以使用的ScriptManager 来显示的JavaScript 警报消息。

示例

 保护无效的Page_Load(对象发件人,EventArgs的发送)
{
    ScriptManager.RegisterStartupScript(this.Page,this.Page.GetType(),
          ERR_MSG
          警报('派遣作业保存,但你忘了点击确认或取消!)');,
          真正);
}

更多信息

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>

解决方案

Description

Assuming i understand your question.

You can use the ScriptManager to show a javascript alert message.

Sample

protected void Page_Load(object sender, EventArgs e)
{
    ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), 
          "err_msg", 
          "alert('Dispatch assignment saved, but you forgot to click Confirm or Cancel!)');",
          true);
}

More Information

这篇关于ASP.NET:显示从C#code-背后警报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆