Solved

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

Posted on 2004-10-13
3
225 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
3 Comments
 
LVL 3

Accepted Solution

by:
Realmrat earned 250 total points
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

771 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now