?
Solved

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

Posted on 2004-10-13
3
Medium Priority
?
266 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

650 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