ASP.Net Editing Textbox

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

For various reasons, I have had to produce a quick (!) page to edit one column of data in a database with ASP.net. With this being my first foray into ASP.net, I apoligise for any basic erros I have made.

The main problem is when setting up an ASP.net datagrid, which I can edit the single value of a specific column. I have wired up the update and edit methods to change the current edit row. I worked through the example in the MSDN help to get the editing textbox and extract the value out of it, but in my code, I get a blank value. I can successfully recieve the textbox, but with no text.

Thanks in advance.
Martin

Below is a snippet of the code (I know there is a security risk, and will validate user input later!!!)

TextBox txtInvoiceNo = (TextBox)e.Item.Cells[8].Controls[0];
string InvoiceNo = txtInvoiceNo.Text;
string PONumber = e.Item.Cells[4].Text;
string CBR = e.Item.Cells[5].Text;
DateTime Date = Convert.ToDateTime(e.Item.Cells[7].Text);
string cmdText = "UPDATE SALE SET INVOICENO = '" + txtInvoiceNo.Text + "' WHERE " + "CUSTOMERBOOKINGREF = '" + CBR + "' AND PONUMBER = '" + PONumber + "' AND " + "SOLDDATE = '" + Date.ToString("dd-MMM-yyyy") + "'";
 
Hi Martin,

From your description, you use the WebForm DataGrid to display and allow
the user to edit the some datas retrieved from the database. However, you
find when the user enter edited data in the datagrid's textbox and update
it, you can't get the new input value at serverside, yes?

From the code you provided, I can't find anything incorrect. Also, I've
made a simple page which use a webform datagrid and when the user hit
"Update" button get the new value in the DataGrid's UpdateCommand event and
it worked well. I've pasted the page's code here, you can also have a test
on your side to see whether it work:

============aspx page==============
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>BindGrid</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">
<table width="100%" align="center">
<tr>
<td>
<asp:DataGrid id="dgMain" runat="server" AutoGenerateColumns="False"
AllowSorting="True">
<Columns>
<asp:BoundColumn DataField="index" SortExpression="sort_index"
HeaderText="Index"></asp:BoundColumn>
<asp:BoundColumn DataField="name" SortExpression="sort_name"
HeaderText="Name"></asp:BoundColumn>
<asp:BoundColumn DataField="description"
SortExpression="sort_description"
HeaderText="Description"></asp:BoundColumn>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update"
HeaderText="Operation" CancelText="Cancel"
EditText="Edit"></asp:EditCommandColumn>
</Columns>
</asp:DataGrid></td>
</tr>
<tr>
<td>
<asp:Button id="btnChangeSortText" runat="server" Text="Change Sort
Text"></asp:Button></td>
</tr>
<tr>
<td></td>
</tr>
</table>
</form>
</body>
</HTML>

==========code behind classs=========
public class BindGrid : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button btnChangeSortText;
protected System.Web.UI.WebControls.DataGrid dgMain;

private void Page_Load(object sender, System.EventArgs e)
{

if(!IsPostBack)
{
Load_Data();
Bind_Data();
Response.Write("<br>" + Test.name);
}
}

protected void Load_Data()
{
DataTable tb = new DataTable();
tb.Columns.Add("index");
tb.Columns.Add("name");
tb.Columns.Add("description");
tb.Columns.Add("type1",typeof(string));
tb.Columns.Add("type2",Type.GetType("System.String"));

for(int i=1;i<=10;i++)
{
DataRow row = tb.NewRow();
row["index"] = i.ToString();
row["name"] = "Name" + i.ToString();
row["description"] = "Description" + i.ToString();
tb.Rows.Add(row);
}

Session["TEMP_DATA"] = tb;
}

protected void Bind_Data()
{
DataTable tb = (DataTable)Session["TEMP_DATA"];
dgMain.DataSource = tb;
dgMain.DataBind();
((BoundColumn)dgMain.Columns[0]).ReadOnly = true;
}


#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.dgMain.CancelCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgMain_CancelComm
and);
this.dgMain.EditCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgMain_EditComman
d);
this.dgMain.SortCommand += new
System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.dgMain_SortCo
mmand);
this.dgMain.UpdateCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgMain_UpdateComm
and);
this.dgMain.ItemDataBound += new
System.Web.UI.WebControls.DataGridItemEventHandler(this.dgMain_ItemDataBound
);
this.btnChangeSortText.Click += new
System.EventHandler(this.btnChangeSortText_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void dgMain_UpdateCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
TextBox txtName = (TextBox)e.Item.Cells[1].Controls[0];
TextBox txtDesc = (TextBox)e.Item.Cells[2].Controls[0];

Response.Write("<br>UpdateName: " + txtName.Text);
Response.Write("<br>UpdateDescription: " + txtDesc.Text);

dgMain.EditItemIndex = -1;
Bind_Data();
}

private void dgMain_CancelCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{

dgMain.EditItemIndex = -1;
Bind_Data();
}

private void dgMain_EditCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
dgMain.EditItemIndex = e.Item.ItemIndex;
Bind_Data();
}

private void dgMain_SortCommand(object source,
System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
{
Response.Write("<br>" + e.SortExpression);
Bind_Data();

}

private void btnChangeSortText_Click(object sender, System.EventArgs e)
{
dgMain.Columns[1].HeaderText = "fdsaf";
Bind_Data();
}

private void dgMain_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.Header)
{

Response.Write("<br>" +
e.Item.Cells[1].Controls[0].GetType().ToString());
}


}
}
========================================

Hope helps. Thanks.


Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 
I have merged your sample with my code, and realised what the problem was - in the page load event, I was not checking for !IsPostBack (thought I had done that)

Cheers

Martin

Steven Cheng said:
Hi Martin,

From your description, you use the WebForm DataGrid to display and allow
the user to edit the some datas retrieved from the database. However, you
find when the user enter edited data in the datagrid's textbox and update
it, you can't get the new input value at serverside, yes?

From the code you provided, I can't find anything incorrect. Also, I've
made a simple page which use a webform datagrid and when the user hit
"Update" button get the new value in the DataGrid's UpdateCommand event and
it worked well. I've pasted the page's code here, you can also have a test
on your side to see whether it work:

============aspx page==============
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>BindGrid</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">
<table width="100%" align="center">
<tr>
<td>
<asp:DataGrid id="dgMain" runat="server" AutoGenerateColumns="False"
AllowSorting="True">
<Columns>
<asp:BoundColumn DataField="index" SortExpression="sort_index"
HeaderText="Index"></asp:BoundColumn>
<asp:BoundColumn DataField="name" SortExpression="sort_name"
HeaderText="Name"></asp:BoundColumn>
<asp:BoundColumn DataField="description"
SortExpression="sort_description"
HeaderText="Description"></asp:BoundColumn>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update"
HeaderText="Operation" CancelText="Cancel"
EditText="Edit"></asp:EditCommandColumn>
</Columns>
</asp:DataGrid></td>
</tr>
<tr>
<td>
<asp:Button id="btnChangeSortText" runat="server" Text="Change Sort
Text"></asp:Button></td>
</tr>
<tr>
<td></td>
</tr>
</table>
</form>
</body>
</HTML>

==========code behind classs=========
public class BindGrid : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button btnChangeSortText;
protected System.Web.UI.WebControls.DataGrid dgMain;

private void Page_Load(object sender, System.EventArgs e)
{

if(!IsPostBack)
{
Load_Data();
Bind_Data();
Response.Write("<br>" + Test.name);
}
}

protected void Load_Data()
{
DataTable tb = new DataTable();
tb.Columns.Add("index");
tb.Columns.Add("name");
tb.Columns.Add("description");
tb.Columns.Add("type1",typeof(string));
tb.Columns.Add("type2",Type.GetType("System.String"));

for(int i=1;i<=10;i++)
{
DataRow row = tb.NewRow();
row["index"] = i.ToString();
row["name"] = "Name" + i.ToString();
row["description"] = "Description" + i.ToString();
tb.Rows.Add(row);
}

Session["TEMP_DATA"] = tb;
}

protected void Bind_Data()
{
DataTable tb = (DataTable)Session["TEMP_DATA"];
dgMain.DataSource = tb;
dgMain.DataBind();
((BoundColumn)dgMain.Columns[0]).ReadOnly = true;
}


#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.dgMain.CancelCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgMain_CancelComm
and);
this.dgMain.EditCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgMain_EditComman
d);
this.dgMain.SortCommand += new
System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.dgMain_SortCo
mmand);
this.dgMain.UpdateCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgMain_UpdateComm
and);
this.dgMain.ItemDataBound += new
System.Web.UI.WebControls.DataGridItemEventHandler(this.dgMain_ItemDataBound
);
this.btnChangeSortText.Click += new
System.EventHandler(this.btnChangeSortText_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void dgMain_UpdateCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
TextBox txtName = (TextBox)e.Item.Cells[1].Controls[0];
TextBox txtDesc = (TextBox)e.Item.Cells[2].Controls[0];

Response.Write("<br>UpdateName: " + txtName.Text);
Response.Write("<br>UpdateDescription: " + txtDesc.Text);

dgMain.EditItemIndex = -1;
Bind_Data();
}

private void dgMain_CancelCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{

dgMain.EditItemIndex = -1;
Bind_Data();
}

private void dgMain_EditCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
dgMain.EditItemIndex = e.Item.ItemIndex;
Bind_Data();
}

private void dgMain_SortCommand(object source,
System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
{
Response.Write("<br>" + e.SortExpression);
Bind_Data();

}

private void btnChangeSortText_Click(object sender, System.EventArgs e)
{
dgMain.Columns[1].HeaderText = "fdsaf";
Bind_Data();
}

private void dgMain_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.Header)
{

Response.Write("<br>" +
e.Item.Cells[1].Controls[0].GetType().ToString());
}


}
}
========================================

Hope helps. Thanks.


Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 
Back
Top