Reputation: 145
I want to figure out how can I pass value from input to web services using c#? I have code:
Animal.aspx:
<div class="form-group">
<asp:Label ID="LabelForType" runat="server" Text="Animal Type"></asp:Label>
<asp:TextBox ID="AnimalType" runat="server" CssClass="form-control" Text="Animal Type"></asp:TextBox>
</div>
Animal.aspx.cs:
protected void AddAnimal_Click(object sender, EventArgs e)
{
ClientService service = new ClientService();
service.getAnimalType();
}
public String getAnimalType()
{
return AnimalType.Text.ToString();
}
Web services. ClientService.asmx.cs
[WebMethod]
public String getAnimalType()
{
return animal.getAnimalType();
}
Unfortunately it does not work. If I send my input value as a parameter to a web service. I can type whatever in soap window but it will not return the value which I typed in input. Any ideas? Or maybe there is a tutorial? Thank you.
I am looking how to pass the value from textbox ID="AnimalType" to WebServices. If type Chicken it should pass chicken to web services.
Basically. Create value - > Send it from C# as a parameter to Web Services - > Took that value in Java by calling to Web Services.
I made like.
private String type;
public void setAnimalType(String typee)
{
type = typee;
}
[WebMethod]
public String getAnimalType()
{
return type;
}
But it returns null.
Upvotes: 0
Views: 1439
Reputation: 218798
This isn't doing what you think:
private String type;
public void setAnimalType(String typee)
{
type = typee;
}
[WebMethod]
public String getAnimalType()
{
return type;
}
It makes all the sense in the world from a strictly object-oriented point of view. However, when you're in the context of ASP.NET there are some subtleties of the framework and the stateless nature of HTTP which change things.
In short, every time you invoke your ASP.NET "page", a new instance of that page's class is created. So any class-level values you set on previous instances are gone.
You need to persist that value somewhere. There are many options:
static
variableThe ASP.NET page objects are stateless. With every request an instance is created, interacted with, and destroyed. Values which need to persist across multiple requests need to be persisted outside class-level instance members in this case.
Upvotes: 2
Reputation: 575
Change you service method to take parameters as follow
[WebMethod]
public String getAnimalType(string animal)
{
//Do something with the param
return "something";
}
Then call your service like
ClientService service = new ClientService();
var animalType = service.getAnimalType(AnimalType.Text);
Upvotes: 1