AS.NET Alerts:how to display message box es from server-side code?

16145 ワード

Pasted from:
http://weblogs.asp.net/bleroy/archive/2005/12/01/432016.aspx
 
Onee of the most common mistake s beginer AS.NET developers makes to call MsgBox.Showfrom their AS.NETserver-side code.It is a mistake because e ththiscode corver-side and will jll just just display thethethesasasasasasasasasasasasasasasasasasasasasasasasasasasasasasasasasasasasasasasasasasasasasasasasasasasasasasasasasasasasasasasasasasasasasasasasasasasaleleleleleleleleleleleleleleleleleleleleleion.
Onese the developers understand the disconnected way HTTP works,it becommes quite nature,but the need to trigger an alert box from the server-side remans.
That's why I just pusblished a new Got DotNet workSpace that contains an Alert server control and a Confirmt on server control that you can use both client-side.The control shord fider.The control。 modal dialog feature.
Here's how you use the controlls…
First,if You want to show alert from server-side code,you've got to first declare the alert in your page's markp(or new up new CodePlexプロジェクト.Samples.Alert control and seits propertrators and propertrapt's。
<ms:Alert ID="ServerAlert" runat="server" Buttons="OK" Title="Server Alert" OnChoice="ServerAlertChoice"
Font-Names="Arial" HorizontalAlign="Center" Width="350px" Height="100px">
This is a rich alert box that was triggered by<br /><i>server-side code</i>.<br />
</ms:Alert>
.csharpcode.csharpcode pre&唴13;
{啝13
font-size:small;&啱13;
カラー:black;13;
font-family:consosolas、「Courier New」、courier、monoospace;13;
background-カラー:&菗ffff;13;
//white-space:pre;*/&嗳13;
}&{13;
.csharp code pre{margin:0 em;}&萶13;
.csharrpcode.rem{color=ble 008000;}&菗13;
.csharpcode.kwrd{color=blue 0000 ff}&菗13;
.csharpcode.str{color=blue 006080}&菷13;
.csharpcode.op{color=0000 c 0}&菗13;
.csharpcode.preproc{color=red}13;
.csharpcode.asp{background-カラー}13;
.csharrpcode.{啚80 0000;}13;
.csharpcode.atr{color=blue}13;
.csharpcode.alt&啝13;
{啝13
background-f 4 f 4;13;
width:100%;13;
margin:0 em;13;
}&{13;
.csharpcode.lnum{color=blue 6060}&荆13;
And then simply call the Show()method on the Alert instance from server-side code:
ServerAlert.Show();
You can handle the user's check server-side by handling the OnChoice event and act accodingly:
public void ServerAlertChoice(object sender, AlertChoiceEventArgs e) {
  AlertResult.Text = "You clicked " + e.Result.ToString();
  AlertResult.Visible = true;
}
.csharpcode.csharpcode pre&唴13;
{啝13
font-size:small;&啱13;
カラー:black;13;
font-family:consosolas、「Courier New」、courier、monoospace;13;
background-カラー:&菗ffff;13;
//white-space:pre;*/&嗳13;
}&{13;
.csharp code pre{margin:0 em;}&萶13;
.csharrpcode.rem{color=ble 008000;}&菗13;
.csharpcode.kwrd{color=blue 0000 ff}&菗13;
.csharpcode.str{color=blue 006080}&菷13;
.csharpcode.op{color=0000 c 0}&菗13;
.csharpcode.preproc{color=red}13;
.csharpcode.asp{background-カラー}13;
.csharrpcode.{啚80 0000;}13;
.csharpcode.atr{color=blue}13;
.csharpcode.alt&啝13;
{啝13
background-f 4 f 4;13;
width:100%;13;
margin:0 em;13;
}&{13;
.csharpcode.lnum{color=blue 6060}&荆13;
You can also trigger the alert's display client-side and handle its reponse client-side:
<ms:Alert ID="ClientAlert" runat="server" Buttons="YesNoCancel" Title="Client Alert"
  Font-Names="Arial" HorizontalAlign="Center" Width="350px" Height="140px">
  This is a rich alert box that was triggered by<br /><i>client-side code</i>.<br />
  The button will take the value that you chose.<br /><br />
</ms:Alert>
<input type="button" name="ClientShowButton" value="Show alert without posting back"
onclick="this.value=<%= ClientAlert.GetShowClientEvent() %>;" /><br />
.csharpcode.csharpcode pre&唴13;
{啝13
font-size:small;&啱13;
カラー:black;13;
font-family:consosolas、「Courier New」、courier、monoospace;13;
background-カラー:&菗ffff;13;
//white-space:pre;*/&嗳13;
}&{13;
.csharp code pre{margin:0 em;}&萶13;
.csharrpcode.rem{color=ble 008000;}&菗13;
.csharpcode.kwrd{color=blue 0000 ff}&菗13;
.csharpcode.str{color=blue 006080}&菷13;
.csharpcode.op{color=0000 c 0}&菗13;
.csharpcode.preproc{color=red}13;
.csharpcode.asp{background-カラー}13;
.csharrpcode.{啚80 0000;}13;
.csharpcode.atr{color=blue}13;
.csharpcode.alt&啝13;
{啝13
background-f 4 f 4;13;
width:100%;13;
margin:0 em;13;
}&{13;
.csharpcode.lnum{color=blue 6060}&荆13;
Or any cobination that's useful for you.
You can also use the ConfirmButton as you would use a reglar button(it deries from Button)to ask the user for confirmation before posting back to the server:
<ms:ConfirmButton ID="Confirm" runat="server" OnClick="ConfirmClick"
Text="Confirm Button" ConfirmText="Are you sure you want to press this button?" />
.csharpcode.csharpcode pre&唴13;
{啝13
font-size:small;&啱13;
カラー:black;13;
font-family:consosolas、「Courier New」、courier、monoospace;13;
background-カラー:&菗ffff;13;
//white-space:pre;*/&嗳13;
}&{13;
.csharp code pre{margin:0 em;}&萶13;
.csharrpcode.rem{color=ble 008000;}&菗13;
.csharpcode.kwrd{color=blue 0000 ff}&菗13;
.csharpcode.str{color=blue 006080}&菷13;
.csharpcode.op{color=0000 c 0}&菗13;
.csharpcode.preproc{color=red}13;
.csharpcode.asp{background-カラー}13;
.csharrpcode.{啚80 0000;}13;
.csharpcode.atr{color=blue}13;
.csharpcode.alt&啝13;
{啝13
background-f 4 f 4;13;
width:100%;13;
margin:0 em;13;
}&{13;
.csharpcode.lnum{color=blue 6060}&荆13;
And so that you can use such confirm buttons in a repeated control such as a GridView、 instances of a repeated confirm button can reference a single alert to save HTML rendeng and not repeat the alert text for each data row:
<asp:GridView AutoGenerateColumns="False" DataSourceID="Datasource1"
  ID="GridView1" runat="server" DataKeyNames="ID">
  <SelectedRowStyle BackColor="gray" />
  <Columns>
    <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True"
      SortExpression="ID" Visible="False"/>
    <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name"/>
    <asp:BoundField DataField="Alias" HeaderText="Alias" SortExpression="Alias"/>
    <asp:TemplateField HeaderText="Command">
      <ItemTemplate>
        <ms:ConfirmButton ID="Confirm" runat="server"
          Alert="GridSelectConfirmAlert" CommandName="select"
          Text="Select" /><br />
      </ItemTemplate>
    </asp:TemplateField>
  </Columns>
</asp:GridView>

<ms:Alert ID="GridSelectConfirmAlert" runat="server" Buttons="YesNo" Title="Are you sure?"
  Font-Names="Arial" HorizontalAlign="Center" Width="320px" Height="50px">
  Are you sure you want to select this row?
</ms:Alert>

.csharpcode.csharpcode pre&唴13;
{啝13
font-size:small;&啱13;
カラー:black;13;
font-family:consosolas、「Courier New」、courier、monoospace;13;
background-カラー:&菗ffff;13;
//white-space:pre;*/&嗳13;
}&{13;
.csharp code pre{margin:0 em;}&萶13;
.csharrpcode.rem{color=ble 008000;}&菗13;
.csharpcode.kwrd{color=blue 0000 ff}&菗13;
.csharpcode.str{color=blue 006080}&菷13;
.csharpcode.op{color=0000 c 0}&菗13;
.csharpcode.preproc{color=red}13;
.csharpcode.asp{background-カラー}13;
.csharrpcode.{啚80 0000;}13;
.csharpcode.atr{color=blue}13;
.csharpcode.alt&啝13;
{啝13
background-f 4 f 4;13;
width:100%;13;
margin:0 em;13;
}&{13;
.csharpcode.lnum{color=blue 6060}&荆13;
.csharpcode.csharpcode pre&唴13;
{啝13
font-size:small;&啱13;
カラー:black;13;
font-family:consosolas、「Courier New」、courier、monoospace;13;
background-カラー:&菗ffff;13;
//white-space:pre;*/&嗳13;
}&{13;
.csharp code pre{margin:0 em;}&萶13;
.csharrpcode.rem{color=ble 008000;}&菗13;
.csharpcode.kwrd{color=blue 0000 ff}&菗13;
.csharpcode.str{color=blue 006080}&菷13;
.csharpcode.op{color=0000 c 0}&菗13;
.csharpcode.preproc{color=red}13;
.csharpcode.asp{background-カラー}13;
.csharrpcode.{啚80 0000;}13;
.csharpcode.atr{color=blue}13;
.csharpcode.alt&啝13;
{啝13
background-f 4 f 4;13;
width:100%;13;
margin:0 em;13;
}&{13;
.csharpcode.lnum{color=blue 6060}&荆13;
Check out the Alert.aspx page for these examples in context.
I hope this is useful.As always、feedback is welcome.
Microsoft
UPDATE:The first release was missing a file.I've corected the version in source control and the release file.Thanks for poingthat.
UPDATE 2:the project migrated to CodePlex.