Problem with DataList and updating data

  • Thread starter Thread starter Colin Young
  • Start date Start date
C

Colin Young

I'm having a bit of a problem with my DataList when I try to update from the
user's input. I've included relevant excerpts at the end of this message. In
the UpdateCommand code, the "PageLevelTextBox" appears to be found (it isn't
returning null), but the Text property is an empty string ("").

Am I missing something really obvious?

Thanks

Colin

private void DeployList_UpdateCommand(object source,
System.Web.UI.WebControls.DataListCommandEventArgs e)
{
int PageLevel, ZoneID;

ZoneID = int.Parse(e.CommandArgument.ToString());
TextBox PageLevelTB =
(TextBox)e.Item.FindControl("PageLevelTextBox");

try
{
PageLevel = int.Parse(PageLevelTB.Text);

Distribution dist = new
Distribution(appEnv.GetConnection());
dist.InsertAtEnd(cid, ver, ZoneID, PageLevel);

DeployList.EditItemIndex = -1;
Bind();
}
catch(Exception ex)
{
Controls.Add(new LiteralControl(ex.Message));
}
}

<edititemtemplate>
<div class="row">
<span class="label" style="width: 35%; text-align: left; ">
<%# DataBinder.Eval(Container.DataItem, "Title") %>
</span>
<span class="formw" style="width: 10%; ">
<asp:linkbutton
commandname="update"
commandargument='<%# DataBinder.Eval(Container.DataItem,
"ZoneID") %>'
text="update"
runat="server"/>
</span>
<span class="formw">
Page:
<asp:textbox
Text='<%# DataBinder.Eval(Container.DataItem, "PageLevel")
%>'
size="2"
id="PageLevelTextBox"
runat="server" />
</span>
</div>
</EditItemTemplate>
 
Hello Colin,

Thanks for your post. I reviewed your code carefully, however, I did not
find any known issue. In addition, I built a sample web page in Visual
Studio .NET 2002 and it works properly on my side. I post my my test code
below, please check it on your side.

I look forward to your response.

Have a nice day!

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! ¨C www.microsoft.com/security
This posting is provided ¡°as is¡± with no warranties and confers no rights.

//------------------------WebForm3.aspx.cs---------------------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
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 WebApplication2
{
/// <summary>
/// Summary description for WebForm3.
/// </summary>
public class WebForm3 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataList DeployList;
protected System.Web.UI.WebControls.Button Button2;

ICollection CreateDataSource()
{
DataTable dt = new DataTable();
DataRow dr;

dt.Columns.Add(new DataColumn("StringValue", typeof(string)));

for (int i = 0; i < 10; i++)
{
dr = dt.NewRow();
dr[0] = "Item " + i.ToString();
dt.Rows.Add(dr);
}

DataView dv = new DataView(dt);
return dv;
}

private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
DeployList.DataSource = CreateDataSource();
DeployList.DataBind();
}

}

#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.DeployList.UpdateCommand += new
System.Web.UI.WebControls.DataListCommandEventHandler(this.DeployList_Update
Command);
this.Button2.Click += new System.EventHandler(this.Button2_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void DeployList_UpdateCommand(object source,
System.Web.UI.WebControls.DataListCommandEventArgs e)
{
int PageLevel, ZoneID;

// ZoneID = int.Parse(e.CommandArgument.ToString());
TextBox PageLevelTB =
(TextBox)e.Item.FindControl("PageLevelTextBox");

if(PageLevelTB == null)
Response.Write("Fail to find control");
else
Response.Write(PageLevelTB.Text );
try
{
PageLevel = int.Parse(PageLevelTB.Text);
}
catch(Exception ex)
{
Controls.Add(new LiteralControl(ex.Message));
}
}

private void Button2_Click(object sender, System.EventArgs e)
{
DeployList.EditItemIndex = 1;
}

}
}
//--------------------------------end of-----------------------------

//------------------------------ WebForm3.aspx------------------
<%@ Page language="c#" Codebehind="WebForm3.aspx.cs"
AutoEventWireup="false" Inherits="WebApplication2.WebForm3" %>
<%@ Import Namespace="System.Data" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm2</title>
<meta content="Microsoft Visual Studio 7.0" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="WebForm2" method="post" runat="server">
<FONT face="arial">
<asp:datalist id="DeployList" style="Z-INDEX: 102; LEFT: 91px;
POSITION: absolute; TOP: 21px" runat="server"
AlternatingItemStyle-BackColor="Gainsboro" HeaderStyle-BackColor="#aaaadd"
Font-Size="8pt" Font-Name="Verdana" CellPadding="3" BorderColor="black">
<HeaderTemplate>
Items
</HeaderTemplate>
<AlternatingItemStyle BackColor="Gainsboro"></AlternatingItemStyle>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "StringValue") %>
</ItemTemplate>
<HeaderStyle BackColor="#AAAADD"></HeaderStyle>
<edititemtemplate>
<div class="row">
<span class="label" style="width: 35%; text-align: left; ">
<%# DataBinder.Eval(Container.DataItem, "Title") %>
</span><span class="formw" style="width: 10%; ">
<asp:linkbutton commandname="update" commandargument='<%#
DataBinder.Eval(Container.DataItem,"ZoneID") %>' text="update"
runat="server" ID="Linkbutton1"/>
</span><span class="formw">Page:
<asp:textbox Text='<%# DataBinder.Eval(Container.DataItem,
"PageLevel")%>' size="2" id="PageLevelTextBox" runat="server" />
</span>
</div>
</edititemtemplate>
</asp:datalist>
<asp:button id="Button2" style="Z-INDEX: 103; LEFT: 290px; POSITION:
absolute; TOP: 37px" runat="server" Text="Button"></asp:button>
</FONT>
</form>
</body>
</HTML>
//------------------------------------------end
of------------------------------
 
Your sample works. I'm stumped by my code. The trace shows the form
collection containing "DeployList:_ctl1:PageLevelTextBox" with the entered
value and the control tree also contains that control (with that exact
name), however ASP.Net is not properly populating that control with the
entered value.

Any ideas?

Thanks

Colin

Tian Min Huang said:
Hello Colin,

Thanks for your post. I reviewed your code carefully, however, I did not
find any known issue. In addition, I built a sample web page in Visual
Studio .NET 2002 and it works properly on my side. I post my my test code
below, please check it on your side.

I look forward to your response.

Have a nice day!

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! ¨C www.microsoft.com/security
This posting is provided ¡°as is¡± with no warranties and confers no rights.

//------------------------WebForm3.aspx.cs---------------------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
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 WebApplication2
{
/// <summary>
/// Summary description for WebForm3.
/// </summary>
public class WebForm3 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataList DeployList;
protected System.Web.UI.WebControls.Button Button2;

ICollection CreateDataSource()
{
DataTable dt = new DataTable();
DataRow dr;

dt.Columns.Add(new DataColumn("StringValue", typeof(string)));

for (int i = 0; i < 10; i++)
{
dr = dt.NewRow();
dr[0] = "Item " + i.ToString();
dt.Rows.Add(dr);
}

DataView dv = new DataView(dt);
return dv;
}

private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
DeployList.DataSource = CreateDataSource();
DeployList.DataBind();
}

}

#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.DeployList.UpdateCommand += new
System.Web.UI.WebControls.DataListCommandEventHandler(this.DeployList_Update
Command);
this.Button2.Click += new System.EventHandler(this.Button2_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void DeployList_UpdateCommand(object source,
System.Web.UI.WebControls.DataListCommandEventArgs e)
{
int PageLevel, ZoneID;

// ZoneID = int.Parse(e.CommandArgument.ToString());
TextBox PageLevelTB =
(TextBox)e.Item.FindControl("PageLevelTextBox");

if(PageLevelTB == null)
Response.Write("Fail to find control");
else
Response.Write(PageLevelTB.Text );
try
{
PageLevel = int.Parse(PageLevelTB.Text);
}
catch(Exception ex)
{
Controls.Add(new LiteralControl(ex.Message));
}
}

private void Button2_Click(object sender, System.EventArgs e)
{
DeployList.EditItemIndex = 1;
}

}
}
//--------------------------------end of-----------------------------

//------------------------------ WebForm3.aspx------------------
<%@ Page language="c#" Codebehind="WebForm3.aspx.cs"
AutoEventWireup="false" Inherits="WebApplication2.WebForm3" %>
<%@ Import Namespace="System.Data" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm2</title>
<meta content="Microsoft Visual Studio 7.0" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="WebForm2" method="post" runat="server">
<FONT face="arial">
<asp:datalist id="DeployList" style="Z-INDEX: 102; LEFT: 91px;
POSITION: absolute; TOP: 21px" runat="server"
AlternatingItemStyle-BackColor="Gainsboro" HeaderStyle-BackColor="#aaaadd"
Font-Size="8pt" Font-Name="Verdana" CellPadding="3" BorderColor="black">
<HeaderTemplate>
Items
</HeaderTemplate>
<AlternatingItemStyle BackColor="Gainsboro"></AlternatingItemStyle>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "StringValue") %>
</ItemTemplate>
<HeaderStyle BackColor="#AAAADD"></HeaderStyle>
<edititemtemplate>
<div class="row">
<span class="label" style="width: 35%; text-align: left; ">
<%# DataBinder.Eval(Container.DataItem, "Title") %>
</span><span class="formw" style="width: 10%; ">
<asp:linkbutton commandname="update" commandargument='<%#
DataBinder.Eval(Container.DataItem,"ZoneID") %>' text="update"
runat="server" ID="Linkbutton1"/>
</span><span class="formw">Page:
<asp:textbox Text='<%# DataBinder.Eval(Container.DataItem,
"PageLevel")%>' size="2" id="PageLevelTextBox" runat="server" />
</span>
</div>
</edititemtemplate>
</asp:datalist>
<asp:button id="Button2" style="Z-INDEX: 103; LEFT: 290px; POSITION:
absolute; TOP: 37px" runat="server" Text="Button"></asp:button>
</FONT>
</form>
</body>
</HTML>
//------------------------------------------end
of------------------------------
 
Hi Colin,

I did not find any obvious issue that may cause the problem in your code.
Since my sample works, could you post a simple web page which is able to
reproduce the problem? I will be glad to check it on my side.

I look forward to hearing from you.

Have a nice day!

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! ¨C www.microsoft.com/security
This posting is provided ¡°as is¡± with no warranties and confers no rights.
 
Back
Top