I am trying to bind information from a web service to a tree view in my asp.net website. Using a drop down list, I choose a stock symbol that retrieves that company's information from the web service. I am currently outputting that information as a string.
What I am trying to do is bind it to a tree view dynamically. This is where I'm at:
My aspx.cs file..
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using net.webservicex.www;
using System.Xml;
public partial class _Default : System.Web.UI.Page
{
private net.webservicex.www.StockQuote StockQuote;
private string StockInfo;
private XmlDocument stockDoc = new XmlDocument();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
string symbol = DropDownList1.SelectedItem.Text;
StockQuote = new net.webservicex.www.StockQuote();
StockInfo = StockQuote.GetQuote(symbol);
stockDoc.LoadXml(StockInfo);
test.Text = StockInfo;
}
}
I cannot configure the data source in design view because it's dynamic, and I never used a hierarchical scheme. Do I need to do that? Is there another way?
Use an XmlDataSource and set it's DataFile property to the desired Xml file. Now go to your TreeView and set it's DataSourceID to the ID of XmlDataSource object.
Update
I just noticed that you are getting your Xml from a service. In that case use the Data property of XmlDataSource object to set the data xml.
Update
Declare your XmlDataSource in the aspx markup and set it as the DataSourceID of the TreeView (again in the markup). In the code behind (in an appropriate event), do something like below.
xmlDataSource.Data = StockInfo; // StockInfo is your string variable
treeView.DataBind(); // i am not sure whether this is necessary
Hope this helps.
Related
I currently wanted to upgrade an old ASP.NET Webforms which is using (NVelocity template) to RazorView(template) MVC is not required at the moment. BusinessLogic is already in the code behind of aspx.cs file.
Is it possible to pass Objects and Variables from Webforms to RazorViews:
I have already tried using session(Not Workerd) and querystring(Worked). passing data using Querystring works but on cost of rewriting BusinessLogic which is not an option.
Example:
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
string myData = "DATA FROM ANOTHER UNIVERSE";
ArrayList myList = new ArrayList();
//Possible Function to invoke and pass data to RazorView
RenderDataBridgeToRazorView("defaultRazorTemplate", myData);
//Another way to look at the same functions with Objects
RenderDataBridgeToRazorView("defaultRazorTemplate", myList);
}
}
I have a dropdownlist in my aspx that have an event that sets values to it:
protected void Page_Load(object sender, EventArgs e)
{
DropDownList1.Items.Add("No Image News");
DropDownList1.Items.Add("Small News");
DropDownList1.Items.Add("Medium News");
DropDownList1.Items.Add("Large News");
DropDownList1.DataBind();
}
I have a class file that have a method and I would like to use the dropdownlist inside of the method.
example: If selected dropdownlist value is "Car" do that..
Is it possible to somehow use the dropdownlist from the aspx file inside a class file?
create a function which take DropDownList as an argument
Call that function from you .aspx.cs file.
It will work.
Function will be like this
public void MyControlData(DropDownList ddl)
{
}
Add reference of System.Web in you .cs file where you want to define this function.
Edit 1
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public class MyControlData
{
public void MyControlData(DropDownList ddl)
{
// DO SOMETHING HERE
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication3
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
CustomValidator1.Validate();
}
protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
{
if (RadioButton1.Checked == false && RadioButton2.Checked == false)
args.IsValid = false;
else
args.IsValid = true;
}
}
}
Here is the code I used to program my server side custom validator. I'm having trouble understanding how this works because if I take it out of the IsPostBack "if", it shows up in the summary when I start up the page but when I click a button it doesn't work. Anyone know what might be wrong?
Also as some side info, what is the major difference between server side and client side validation in terms of this type of validation ?
The biggest difference betwen server side and client side validation (besides the obvious) is that client-side validation can prevent you from even submitting a page. Server-side validation only happens once the page has been submitted.
Mixing them can produce the situation where you, the user, have filled in all your fields, made everything the right length and value, and hit submit - and then when the page returns, there are more validation messages!
I have a simple webservice running on a remote server that just takes a couple of strings and integers and updates a SQL Server database. I want to use it from a new ASP.NET web form.
So today I added the remote webservice to a project by adding an App_Webservice. I added the remote wsdl filename to the .NET project (in VS2005), and named the reference wsStoreData. It created a folder called wsStoreData.
The webservice has a function called StoreRecentPage. Here is the simple page code:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.Services;
using System.Web.UI.HtmlControls;
public partial class Training_FinalPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
wsStoreData.StoreRecentPage("1", "11", 18, 100);
}
}
The build fails with "The type or namespace name 'StoreRecentPage' does not exist in the namespace 'wsStoreData' (are you missing an assembly reference?)"
I haven't coded in .NET for a few years, so I may be missing something obvious to you. But I have looked at online examples and they don't seem to do anything different than I have done here.
Thanks for any suggestions.
You can try with this code
protected void Page_Load(object sender, EventArgs e)
{
//Idon't know name of your proxy, but it's your generated class from your Wsdl after
//adding reference
var proxy = new ProxyWebService();//Replace with your proxy class
proxy.StoreRecentPage("1", "11", 18, 100);
}
I put a list box and an image box.
now I want the image to swap every time the user clicks on a different element in the list. It doesnt seem to work
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
string[] pictures = { "~/createWii.jpg",
"~/DKC4_wii.png",
"~/Donkey-Kong-Country-1.jpg",
"~/DSCallOfDutyBlackOps.jpg",
"~/DSPreviewsCodmw2.jpg",
"~/DSPreviewsAliceInWonderLAnds.jpg",
"~/DSPreviewPicross3d.jpg",
"~/createii.jpg",
};
string[] picturesNames = { "picture1", "picture2", "picture3", "picture4", "picture5", "picture6", "picture7", "picture8" };
protected void Page_Load(object sender, EventArgs e)
{
for (int i = 0; i < pictures.Length; i++)
{
ListBox1.Items.Add(new ListItem(picturesNames[i],pictures[i]));
}
Image1.ImageUrl = "~/Donkey-Kong-Country-1.jpg";
ListBox1.DataSource = picturesNames;
ListBox1.DataBind();
}
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
Image1.ImageUrl = pictures[ListBox1.SelectedIndex];// it tells me that there is index out of range each time. why ?
}
}
A couple of things.
1.) you should wrap the code in page_load with
if(!IsPostback)
2.) Make sure on the .aspx that the "AutoPostback" property is set to true on the listbox!
Edit
Per the request in the comments, the reason this is needed is two fold.
ASP.NET ViewState will handle the persistence of the values on postback, therefore, you can use the !IsPostback condition to ensure that the information is only bound once. This prevents any "oddities" from coming up in the future.
By default ListBoxes/DropDownLists/etc do not post back automatically when the user changes a selection. So to actually trigger the event you either need to have a button that does the postback, or update the "AutoPostback" property as I directed to ensure that when the user makes a change that it triggers the server-side code.