プリンタリスト画面の Web アプリのコードです。
VB
Private Sub PrinterDataSource_Selected(
sender As Object, e As ObjectDataSourceStatusEventArgs) Handles PrinterDataSource.Selected
Me.CountLabel.Text = String.Format("{0} 個のオブジェクト", DirectCast(e.ReturnValue, ICollection).Count)
End Sub
Private Sub DetailDataSource_Selecting(
sender As Object, e As ObjectDataSourceSelectingEventArgs) Handles DetailDataSource.Selecting
If Me.PrinterListBox.SelectedIndex = -1 Then
e.Cancel = True
End If
End Sub
Private Sub DetailDataSource_Selected(
sender As Object, e As ObjectDataSourceStatusEventArgs) Handles DetailDataSource.Selected
Dim printer = DirectCast(e.ReturnValue, PrintQueue)
ViewState("Speed") = printer.Speed
ViewState("MaxResolution") = printer.MaxResolution
End Sub
Protected Sub TextBox_TextChanged(sender As Object, e As EventArgs)
Dim txt = DirectCast(sender, TextBox)
Dim stateKey = txt.ID.Replace("TextBox", String.Empty)
Dim value As Integer
If Int32.TryParse(txt.Text, value) = False Then '整数に変換できない時
Dim d As Decimal
If Decimal.TryParse(txt.Text, d) Then '実数には変換できる時
txt.Text = Decimal.ToInt32(Decimal.Truncate(d)).ToString() '小数部を切捨て
Else '実数にも変換できない時
txt.Text = Convert.ToString(ViewState(stateKey)) '変更前の値に戻す
txt.Focus()
End If
End If
ViewState(stateKey) = txt.Text
End Sub
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Response.Redirect("Main.aspx?idx=4")
End Sub
C#
protected void PrinterDataSource_Selected(object sender, ObjectDataSourceStatusEventArgs e)
{
this.CountLabel.Text = String.Format("{0} 個のオブジェクト", ((ICollection)e.ReturnValue).Count);
}
protected void DetailDataSource_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
{
if (this.PrinterListBox.SelectedIndex == -1)
{
e.Cancel = true;
}
}
protected void DetailDataSource_Selected(object sender, ObjectDataSourceStatusEventArgs e)
{
var printer = (PrintQueue)e.ReturnValue;
ViewState["Speed"] = printer.Speed;
ViewState["MaxResolution"] = printer.MaxResolution;
}
protected void TextBox_TextChanged(object sender, EventArgs e)
{
var txt = (TextBox)sender;
var stateKey = txt.ID.Replace("TextBox", String.Empty);
int value;
if (Int32.TryParse(txt.Text, out value) == false) //整数に変換できない時
{
decimal d;
if (Decimal.TryParse(txt.Text, out d)) //実数には変換できる時
{
txt.Text = Decimal.ToInt32(Decimal.Truncate(d)).ToString(); //小数部を切捨て
}
else //実数にも変換できない時
{
txt.Text = Convert.ToString(ViewState[stateKey]); //変更前の値に戻す
txt.Focus();
}
}
ViewState[stateKey] = txt.Text;
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("Main.aspx?idx=4");
}