通过Windows Phone 7应用程序中的表单将数据发送到Web服务中的Web metod

时间:2014-02-14 12:31:26

标签: c# forms windows-phone-7

我正在为Windows Phone 7构建一个应用程序,其中我有一个包含以下文本字段和1个单选按钮的表单。表单中文本字段的名称为:

姓名,年龄,悲伤,cadd,eadd,phn,zip。单选按钮的xaml是:

 <TextBlock Canvas.Left="20" Canvas.Top="157" Height="44" Name="gen" Text="Gender" Foreground="Black" FontFamily="Verdana" FontSize="24" Width="134" />
 <RadioButton Canvas.Left="139" Canvas.Top="157" FontStyle="Italic" GroupName="Gender" Foreground="Black" Content="Male" Height="71" Name="male" Width="154" />
 <RadioButton Canvas.Left="139" Canvas.Top="207"  FontStyle="Italic" GroupName="Gender" Foreground="Black" Content="Female" Height="71" Name="fem" Width="140" />

现在我想将这些数据发送到web方法registertoteam,其中包含以下方式的这些字段:

name,street_address,city_address,zip_code,email_address,phone_no,年龄,性别

我在提交按钮中编写了以下代码。

 private void submit_Click(object sender, RoutedEventArgs e)
    {
        if (name.Text == String.Empty)
        {
            MessageBox.Show("Please Enter the name");
            name.Focus();
        }

        if (age.Text == String.Empty)
        {
            MessageBox.Show("Please Enter the age");
            age.Focus();
        }

        if (male.IsChecked == true)
        {
            string gender = male.Content.ToString();
        }
        else if (fem.IsChecked == true)
        {
            string gender = fem.Content.ToString();
        }
        else    //none of them is selected.
        {
            MessageBox.Show("Please select your Gender");
        }

        if (sadd.Text == String.Empty)
        {
            MessageBox.Show("Please Enter the Street Address");
            sadd.Focus();
        }

        if (cadd.Text == String.Empty)
        {
            MessageBox.Show("Please Enter the City Address");
            cadd.Focus();
        }

        if (eadd.Text == String.Empty)
        {
            MessageBox.Show("Please Enter the Email Address");
            eadd.Focus();
        }

        if (phn.Text == String.Empty)
        {
            MessageBox.Show("Please Enter the Phone Number");
            phn.Focus();
        }

        if (zip.Text == String.Empty)
        {
            MessageBox.Show("Please Enter the Zipcode");
            zip.Focus();
        }

        else
        {
           var svc = new KejriwalService.aapSoapClient();
           svc.registerToTeamAsync(name.Text, sadd.Text, cadd.Text, zip.Text, eadd.Text, phn.Text, age.Text);
        }

}

在else部分,我希望代码在webmethod中提交数据,即registertoteam。我在else部分写的代码不起作用,也有错误。请在else部分编辑我的代码,以便提交数据。感谢

1 个答案:

答案 0 :(得分:1)

您的代码看起来很好。我测试过这种方式:

var svc = new KejriwalService.aapSoapClient();
svc.registerToTeamAsync("har07", "addr", "city", "zip", "email", "000", "2", "a");
svc.registerToTeamCompleted += (o, args) =>
                               {
                                   MessageBox.Show("Registration Successful");
                                   var result = args.Result;
                               };

没有错误,并且第二次运行相同的代码会导致Web服务在args.Result中返回“emailExists”消息。第1次运行中确认的数据已成功保存,因此我无法再使用相同的数据提交。

更新:

使用完全代码更新:

private void submit_Click(object sender, RoutedEventArgs e)
{
    var gender = "";
    var isValid = ValidateInput(out gender);
    if(isValid)
    {
        var svc = new KejriwalService.aapSoapClient();
        svc.registerToTeamAsync(name.Text, sadd.Text, cadd.Text, 
                            zip.Text, eadd.Text, phn.Text, age.Text, gender);
    }
}

private bool ValidateInput(out string gender)
{
    gender = "";
    if (name.Text == String.Empty)
    {
        MessageBox.Show("Please Enter the name");
        name.Focus();
        return false;
    }
    if (age.Text == String.Empty)
    {
        MessageBox.Show("Please Enter the age");
        age.Focus();
        return false;
    }
    if (male.IsChecked == true)
    {
        gender = male.Content.ToString();
    }
    else if (fem.IsChecked == true)
    {
        gender = fem.Content.ToString();
    }
    else    //none of them is selected.
    {
        MessageBox.Show("Please select your Gender");
        return false;
    }
    if (sadd.Text == String.Empty)
    {
        MessageBox.Show("Please Enter the Street Address");
        sadd.Focus();
        return false;
    }
    if (cadd.Text == String.Empty)
    {
        MessageBox.Show("Please Enter the City Address");
        cadd.Focus();
        return false;
    }
    if (eadd.Text == String.Empty)
    {
        MessageBox.Show("Please Enter the Email Address");
        eadd.Focus();
        return false;
    }
    if (phn.Text == String.Empty)
    {
        MessageBox.Show("Please Enter the Phone Number");
        phn.Focus();
        return false;
    }
    if (zip.Text == String.Empty)
    {
        MessageBox.Show("Please Enter the Zipcode");
        zip.Focus();
        return false;
    }
    return true;
}