Data grid EditCommandColumn

G

Guest

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 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>
 
S

Scott Mitchell [MVP]

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.


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 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
(e-mail address removed)
http://www.4GuysFromRolla.com

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

Bonj

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,743
Latest member
WoodrowMea

Latest Threads

Top