DataGrid onItemCommand problem

G

Guest

Hello,

<asp:DataGrid ID="dg1" AllowPaging="True" AllowSorting="True"
AlternatingItemStyle-BackColor="#ccccff"
AlternatingItemStyle-Font-Name="arial"
AlternatingItemStyle-ForeColor="#333366" CellPadding="3"
DataKeyField="empid" OnPageIndexChanged="chgpage"
OnSortCommand="sortdg" PageSize="10" PagerStyle-Mode="NumericPages"
Runat="server" PagerStyle-HorizontalAlign="Center"
OnSelectedIndexChanged="chgsel" SelectedItemStyle-BackColor="#FF0000"
AutoGenerateColumns="False" PagerStyle-BackColor="#4682b4"
PagerStyle-Font-Bold="True" PagerStyle-ForeColor="#FFFF00"
PagerStyle-Font-Name="arial,verdana" BorderColor="#4682b4"
BorderStyle="Solid" BorderWidth="2"
OnEditCommand="chkedit" OnCancelCommand="chkcancel"
OnUpdateCommand="chkupd" OnItemCommand="chkitem">
<HeaderStyle Font-Name="arial,verdana" ForeColor="#FFFF00"
BackColor="#4682b4"></HeaderStyle>
<Columns>
<asp:BoundColumn DataField="empid" HeaderText="Emp ID"
SortExpression="empid" ReadOnly="True"></asp:BoundColumn>
<asp:BoundColumn DataField="empname" HeaderText="Name"
SortExpression="empname"></asp:BoundColumn>
<asp:BoundColumn DataField="passportno" HeaderText="Passport
No"></asp:BoundColumn>
<asp:BoundColumn DataField="curbasic" HeaderText="Basic"
SortExpression="curbasic"></asp:BoundColumn>
<asp:EditCommandColumn EditText="Edit" CancelText="Cancel"
UpdateText="Update" HeaderText="Edit Item">
<ItemStyle HorizontalAlign="Center" Wrap="False"></ItemStyle>
</asp:EditCommandColumn>
<asp:ButtonColumn HeaderText="Delete Item" Text="Delete"
ButtonType="PushButton" CommandName="delete"></asp:ButtonColumn>
</Columns>
</asp:DataGrid>


in the onitemcommand procedure,
select case ctype(e.commandsource,button).commandname
case "delete"
delete()
end select

when i press delete button, its working fine. For any other operation(sort,
page changed, edit or cancel or update) displaying an error "cast invalid at
ctype(e.commandsource,button).commandname "
Coz edit and cancel are LinkButtons and Delete is Pushbutton.

How can i validate whether e.commandsource is button or linkbutton?


And when i click Edit, datagrid columns are incorporated with textboxes
those who have been allowed for editing. Here, the textbox is displaying its
default size. How can i specify diff widths for each textbox in each column?

thanks in advance
 
M

Michael Nemtsev

Hello Rajani,

Check type of the button casting to the required class. See sample below (C#)

LinkButton lnkUpdate = (LinkButton)((TableCell)e.Item.Controls[0]).Controls[0];
if (lnkUpdate != null && lnkUpdate.CommandName == "Update")
{

}

R> Hello,
R>
R> in the onitemcommand procedure,
R> select case ctype(e.commandsource,button).commandname
R> case "delete"
R> delete()
R> end select
R> when i press delete button, its working fine. For any other
R> operation(sort,
R> page changed, edit or cancel or update) displaying an error "cast
R> invalid at
R> ctype(e.commandsource,button).commandname "
R> Coz edit and cancel are LinkButtons and Delete is Pushbutton.
R> How can i validate whether e.commandsource is button or linkbutton?
R>
R> And when i click Edit, datagrid columns are incorporated with
R> textboxes those who have been allowed for editing. Here, the textbox
R> is displaying its default size. How can i specify diff widths for
R> each textbox in each column?
R>
R> thanks in advance
R>
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsch
 
M

Michael Nemtsev

Hello Rajani,

Check type of the button casting to the required class. See sample below
(C#, this code gets row button)

LinkButton lnkUpdate = (LinkButton)((TableCell)e.Item.Controls[0]).Controls[0];
if (lnkUpdate != null && lnkUpdate.CommandName == "Update")
{

}

R> Hello,
R>
R> in the onitemcommand procedure,
R> select case ctype(e.commandsource,button).commandname
R> case "delete"
R> delete()
R> end select
R> when i press delete button, its working fine. For any other
R> operation(sort,
R> page changed, edit or cancel or update) displaying an error "cast
R> invalid at
R> ctype(e.commandsource,button).commandname "
R> Coz edit and cancel are LinkButtons and Delete is Pushbutton.
R> How can i validate whether e.commandsource is button or linkbutton?
R>
R> And when i click Edit, datagrid columns are incorporated with
R> textboxes those who have been allowed for editing. Here, the textbox
R> is displaying its default size. How can i specify diff widths for
R> each textbox in each column?
R>
R> thanks in advance
R>
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsch
 

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

Top