What's wrong with DataBind() in GridView control? The data in GridView can't be updated when the datasource have changed!

What's wrong with DataBind() in GridView control? The data in GridView can't be updated when the datasource have changed!

I bind the different datasource to the Gridview when I change the node of TreeView, the GridView control always display the same data when I click different the node of TreeView control,why?
but I can get the different data in GridView when I click the Button1 or Button2.

//-------------------Program Code (You may save it as a ASPX file and run it)--------------------------------
<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<script runat="server">

    protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
        if (TreeView1.SelectedNode.Value == "a6dce8fe-749c-4e38-ab2f-3d03d9711b3d")
            XmlDataSource2.Data = @"<UserBookmarks>
                  <Bookmark Title=""Root""></Bookmark>

            GridView1.DataSourceID = XmlDataSource2.ID;

        if (TreeView1.SelectedNode.Value == "b8dcf8fe-749c-4e38-ab2f-6d03d9711b8j")
            XmlDataSource2.Data = @"<UserBookmarks>
                  <Bookmark Title=""Card""></Bookmark>
            GridView1.DataSourceID = XmlDataSource2.ID;

    protected void Button1_Click(object sender, EventArgs e)
        XmlDataSource3.Data = @"<UserBookmarks>
                  <Bookmark Title=""3""></Bookmark>
        GridView1.DataSourceID = XmlDataSource3.ID;

    protected void Button2_Click(object sender, EventArgs e)
        XmlDataSource4.Data = @"<UserBookmarks>
                  <Bookmark Title=""4""></Bookmark>
        GridView1.DataSourceID = XmlDataSource4.ID;

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Untitled Page</title>
    <form id="form1" runat="server">
        <asp:XmlDataSource ID="XmlDataSource1" runat="server">
<Folder Name="Root" Id="a6dce8fe-749c-4e38-ab2f-3d03d9711b3d">
  <Folder Name="Card" Id="b8dcf8fe-749c-4e38-ab2f-6d03d9711b8j"/>
        <asp:TreeView ID="TreeView1" runat="server" DataSourceID="XmlDataSource1" OnSelectedNodeChanged="TreeView1_SelectedNodeChanged">
                <asp:TreeNodeBinding DataMember="Folder" TextField="Name" ValueField="Id" />
        <asp:GridView ID="GridView1" runat="server">
        <asp:XmlDataSource ID="XmlDataSource2" runat="server"></asp:XmlDataSource>
        <br />
        <br />
        &nbsp;<asp:XmlDataSource ID="XmlDataSource3" runat="server"></asp:XmlDataSource>
        <asp:XmlDataSource ID="XmlDataSource4" runat="server"></asp:XmlDataSource>
        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
        <asp:Button ID="Button2" runat="server" Text="Button" OnClick="Button2_Click" />
//-------------------Program Code (You may save it as a ASPX file and run it)--------------------------------


SuperCool Multiple ZIP - A utility to unzip multiple files and work with multiple zip files

SuperCool Random Number Generator
10/18/2005 2:41:05 AM
asp.net.presentation-controls 72751 articles. 3 followers. Follow

3 Replies

Similar Articles

[PageSpeed] 11

Are you sure you're reaching the code to change datasources in the TreeView event? The problem might be with the logic in your if statements.
This posting is provided "AS IS" with no warranties, and confers no rights.
10/18/2005 10:47:34 PM

i've already answered in the other post. the problem is that xmldatasource control performs caching by default and doesn't invalidate it when you change the data property. for this to work, you have to set the enablecaching property to false.
Luis Abreu
email: labreu_at_gmail.com
EN blog:http://msmvps.com/blogs/luisabreu
10/19/2005 11:01:18 AM

Similar Artilces:

First gridview's selected row doesn't update second gridview's objectdatasource
 HelloI got 2 gridviews on a page, each with its own ObjectDataSource. The first gridview fills itself just normally, no problem there. The second gridview needs to fill itself with other data, where the primary key is the primary key of the selected row of the first gridview.schematically:gridview1 row: IDfield, x, y, zrow: IDfield, x, y, z (-> this row is selected)row: IDfield, x, y, z gridview2 row:IDfield_of_selected_row_in_gridview1, a, b, c I tried to do this by configuring the ObjectDataSource behind gridview2 with a parameter pointing to gridview1.selectedValue. (by usin...

I can't update data when using GridView control.
Hi all.I enable editing option in  GridView control but it doesn't work.When updating I can't see anything happens.What's wrong ?Thanks Did you use two-way binding?  Make sure you use <%#Bind("xxx")%> instead of  <%#Eval("xxx")%>. Now, I can update the data as expecting.But I still have one problem.When using ntext type and updating, an unhandled exception occurred: The data types ntext and nvarchar are incompatible in the equal to operator.But when I change ntext type to nvarchar(max) everything goes fine.How can I solve this problem without changing ntext to ...

why can't Gridview update if empty data is presented into the textfield of Gridview edit
 HI all,I have basically two fields which are Name,description. Whenever I was trying to edit fields(name, description) the command event is not firing because description(textbox) is empty. Why and how to fix this issuse. Because Name field is a required one and description is optional it could be empty one. Any help please thanks and regards,Vijaycanaan.  Can you please give your code. This can help us understand and solve your problem!! Here is the code   <asp:GridView ID="GridView1" runat="server" AllowPaging="True" Aut...

Label control's text doesn't appear to be set (in the page which has a user control )when trying to update the text property in the FormView's ItemUpdated event
Hi There,I have a user control which hosts a formview control, gridview control and a label control. The grid view is usedto show rows in the DB and when clicked to edit each row, a form view opens up in edit mode with that particular row info. So I have nothing in the item template.My first question is..is it mandatory to have smth in the itemtemplate of the form view.I have controls in edititemtemplate and insertitemtemplate.When a particular row is edited, I would like to show a status msg showing that the row has been updated.For this, I use the ItemUpdated event of the formview and try ...

About Today's (2005-01-07) Article 'Change the style of a column within a GridView control based on its value'
Hi I read the article, sounded interesting although I've done it several times in asp.net (vb), not in C# as this article is. I wonder what the author tries to prove with this article since you cannot run it and see what it does? Since there's some hints that it might be using the Northend database there's no sign of it in the code, thus you can't do anything. Has the author deliberatly left out the datasource so that noone will ever know what the code does? Or is it so that he never thoght of the datasource since that worked in his machine, and he thought if it does work on hi...

GridView's data sent to another GridView control on another page
I'm trying to get send a Gridview1 control's DATA on page1.aspx to Griedview2 on page1.aspx. I tried adding to a session varible like so:page1.aspxSession["gridsData"] = GridView1;page2.aspxif (Session["gridsData"] != null){    GridView2 = ( GridView )Session["gridsData"];}When page2.aspx loads, GridView2 does not display data. There is data there, because it is NOT null in the conditional statement. Is this the best solution??Thank you,Edski Use the data itself, not the Gridview, also, I would use the cache object instead of sessionDav...

Can't remove a row in dataset that is Gridview's datasource
I have the gridview display records and I add a check box in gridview, then I let user choose the row that they want to remove when they hit Remove button. The code when in VB NET form worked but I transfer it into C# and it does not work no more. Please advise. Following is the code. Prolem is at the btnDelete_Click event line 78: 78 myDS.Tables("Catalog").Rows(row.RowIndex).Delete(); It says "Error 1 Non-invocable member 'System.Data.DataSet.Tables' cannot be used like a method."  1 using System; 2 using System.Collec...

Easy question, but I can't figure it out. Gridview nested in datalist .. I need to access the Gridview caption, but can't
To be more clear ...The datalist contains ItemIds, the gridview contains applications ids. When the page loads I need to check the database to see if the itemid / application id link already exists and if so check the checkbox. Everyhting works, except I can't get the itemid to check the DB with. Any suggestions, what am I doing wrong? I'm getting the object reference not set to the instance of an object error. TIA,Kyle 1 <asp:DataList ID="ItemsDataList" runat="server" DataSourceID="ObjectDataSource1"> 2 <ItemTemplate> 3 <h2>Part...

Update Panel's Trigger Collection can't find control
I need to put a trigger of a button (btn1) that is inside of a accordion (accordion1) to update my updatepanel. A control with ID 'btn1' could not be found for the trigger in UpdatePanel 'UpdatePanel1'. I've already tried 'accordion1$btn1' can anybody help me?[]shttp://www.fabiocolombini.com Are you trying to access it via Javascript? If so you can use <%= btn1.ClientID %> to get the full client side ID of the control.--Rob WallingASP.NET MVPMy blog, Software by RobPlease click "Mark as Answer" if my post helped you. I have a update pa...

How to change the 'edit' button to 'Update'+'Cancel' button in Gridview
Hi all, I'm using a commandField 'edit' button in a Gridview which I hope to be changed to a 'update' and a 'cancel' button when the 'edit' button is pushed. My codes are as follow.  My problem is that I don't know what codes should be added to make the change I mentioned to happen. THanks a lot for your assistance. TOny<%@ Page Language="VB" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><%@ Import Namespace="syste...

Can't UNDO, REDO and change control's ID in Design View
Hi all, I just noticed that when the aspx page in Design View, I couldn't Undo, Redo and change the ID of the controls. It is weired as it was just fine in last week. Any advice would be appreacited, thanks.   This may be a stupid question, but are you debugging when you are trying to do this? VMK007:This may be a stupid question, but are you debugging when you are trying to do this?  Of course not, everything is ok when the page in Source View. Even I have restarted VS2005 many times, but the problem still exist when the page in Design View, weired!  ...

Why can't I Enable Editing on a Gridview that gets it's data from a View through an SqlDataSource?
Hi, I think the title explains it all really. I cannot configure the SqlDataSource to add Insert, Update and Delete statements - not through the wizard anyway. The view combines two tables, each containing 3 columns. I have tried using different combinations of columns, and ended up selecting all of them (making sure the foreign key column of the first table was given a different alias to the primary key column of the second table) but I still cannot enable editing. Cheers, PatrickDisco Patrick writes web applications using:Visual Studio 2008ASP.NET 3.5C#SQL Server 2008 Use stored pr...

Can't reach dynamically added control on update in GridView
Hi-I have a GridView control that has a dynamically added control in one column. What sort of control gets added is based on what the content is - could be a textbox, RTF Editor (instead of a normal textarea, similar to what I'm writing this post in), datepicker, etc. I'm getting the data into the control just fine when I go into edit mode for a row, but can't seem to get the contents of that control when I try to save changes and update that row.I've tried using the RowUpdating event for the GridView control as well as the Updating event for the SQLDatasource control that the GridView is bo...

GridView doesn't refresh it's schema after the object data source's GetData() method returns additional fields .
Hi . I am creating an ASP.NET web site and have the following problem :  I have a GridView which bound to the object data source. This object data source is SQL Table adapter that I added to the data set via IDE . The method GetData of this table adapter uses existing stored procedure on my SQL 2005 server. Everything worked fine , until I made a changes to the stored procedure ( the new result records have additional fields )  .  The problem is :   The new fields don’t appear in the grid . Note : When I right click to the GetData method inside the tabl...

Web resources about - What's wrong with DataBind() in GridView control? The data in GridView can't be updated when the datasource have changed! - asp.net.presentation-controls

Resources last updated: 1/15/2016 7:18:39 AM