Solved

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

Posted on 2004-10-13
3
231 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

914 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

17 Experts available now in Live!

Get 1:1 Help Now