Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DataGrid Buttons 1

Status
Not open for further replies.

Kalisto

Programmer
Feb 18, 2003
997
GB
I have a Datagrid on one of my forms, with edit and delete columns. If I have the columns as Link Buttons, then it all works fine, but if Imake them push buttons, my edit and delete functions are no longer called.

Whats the difference, and what do I need to change to make the buttons work ?

Thanks,
K
 
How are you changing from Link Buttons to Push Buttons?

Can you submit your HTML?

Hope everyone is having a great day!

Thanks - Jennifer
 
I am changing them in the properties option, so the HTML is all default code.

(Right click on dg, select Button Column and change from the default link style to puch button style)
 
You have to modify your Page_Load method. More than likely, you are doing something like this:

private void Page_Load(object sender, System.EventArgs e)
{
// load your grid
daMySource.Fill(dsMySource1);
grdMyGrid.DataBind()
}

The push buttons in a DataGrid cause a post back event to fire so you need to wrap the initial fill so it doesn't run if the page is posted back, like this:

private void Page_Load(object sender, System.EventArgs e)
{
if (Page.IsPostBack)
{ // now load your grid initially
daMySource.Fill(dsMySource1);
grdMyGrid.DataBind()
}
}

that should get everything working for you.
 
Have you defined the Public Delegate?

Code:
Public Delegate Sub ClickCellButtonEventHandler(ByVal sender As Object, ByVal e As CellEventArgs)

Public Sub ClickCellButton(ByVal sender As System.Object, ByVal e As Infragistics.WebUI.UltraWebGrid.CellEventArgs) Handles dgCriteria.ClickCellButton  '.ClickCellButton

  Dim Col As String
  Col = e.Cell.Column.ToString
  
  Select Case Col
      Case "Edit"
         'Edit Code
      Case "Delete"
         'Delete Code
  End Select

End Sub

Here is an example of how I did this - I used a third party grid, but it is probably something similar for a microsoft grid.
 
Heh, thanks guys, I had more or less given up on this one, I'll try ir and see (makes it all look tidier if I can use buttons not hyperlinks)

If it works I'll let you know.

K
 
I had the same problem with mine a little while back, and once I wrapped the the initial fill in the Page_Load method to prevent it running during a post back, everything worked great.
 
JBalcerzak had the answer, but has caused me another problem! My form had some validators, and whereas the link buttons didnt trigger them, the new buttons do. (There is no Causes Validation property in the datagrid.) any more ideas?

K
 
You could add a template columns to your grid instead of using the property builder columns. Then in the template column, you can add server side controls like labels and textboxes to them and validate the controls using server validators - worth a try anyway...
 
Yup, worls fine now. Had a slight issue with some validators firing on the page that I didn;t want to, but got that sorted too. Im glad to know that someone out there trawls the old posts to see if any still need answering !

K
 
Glad to hear it - if I wasn't having a similar problem a few days ago, I wouldn't have stumbled across your post. Once I found a solution, I figured I'd pass it on...
 
These are the columns I used on 7 different web forms and they all work perfectly just copy and paste them in the right place. If I wanted to use a button all I needed to do was change the ButtonType and the Text attributes.

But I opted to use images -- it worked really cool!

I hope this helps you out

Code:
'There's more code above
.....
<asp:EditCommandColumn 
   ButtonType="LinkButton" 
   UpdateText="&lt;img src='images/save.gif' 
   border=0&gt;" 
   HeaderText="Edit" 
   CancelText="&lt;img src='images/cancel.gif' border=0&gt;" 
   EditText="&lt;img src='images/edit.jpg' 
   border=0&gt;">
<HeaderStyle font-size="8pt" 
   HorizontalAlign="Center" 
   ForeColor="White">
</HeaderStyle>

<ItemStyle font-size="8pt" 
   HorizontalAlign="Center">
</ItemStyle>
</asp:EditCommandColumn>

<asp:ButtonColumn 
   Text="&lt;img src='images/delete.jpg' 
   border=0&gt;" 
   HeaderText="Del" 
   CommandName="Delete">
<HeaderStyle font-size="8pt" 
   HorizontalAlign="Center" 
   ForeColor="White">
</HeaderStyle>

<ItemStyle font-size="8pt" HorizontalAlign="Center">
</ItemStyle>
</asp:ButtonColumn>
</Columns>
</asp:datagrid>

Be sure you have these defined:

OnEditCommand="Grid1_Edit"
OnUpdateCommand="Grid1_Update"
OnCancelCommand="Grid1_Cancel"
OnDeleteCommand="Grid1_Delete"


Code:
<asp:datagrid id="Grid1" 
	runat="server" 	
	Font-Names="Arial" 	
	bordercolor="#336699" 	
	AutoGenerateColumns="False" 	
	EditItemStyle-BackColor="#00BFFF" 	
	DataKeyField="ID" 	
	font-name="Arial"
	Font-Size="8px" 	 	
	HorizontalAlign="Center" 	
	width="90%" 	
        OnEditCommand="Grid1_Edit"
        OnUpdateCommand="Grid1_Update"
        OnCancelCommand="Grid1_Cancel"
        OnDeleteCommand="Grid1_Delete">

I always do it all in code since those tools in VS aren't very reliable -- they make lots of mistakes for you. I make enough of my own!

David
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top