【问题标题】:Send data to a web metod in web service from a form in windows phone 7 application从 windows phone 7 应用程序中的表单将数据发送到 web 服务中的 web 方法
【发布时间】:2014-02-15 16:42:56
【问题描述】:

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

姓名、年龄、sadd、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,它以下列方式包含这些字段:

姓名、街道地址、城市地址、邮政编码、电子邮件地址、电话号码、年龄、性别

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

 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);
        }

}

在其他部分,我希望代码以该 web 方法提交数据,即 registertoteam。我在 else 部分编写的代码不起作用并且也有错误。请在 else 部分编辑我的代码,以便提交数据。谢谢

【问题讨论】:

  • 什么样的错误,编译时或运行时错误?和错误信息?
  • @har07 编译时错误。这是我正在做的正确方法吗?几个月前我从你那里得到了帮助。实际上我现在不在办公室,所以无法准确发布错误。你能在这方面帮助我吗?到目前为止,您为我编写的所有代码都运行良好。你能写下我应该放在else部分的代码吗
  • 在不知道 Web 服务/可以调用它的情况下,没有人可以编写用于该部分的工作代码。这是一个猜测工作(我在你之前关于这个问题的问题中做过)。至少让我知道服务地址
  • @har07 服务地址为:ws.clearwintech.com/aap.asmx,registertoteam地址为:ws.clearwintech.com/aap.asmx?op=registerToTeam
  • @har07 请查看我的服务地址并尝试解决我的问题。

标签: c# forms windows-phone-7


【解决方案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”消息。第一次运行的确认数据已成功保存,因此我无法使用相同的数据再次提交。

更新:

用准确代码更新:

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;
}

【讨论】:

  • 你能写出我应该放在其他部分的确切代码吗?此外,性别字段有一个单选按钮,所以我不知道怎么放。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-16
  • 1970-01-01
相关资源
最近更新 更多