Showing posts with label xml. Show all posts
Showing posts with label xml. Show all posts

Wednesday, March 28, 2012

Change datasource for a datagrid using ajax

Hi ppl,

I have a datagrid with XML's as a source file. I have a combo box in the same page and when a user selects a value from the combo box i want to display different stats data without doing a normal postback. I am still learning the updatepanel Ajax control . Can anyone please guide me what i should i do to achieve the functionality? do i need to write any function in js ?

Any help much appreciated

thanks

Place the UpdatePanel around the DataGrid and the Combobox. Make sure the combobox do a postback, set the Autopostback attribute to true. Hook up to the OnSelectedIndexChanged event and get the data based on the selected value and bind it to the DataGrid. When a control inside a UpdatePanel will do a postback, the content of the UpdatePanel will be updated.

If you layout of you page don't allow a UpdatePanel around your combobox, you can instead add a async. postback trigger to the UpdatePanel that will listen to a control's event and update the panel.


Hi Thanks for the response. I placed as the gridview control in the updatepanel and used async trigger as you mentioned. when i changed the dropdownlist the grid doesnt appear at all.

Here is the script and code:

<

divID="Panel1"style="width: 568px; position: static; height: 180px; background-image: url(images/bg-nav.png);">

<spanstyle="color: #ffffff"><strong>

Select L :

</strong></span><asp:DropDownListID="DropDownList1"runat="server"Width="134px"AutoPostBack="True"OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged1"><asp:ListItemSelected="True"Value="1">yes</asp:ListItem><asp:ListItemValue="2">no</asp:ListItem></asp:DropDownList><br/><asp:UpdateProgressID="UpdateProgress1"runat="server"AssociatedUpdatePanelID="test"><ProgressTemplate><divstyle="color: #ffffff; background-color: transparent"><imgsrc="images/indicator.gif"/>

Updating ....

</div></ProgressTemplate></asp:UpdateProgress><asp:UpdatePanelID="test"runat="server"><ContentTemplate><asp:GridViewID="GridView1"runat="server"ForeColor="Silver"BorderColor="LimeGreen"BorderStyle="Inset"AutoGenerateColumns="True"><AlternatingRowStyleBackColor="LightSkyBlue"ForeColor="#404040"/></asp:GridView></ContentTemplate><Triggers><asp:AsyncPostBackTriggerControlID="DropDownList1"EventName="SelectedIndexChanged"/></Triggers></asp:UpdatePanel>

Code:

ProtectedSub DropDownList1_SelectedIndexChanged1(ByVal senderAsObject,ByVal eAs System.EventArgs)If DropDownList1.SelectedValue = 1ThenDim myDataSetAsNew DataSet()

myDataSet.ReadXml(Server.MapPath(

"test1.xml"))

GridView1.DataSource = myDataSet

GridView1.DataBind()

ElseDim myDataSetAsNew DataSet()

myDataSet.ReadXml(Server.MapPath(

"test2.xml"))

GridView1.DataSource = myDataSet

GridView1.DataBind()

EndIf

EndSub

When i give wrong XML File name, it comes up with the message saying that it cant find the xml file. Is it something i missing in grid control?

Any help much appreciated?


I copied your code, created 2 example XML files, and everything worked fine for me. When I select a value the GirdView was displayed with the correct XML file.

Hi Fredreik

Can you post your sample XML files? I tried using without AJAX functionality and its working fine :(

thanks


I can't think it has anything to do with your XML files, btw do you use the ASP.Net Ajax version 1.0 RTM, becuase your code runs on my machine with no problems at all so I can't see why it shouldn't work on yours.. maybe you have some more code that I don't have.. I only copy the code you posted.. mabye you do something in code-behind etc..!?

Wednesday, March 21, 2012

CascadingDropDown options not showing.

I have 2 dropdown boxes with the CascadingDropDown ajax extension attached to them, they are reading from an xml file not database. I dont get an error but my first dropdown bar shows "Please select an option" ie my prompt text, but not the xml options... my xml is set out correctly (ie the same hierarchical XML layout as the sample ajax website). Any ideas why this isnt showing me options but doesnt error? thanks in advance John

i feel as though i should explain more. Basically ive got an xml file layed out like :

<?xml version="1.0" encoding="utf-8" ?>
<GroupService>
<Group name="One">
<SubGroup name="One" />
<SubGroup name="Two" />
</Group>
</Service>

Based on two categorys. I have two drop down boxes with the code :

<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
<ajaxToolkit:CascadingDropDown ID="CascadingDropDown1" runat="server" TargetControlID="DropDownList1"
Category="Group" PromptText="Please select a Type" LoadingText="[Loading makes...]"
ServicePath="GroupsService.asmx" ServiceMethod="GetDropDownContents" /
<asp:DropDownList ID="DropDownList2" runat="server">
</asp:DropDownList>
<ajaxToolkit:CascadingDropDown ID="CascadingDropDown2" runat="server" TargetControlID="DropDownList2"
Category="SubGroup" PromptText="Please select a Group Type" LoadingText="[Loading models...]"
ServiceMethod="GroupsService.asmx" ParentControlID="DropDownList1" /
The ServiceMethod is correctly referance.Now my webservice is :

using System;
using System.Web;
using System.Collections;
using System.Collections.Specialized;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml;

/// <summary>
/// Summary description for GroupsService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class GroupsService : System.Web.Services.WebService
{
// Member variables
private static XmlDocument _document;
private static object _lock = new object();

// we make these public statics just so we can call them from externally for the
// page method call
public static XmlDocument Document
{
get
{
lock (_lock)
{
if (_document == null)
{
// Read XML data from disk
_document = new XmlDocument();
_document.Load(HttpContext.Current.Server.MapPath("~/App_Data/GroupsService.xml"));
}
}
return _document;
}
}

public static string[] Hierarchy
{
get { return new string[] {"Group", "SubGroup" }; }
}

public GroupsService () {

//Uncomment the following line if using designed components
//InitializeComponent();
}

[WebMethod]
public AjaxControlToolkit.CascadingDropDownNameValue[] GetDropDownContents(string knownCategoryValues, string category)
{
// Get a dictionary of known category/value pairs
StringDictionary knownCategoryValuesDictionary = AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);

// Perform a simple query against the data document
return AjaxControlToolkit.CascadingDropDown.QuerySimpleCascadingDropDownDocument(Document, Hierarchy, knownCategoryValuesDictionary, category);
}

}

It doesnt give me an error and it says its loading but then no items are shown, has anyone else had this error? thanks in advance John


did a little bit of work on this and saw in the sample website i got with the ajax download the hierarchy is :

public static string[] Hierarchy
{
get { return new string[] { "make", "model" }; }
}

it misses out the last value ie color of the colours. so i change mine to :

public static string[] Hierarchy
{
get { return new string[] { "Group" }; }
}

ie got rid of the last valuein the hierarchy, but still no luck, does anyone know if it is possible to do this from 2 dropdown boxes rather than 3? if so can someone point me in the right direction? thanks in advance John


Hi,

Please refer to thisthread for how to use a XML document with different format.

Or you can change attribute "name" to "value" .


I have had the same issue all day.. Finally noticing the only difference between mine and the sample was case, I tried making the xml attribute strings names lower case ie.. <group> <subgroup> and made sure the heirarchy property was also lower case and they now populate.

Hope this helps

CascadingDropDown Hierarchy

Hi,
I've been playing around with the example from the toolkitand am wondering if its possible to replace the .xml file it uses to set theHierarchy with pointers to a dataset? I have no idea how to go about this! Anyhelp would bee really appreciated

Cheers,
Denis

Hi Denis -

Take a look athttp://blogs.msdn.com/sburke/archive/2006/04/14/576421.aspx


Thats exactly what I was after! Cheers
Glad to help!