Monday, March 12, 2012

how to handle datagrid item command in C#?

Can anyone share a code snippet to handle the link button with Update command in C#? The link button is a template column inside a datagrid.

ASPX code:
<asp:TemplateColumn HeaderText="Update">
<ItemTemplate>
<asp:LinkButton Runat="server" Text="Update" CommandName="Update" ID="Linkbutton1" NAME="Linkbutton1"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn
I tried the following C# code:

private void grdOrderItems_ItemCommand(Object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
if (e.CommandName == "Update")
{
...
}
}

But the handler did not get called when the link button was clicked.

Thanks.Hello, i recommend you check this pageAdding Button Columns to a DataGrid Control.

Good Luck.

Labels: , , , , , , , , , , , , , , ,

How to handle cancel command on a formview inside of a datalist?

I have no problem using the findcontrol method to work with the formview, but I want to hide the formview when the cancel button is pressed. I am new and at a loss... been looking everywhereI can't tell if the Cancel button is on your FormView or on your DataList, but either way, you'd do the same thing. When you handle the Cancel button (you have to do it manually in DataList, in FormView you can handle the OnModeChanging event and see if CancelingEdit in the event args is true), use FindControl to find the FormView and set FormView.Visible = false.
Hope this helps...

Labels: , , , , , , , , , , , ,

How to handle buton event inside datagrid

Hi All

In Datagrid with the help of template i am able to place a button like this

<asp:TemplateColumn HeaderText="Patient_id">
<ItemTemplate>
<asp:Label id=Label1 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Patient_id") %>'>
</asp:Label>
<asp:Button id="Button1" runat="server" Text="Button" OnClick="redir" ></asp:Button>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id=TextBox1 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Patient_id") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>

i added redir in aspx page like this

protected sub redir( sender as object, e as eventargs)

//code

end sub

when i m invoking that with Onclick its generating the error

Invalid postback or callback argument. Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@dotnet.itags.org. Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.

Help Needed

Thank u

Baba

You cant use event handlers directly like that when handling events of a nested control. The nested control bubbles up its event and you can handle them as shown in this article:

http://blogs.msdn.com/paraga/archive/2005/11/26/497120.aspx

Hope this helps,

Vivek

Labels: , , , , , , , , , , ,