Solved

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

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

733 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