Datagrid OnDeleteCommand

C

CalSun

I have a datagrid with a templatecolumn where I have the Delete linkbuttons.
As I click on the Del linkbutton, it calls the delete_func. However, I can't
get the value of the cell to use it as a key in sql_delete statement.

Here is my delete buttons
<asp:TemplateColumn HeaderText="Del/Add" ItemStyle-HorizontalAlign=Center
FooterStyle-HorizontalAlign=Center>
<ItemTemplate>
<asp:LinkButton id="btndelete" Runat=server Text="Del"
CommandName="cmdDelete" ForeColor=red>Del</asp:LinkButton>
</ItemTemplate>
<FooterTemplate>
<asp:LinkButton id="btnAdd" runat="server" CommandName="cmdAddRow"
ForeColor=navy>Add</asp:LinkButton>
</FooterTemplate>
</asp:TemplateColumn>

In the delete function:
public void grid_DelAdd(Object sender, DataGridCommandEventArgs e)
{
if (e.CommandName == "cmdDelete")
{
e.Item.ItemIndex ---> this reflect the correct index on the
current datagrid
string = (string) e.Item.Cells[2].Text; ---> return an
empty string, even the field (cell) is with some data
...
}

Thanks in advance.

--Cal
 
C

CalSun

Thanks Wang. I figured out my problem. I used controlfinder and casted the
control accordingly.

--cal


Elton Wang said:
Hi Cal,

Could you show your whole datagrid html code?

Elton Wang

CalSun said:
I have a datagrid with a templatecolumn where I have the Delete linkbuttons.
As I click on the Del linkbutton, it calls the delete_func. However, I can't
get the value of the cell to use it as a key in sql_delete statement.

Here is my delete buttons
<asp:TemplateColumn HeaderText="Del/Add" ItemStyle-HorizontalAlign=Center
FooterStyle-HorizontalAlign=Center>
<ItemTemplate>
<asp:LinkButton id="btndelete" Runat=server Text="Del"
CommandName="cmdDelete" ForeColor=red>Del</asp:LinkButton>
</ItemTemplate>
<FooterTemplate>
<asp:LinkButton id="btnAdd" runat="server" CommandName="cmdAddRow"
ForeColor=navy>Add</asp:LinkButton>
</FooterTemplate>
</asp:TemplateColumn>

In the delete function:
public void grid_DelAdd(Object sender, DataGridCommandEventArgs e)
{
if (e.CommandName == "cmdDelete")
{
e.Item.ItemIndex ---> this reflect the correct index on the
current datagrid
string = (string) e.Item.Cells[2].Text; ---> return an
empty string, even the field (cell) is with some data
...
}

Thanks in advance.

--Cal
 
E

Elton Wang

That's right. Unless you use BoundColumn, you should use e.Item.FindControl
to locate requied control.

CalSun said:
Thanks Wang. I figured out my problem. I used controlfinder and casted the
control accordingly.

--cal


Elton Wang said:
Hi Cal,

Could you show your whole datagrid html code?

Elton Wang

CalSun said:
I have a datagrid with a templatecolumn where I have the Delete linkbuttons.
As I click on the Del linkbutton, it calls the delete_func. However, I can't
get the value of the cell to use it as a key in sql_delete statement.

Here is my delete buttons
<asp:TemplateColumn HeaderText="Del/Add" ItemStyle-HorizontalAlign=Center
FooterStyle-HorizontalAlign=Center>
<ItemTemplate>
<asp:LinkButton id="btndelete" Runat=server Text="Del"
CommandName="cmdDelete" ForeColor=red>Del</asp:LinkButton>
</ItemTemplate>
<FooterTemplate>
<asp:LinkButton id="btnAdd" runat="server" CommandName="cmdAddRow"
ForeColor=navy>Add</asp:LinkButton>
</FooterTemplate>
</asp:TemplateColumn>

In the delete function:
public void grid_DelAdd(Object sender, DataGridCommandEventArgs e)
{
if (e.CommandName == "cmdDelete")
{
e.Item.ItemIndex ---> this reflect the correct index on the
current datagrid
string = (string) e.Item.Cells[2].Text; ---> return an
empty string, even the field (cell) is with some data
...
}

Thanks in advance.

--Cal
 
C

CalSun

the down side is taking time to name all the controls.
--Cal
Elton Wang said:
That's right. Unless you use BoundColumn, you should use
e.Item.FindControl
to locate requied control.

CalSun said:
Thanks Wang. I figured out my problem. I used controlfinder and casted
the
control accordingly.

--cal


Elton Wang said:
Hi Cal,

Could you show your whole datagrid html code?

Elton Wang

I have a datagrid with a templatecolumn where I have the Delete
linkbuttons.
As I click on the Del linkbutton, it calls the delete_func. However, I
can't
get the value of the cell to use it as a key in sql_delete statement.

Here is my delete buttons
<asp:TemplateColumn HeaderText="Del/Add" ItemStyle-HorizontalAlign=Center
FooterStyle-HorizontalAlign=Center>
<ItemTemplate>
<asp:LinkButton id="btndelete" Runat=server Text="Del"
CommandName="cmdDelete" ForeColor=red>Del</asp:LinkButton>
</ItemTemplate>
<FooterTemplate>
<asp:LinkButton id="btnAdd" runat="server" CommandName="cmdAddRow"
ForeColor=navy>Add</asp:LinkButton>
</FooterTemplate>
</asp:TemplateColumn>

In the delete function:
public void grid_DelAdd(Object sender, DataGridCommandEventArgs e)
{
if (e.CommandName == "cmdDelete")
{
e.Item.ItemIndex ---> this reflect the correct index on the
current datagrid
string = (string) e.Item.Cells[2].Text; ---> return an
empty string, even the field (cell) is with some data
...
}

Thanks in advance.

--Cal
 

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,731
Messages
2,569,432
Members
44,835
Latest member
KetoRushACVBuy

Latest Threads

Top