473,473 Members | 1,960 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Data grid EditCommandColumn

Hi
I've been following the example on

http://aspnet.4guysfromrolla.com/art...71002-1.3.aspx

and no matter what I do, i can't get the DataGrid1_EditCommand event handler
to fire. Could someone please tell me what I'm doing wrong.
Code below.

public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Data.SqlClient.SqlConnection sqlConnection1;

private void Page_Load(object sender, System.EventArgs e)
{
using(SqlCommand cmd = new SqlCommand("select * from SystemInfo",
sqlConnection1))
using(SqlDataAdapter da = new SqlDataAdapter(cmd))
{
sqlConnection1.Open();
DataTable dt = new DataTable();
da.Fill(dt);
DataGrid1.DataSource=dt;
DataGrid1.DataBind();
}
}

public void DataGrid1_EditCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = e.Item.ItemIndex;
DataGrid1.DataBind();
}
....
<form id="Form1" method="post" runat="server">
<asp:DataGrid Runat="server" ID="DataGrid1" AutoGenerateColumns="False"
OnEditCommand="DataGrid1_EditCommand">
<Columns>
<asp:EditCommandColumn ButtonType="PushButton" CancelText="Cancel"
EditText="Edit" UpdateText="Update" />
<asp:BoundColumn ReadOnly="False" DataField="Measure"/>
</Columns>
</asp:DataGrid>
</form>
Nov 18 '05 #1
2 1654
Bonj, you're going to have some issues with the way your code is written
right now. Try doing this:

Create a method called BindData() - this method will be responsible for
binding the database content to the DataGrid. Basically you can take
your code you have now in Page_Load and move it to this new BindData()
method.

Then, in Page_Load, only call BindData() *on the first page load* and
*not* on subsequent postbacks. That is, do:

if (!Page.IsPostBack)
BindData();

Finally, in your EditCommand event handler, update your code to look like:

DataGrid1.EditItemIndex = e.Item.ItemIndex;
BindData();
See if updating the code to this described pattern helps at all. Thanks.

Bonj wrote:
Hi
I've been following the example on

http://aspnet.4guysfromrolla.com/art...71002-1.3.aspx

and no matter what I do, i can't get the DataGrid1_EditCommand event handler
to fire. Could someone please tell me what I'm doing wrong.
Code below.

public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Data.SqlClient.SqlConnection sqlConnection1;

private void Page_Load(object sender, System.EventArgs e)
{
using(SqlCommand cmd = new SqlCommand("select * from SystemInfo",
sqlConnection1))
using(SqlDataAdapter da = new SqlDataAdapter(cmd))
{
sqlConnection1.Open();
DataTable dt = new DataTable();
da.Fill(dt);
DataGrid1.DataSource=dt;
DataGrid1.DataBind();
}
}

public void DataGrid1_EditCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = e.Item.ItemIndex;
DataGrid1.DataBind();
}
...
<form id="Form1" method="post" runat="server">
<asp:DataGrid Runat="server" ID="DataGrid1" AutoGenerateColumns="False"
OnEditCommand="DataGrid1_EditCommand">
<Columns>
<asp:EditCommandColumn ButtonType="PushButton" CancelText="Cancel"
EditText="Edit" UpdateText="Update" />
<asp:BoundColumn ReadOnly="False" DataField="Measure"/>
</Columns>
</asp:DataGrid>
</form>

--

Scott Mitchell
mi******@4guysfromrolla.com
http://www.4GuysFromRolla.com

* When you think ASP.NET, think 4GuysFromRolla.com!
Nov 18 '05 #2
Ah, cheers Scott!
I realise that was a bit rushed, I did think however that there was
something I was missing, perhaps some configurational aspect that I had
completely overlooked that wasn't related to that- so I posted the code to
see if anyone could just spot it. What did throw me a bit was the fact that
creating the event handlers automatically using the forms designer puts them
in as private, whereas they need to be at least protected for the aspx to
access them, and I was wondering whether it didn't work if you did that, so
I typed them in manually and wired them up via the OnEditCommand="~~" in the
aspx... However, I have now gone about it like you say by having a separate
method to get the data and bind it, which is only called by the page_load if
it's not a postback, and is also called by the EditCommand handler. And it
now works.
I'm also beginning to think that, that was actually what was *causing* the
problem - as the page was perhaps doing the page_load again due to the
postback, and then completely rebinding the datagrid, which was why my
EditCommand handler wasn't being fired at all. I had thought that the
EditCommand handler should at least fire first, even if the page_load didn't
test for postback and thus reset the datagrid. But it would seem to explain
it if this was not the case.. do you think?

Anyway, thanks for your help

p.s. great website by the way
"Scott Mitchell [MVP]" <mi******@4guysfromrolla.com> wrote in message
news:fb******************@newssvr21.news.prodigy.c om...
Bonj, you're going to have some issues with the way your code is written
right now. Try doing this:

Create a method called BindData() - this method will be responsible for
binding the database content to the DataGrid. Basically you can take your
code you have now in Page_Load and move it to this new BindData() method.

Then, in Page_Load, only call BindData() *on the first page load* and
*not* on subsequent postbacks. That is, do:

if (!Page.IsPostBack)
BindData();

Finally, in your EditCommand event handler, update your code to look like:

DataGrid1.EditItemIndex = e.Item.ItemIndex;
BindData();
See if updating the code to this described pattern helps at all. Thanks.

Bonj wrote:
Hi
I've been following the example on
http://aspnet.4guysfromrolla.com/art...71002-1.3.aspx

and no matter what I do, i can't get the DataGrid1_EditCommand event
handler to fire. Could someone please tell me what I'm doing wrong.
Code below.

public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Data.SqlClient.SqlConnection sqlConnection1;

private void Page_Load(object sender, System.EventArgs e)
{
using(SqlCommand cmd = new SqlCommand("select * from SystemInfo",
sqlConnection1))
using(SqlDataAdapter da = new SqlDataAdapter(cmd))
{
sqlConnection1.Open();
DataTable dt = new DataTable();
da.Fill(dt);
DataGrid1.DataSource=dt;
DataGrid1.DataBind();
}
}

public void DataGrid1_EditCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = e.Item.ItemIndex;
DataGrid1.DataBind();
}
...
<form id="Form1" method="post" runat="server">
<asp:DataGrid Runat="server" ID="DataGrid1" AutoGenerateColumns="False"
OnEditCommand="DataGrid1_EditCommand">
<Columns>
<asp:EditCommandColumn ButtonType="PushButton" CancelText="Cancel"
EditText="Edit" UpdateText="Update" />
<asp:BoundColumn ReadOnly="False" DataField="Measure"/>
</Columns>
</asp:DataGrid>
</form>

--

Scott Mitchell
mi******@4guysfromrolla.com
http://www.4GuysFromRolla.com

* When you think ASP.NET, think 4GuysFromRolla.com!

Nov 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Bonj | last post by:
Hi I've been following the example on http://aspnet.4guysfromrolla.com/articles/071002-1.3.aspx and no matter what I do, i can't get the DataGrid1_EditCommand event handler to fire. Could...
2
by: Rick | last post by:
I've got a long page (user requirement) with a datagrid on the second scrolled page. I've got smartnavigation on and it works fine for everything else on the page, but if I click a sort header or...
1
by: DC | last post by:
I have this datagrid: <asp:DataGrid id="dgList" runat="server" Width="100%" CssClass="BodyText" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" PageSize="20"...
1
by: angus | last post by:
Dear All, I am using VB.net It is found that the <asp:EditCommandColumn/> wouldn't fired an event for OnEditCommand If pushbutton has been used: that is:
3
by: pmud | last post by:
Hi, I have a web page (asp.net, code:c#). I havean html table with text boxes. Based on the user input , records are displayed in the data grid below it. Now the datagrid has a large no. of...
13
by: John M | last post by:
Hello, In Visual studio .NET 2003, On datagrid object : How can I link the button element : edit, to do a some client events (such as message box, and do another event just after that to the...
6
by: p.mc | last post by:
Hi all, I'm having major problems with a userControl which contains a datagrid. My problem concerns data binding. The Page_Load() procedure calls the DataBind procedure to bind the datagrid...
0
by: srini.venkatesan | last post by:
Please see the class file and the asp file. I want to validate two text fields when the update is clicked. As you guys know, when the edit is clicked, it changes to update and cancel. I want to...
0
by: rcoco | last post by:
Hi, I have a datagrid that is ment to insert data. But when I run the form only the header appears. I would like some advise from you all and solve this problem I'm using visual studio 2003. My...
1
by: rktester | last post by:
Hi, I am trying to implement an editable datagrid in .net 2005 This is how my grid is defined: <asp:DataGrid runat="server" id="dgUser" Font-Name="Verdana" Font-Size="9pt"...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.