?
Solved

Problem: after the parent DataList.DataBind(), changing controls' value within the dataList failed.

Posted on 2004-10-13
3
Medium Priority
?
271 Views
Last Modified: 2010-04-15
Hi Dear Sir/Madam,
I am a web developer writing web applications in C#, now I have a problem with my page and am stuck, :(

I have a aspx page which has a datalist like following. It has multiple server controls, like textbox, dropdownlist.
------------------------------------------------------------------------
<asp:DataList id="customerInfoDetailDataList" runat="server" DataKeyField="CustomerID" OnItemCommand="DLCommandHandler"
OnItemDataBound="customerInfoDetailDataList_ItemDataBound">
<ItemTemplate>
<asp:TextBox id="companyTextBox" runat="server" CssClass="formelements" Text='<%# DataBinder.Eval(Container.DataItem, "CompanyName") %>' size="25"/></td>
....
------------------------------------------------------------------------
in the pageload method of the backend C# file, I write:
------------------------------------------------------------------------
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{                        
generalInfoDataSet = GetCustomerGeneralInfo(pickedCustomerID);                        
customerInfoDetailDataList.DataSource = generalInfoDataSet.Tables[0];
customerInfoDetailDataList.DataBind();

TextBox cityTextBox = (TextBox)customerInfoDetailDataList.Items[0].FindControl("cityTextBox");
cityTextBox.Text = "test";
cityTextBox.DataBind();
customerInfoDetailDataList.DataBind();

DropDownList stateDropDownList = (DropDownList)customerInfoDetailDataList.Items[0].FindControl("stateDropDown");
stateDropDownList.DataSource = dropDownTableDataSourceTable;
stateDropDownList.DataBind();
------------------------------------------------------------------------

Only customerInfoDetailDataList dataBind works, neither changing the contained textbox value or dropdownlist databind works, although through debug lines, those codes are executed.

Does anyone has any idea why it's happening? I am having similar problems with other datalist, but datagrid works fine.

Any help is greatly appreciated!
GotVMail.
0
Comment
Question by:gvmdevelopment
1 Comment
 
LVL 3

Accepted Solution

by:
Realmrat earned 1000 total points
ID: 12305662
Let me ask a question.  Are you trying to populate the DropDownList from values from a Database?  Here's an implimentation:

                       Codebehind
|-----------------------------------------------------------------------------------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.Odbc;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace CCTest
{
      /// <summary>
      /// Summary description for Test2.
      /// </summary>
      public class Test2 : System.Web.UI.Page
      {
        protected System.Web.UI.WebControls.DataList DataList1;
   
            private void Page_Load(object sender, System.EventArgs e)
            {
            if( !IsPostBack )
            {
                DataSet customerData = LoadCustomersFromDatabase();
                DataList1.DataSource = customerData;
                DataList1.DataBind();
            }
            }

        protected void OnDataList_Load(object sender, System.EventArgs e)
        {
            // Get the Data
            DataSet stateData = LoadStatesFromDatabase();

            // Iterate through all rows within the DataList
            for (int i=0; i < DataList1.Items.Count; i++)
            {
                // Find the DropDownList Control
                DropDownList stateDropDownList = (DropDownList)DataList1.Items[i].FindControl("StateDropDownList");

                // This will add the column value to the DropDownList
                foreach( DataRow row in stateData.Tables[0].Rows )
                {
                    stateDropDownList.Items.Add( row["state"].ToString() );
                }
                // Bind the Data to the Control
                stateDropDownList.DataBind();
            }
        }

        private DataSet LoadCustomersFromDatabase()
        {
            //Connection String
            string connectionString = @"...";

            //Command String
            string commandString = @"SELECT * FROM test_customers";

            OdbcDataAdapter dataAdapter = new OdbcDataAdapter(commandString,connectionString);

            DataSet myDataSet = new DataSet();
            dataAdapter.Fill (myDataSet);
            return myDataSet;
        }

        private DataSet LoadStatesFromDatabase()
        {
            //Connection String
            string connectionString = @"...";

            //Command String
            string commandString = @"SELECT state FROM test_states ORDER BY state";

            OdbcDataAdapter dataAdapter = new OdbcDataAdapter(commandString,connectionString);

            DataSet myDataSet = new DataSet();
            dataAdapter.Fill (myDataSet);
            return myDataSet;
        }

            #region Web Form Designer generated code
            override protected void OnInit(EventArgs e)
            {
                  //
                  // CODEGEN: This call is required by the ASP.NET Web Form Designer.
                  //
                  InitializeComponent();
                  base.OnInit(e);
            }
            
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {    
            this.DataList1.Load += new System.EventHandler(this.OnDataList_Load);
            this.Load += new System.EventHandler(this.Page_Load);

        }
            #endregion
      }
}



                       Page.aspx
|-----------------------------------------------------------------------------------------
<%@ Page language="c#" Codebehind="Test2.aspx.cs" AutoEventWireup="false" Inherits="CCTest.Test2" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
    <HEAD>
        <title>Test2</title>
        <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
        <meta name="CODE_LANGUAGE" Content="C#">
        <meta name="vs_defaultClientScript" content="JavaScript">
        <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
    </HEAD>
    <body>
        <form id="Form1" method="post" runat="server">
            <asp:DataList id="DataList1" runat="server" DataKeyField="CustomerID">
                <ItemTemplate>
                    <table>
                        <tr>
                            <td align="right">
                                CustomerID:
                            </td>
                            <td><%# DataBinder.Eval(Container.DataItem, "CustomerID") %></td>
                        </tr>
                        <tr>
                            <td align="right">
                                Name:
                            </td>
                            <td><%# DataBinder.Eval(Container.DataItem, "CustomerName") %></td>
                        </tr>
                        <tr>
                            <td align="right">City:</td>
                            <td><%# DataBinder.Eval(Container.DataItem, "City") %></td>
                        </tr>
                        <tr>
                            <td align="right">State:</td>
                            <td>
                                <asp:DropDownList ID="StateDropDownList" Runat="server"></asp:DropDownList>
                            </td>
                        </tr>
                    </table>
                </ItemTemplate>
                <SeparatorTemplate>
                    <hr>
                </SeparatorTemplate>
            </asp:DataList>
        </form>
    </body>
</HTML>


I think that's what you were looking for ... =]

 - Joe
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question