DataGrid ButtonColumn - Please help !!!

  • Thread starter Thread starter bienwell
  • Start date Start date
B

bienwell

Hi,

I have a datagrid control with the declaration as follows:

<script runat="server">
Sub DGrid_ItemDataBound(sender As Object, e As DataGridItemEventArgs)
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType =
ListItemType.AlternatingItem Then
Dim drv As DataRowView = CType(e.Item.DataItem, DataRowView)
Dim EDate As Object = CType(drv.row("End_Date"),Object)
If IsDbNull(EDate) Then
e.Item.Cells(9).Text = "No End Date"
e.Item.Cells(10).Visible=True
e.Item.Cells(10).Tooltip= "Check here to set today as the
end date."
Else
e.Item.Cells(10).Visible=False
End If
End If
End Sub

'***********************************************************************

Sub DGrid_ItemCommand(s as object, e As
System.Web.UI.WebControls.DataGridCommandEventArgs)
Dim commandName As String = e.CommandName
If commandName.Equals("Update") Then
Dim MyVar=Now()
Dim tempMonth As integer= Month(MyVar)
Dim tempDay As integer= Day(MyVar)
Dim CurrMonth as string
Dim CurrDay as string

If CInt(TempMonth) < 10 Then
CurrMonth = "0" & tempMonth
Else
CurrMonth = CStr(tempMonth)
End If

If CInt(TempDay) < 10 Then
CurrDay = "0" & tempDay
Else
CurrDay = CStr(tempDay)
End If

Dim CurrDate as string=Year(MyVar) & "-" & CurrMonth & "-" &
CurrDay
Dim myStr As String = " UPDATE TBL_EFILING_USERS SET END_DATE='"
& CurrDate & "'" & _
" WHERE ACCOUNT_ID = '" & e.Item.Cells(0).Text & "';COMMIT;"
'response.write("<BR>myStr ==> " & myStr)

Dim myConnection as ODBCConnection
Dim myCommand as ODBCCommand

Dim strConn As String =
"server=Atlantis;uid=eFileUser;pwd=eFileUser1;DSN=Atlantis"
myConnection = New OdbcConnection(strConn)
myConnection.Open()
myCommand = New OdbcCommand(myStr, myConnection)
myCommand.ExecuteNonQuery()
myConnection.Close

Dim myDataSet as DataSet=GetDataSource()
DGrid_Carrier.DataSource=myDataSet
DGrid_Carrier.DataBind()
button1.Enabled=True

End If
End Sub
</script>


<html>
<head>
<title>Test Page</title>
</head>
<body>
<form runat="Server">
<asp:DropDownList id="CarrierList" Width="123px" Runat="Server"
OnSelectedIndexChanged="Carier_SelectedIndexChanged"
AutoPostBack="True"></asp:DropDownList>
<p></p>
<p></p>
<asp:DataGrid id="DGrid_Carrier" runat="server"
OnSelectedIndexChanged="IndexChange_Command"
OnItemDataBound="DGrid_ItemDataBound" OnItemCommand="DGrid_ItemCommand"
Width="353px" Font-Size="Smaller" Font-Names="Arial" CellSpacing="1"
CellPadding="3" AutoGenerateColumns="False">
<AlternatingItemStyle backcolor="#E0E0E0"></AlternatingItemStyle>
<HeaderStyle backcolor="LightYellow"></HeaderStyle>
<Columns>
<asp:BoundColumn DataField="Account_ID"
HeaderText="Account_ID"></asp:BoundColumn>
<asp:BoundColumn DataField="Carrier_Name" HeaderText="Carrier
Name"></asp:BoundColumn>
<asp:BoundColumn DataField="Carrier_Entity" HeaderText="Carrier
Entity"></asp:BoundColumn>
<asp:BoundColumn DataField="First_Name" HeaderText="First
Name"></asp:BoundColumn>
<asp:BoundColumn DataField="Office_Address"
HeaderText="Office_Address"></asp:BoundColumn>
<asp:BoundColumn DataField="Office_City"
HeaderText="Office_City"></asp:BoundColumn>
<asp:BoundColumn DataField="Office_ZipCode"
HeaderText="Office_ZipCode"></asp:BoundColumn>
<asp:BoundColumn DataField="Work_Phone"
HeaderText="Work_Phone"></asp:BoundColumn>
<asp:BoundColumn DataField="Start_Date"
HeaderText="Start_Date"></asp:BoundColumn>
<asp:BoundColumn DataField="End_Date" DataFormatString="{0:d}"
HeaderText="End_Date"></asp:BoundColumn>
<asp:ButtonColumn CommandName="Update" HeaderText="Edit End_Date"
HeaderImageUrl="edit_a.gif" Text="Update to Current Date!" />
</Columns>
</asp:DataGrid>
<P></P>
<asp:Button id= "Button1" text= "Submit" Enabled="False" runat= "server"
/>
</form>
</body>
</html>

The Sub Function DGrid_ItemDataBound() is to check if the End_Date in the
database is empty then display the ButtonColumn in the next cell. When user
click on the link "Update to Current Date", the Sub Function
DGrid_ItemCommand() is called to update current date to the End_Date field.

The code works fine. There are something I'd like to change in Web design:

1- I'd like to display an image (.GIF) file in place of "Update to
Current Date" link button.
2- When user click on the image, it will call DGrid_ItemCommand() with the
confirmation message box ("Are you sure you want to set current date tho
End_Date field ? with button 'Yes' and 'No'. The 'Yes' button will perform
update into the table, the 'No' button will not do anything.

Please give me your advise. Thanks in advance.
 
Hi bienwell,


Try add following code in ItemCreated event

If e.Item.ItemType = ListItemType.Item OrElse
e.Item.ItemType =
ListItemType.AlternatingItem Then
' suppose the button in last column
CType(e.Item.Cells(e.Item.Cells.Count - 1).Controls(0),
LinkButton).Attributes.Add("OnClick", "confirm('message
here');")

endif


HTH

Elton Wang
(e-mail address removed)

-----Original Message-----
Hi,

I have a datagrid control with the declaration as follows:

<script runat="server">
Sub DGrid_ItemDataBound(sender As Object, e As DataGridItemEventArgs)
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType =
ListItemType.AlternatingItem Then
Dim drv As DataRowView = CType (e.Item.DataItem, DataRowView)
Dim EDate As Object = CType(drv.row ("End_Date"),Object)
If IsDbNull(EDate) Then
e.Item.Cells(9).Text = "No End Date"
e.Item.Cells(10).Visible=True
e.Item.Cells(10).Tooltip= "Check here to set today as the
end date."
Else
e.Item.Cells(10).Visible=False
End If
End If
End Sub

'******************************************************** ***************

Sub DGrid_ItemCommand(s as object, e As
System.Web.UI.WebControls.DataGridCommandEventArgs)
Dim commandName As String = e.CommandName
If commandName.Equals("Update") Then
Dim MyVar=Now()
Dim tempMonth As integer= Month(MyVar)
Dim tempDay As integer= Day(MyVar)
Dim CurrMonth as string
Dim CurrDay as string

If CInt(TempMonth) < 10 Then
CurrMonth = "0" & tempMonth
Else
CurrMonth = CStr(tempMonth)
End If

If CInt(TempDay) < 10 Then
CurrDay = "0" & tempDay
Else
CurrDay = CStr(tempDay)
End If

Dim CurrDate as string=Year(MyVar) & "-" & CurrMonth & "-" &
CurrDay
Dim myStr As String = " UPDATE
TBL_EFILING_USERS SET END_DATE='"
& CurrDate & "'" & _
" WHERE ACCOUNT_ID = '" & e.Item.Cells(0).Text & "';COMMIT;"
'response.write("<BR>myStr ==> " & myStr)

Dim myConnection as ODBCConnection
Dim myCommand as ODBCCommand

Dim strConn As String =
"server=Atlantis;uid=eFileUser;pwd=eFileUser1;DSN=Atlantis "
myConnection = New OdbcConnection(strConn)
myConnection.Open()
myCommand = New OdbcCommand(myStr, myConnection)
myCommand.ExecuteNonQuery()
myConnection.Close

Dim myDataSet as DataSet=GetDataSource()
DGrid_Carrier.DataSource=myDataSet
DGrid_Carrier.DataBind()
button1.Enabled=True

End If
End Sub
</script>


<html>
<head>
<title>Test Page</title>
</head>
<body>
<form runat="Server">
<asp:DropDownList id="CarrierList" Width="123px" Runat="Server"
OnSelectedIndexChanged="Carier_SelectedIndexChanged"
AutoPostBack="True"></asp:DropDownList>
<p></p>
<p></p>
<asp:DataGrid id="DGrid_Carrier" runat="server"
OnSelectedIndexChanged="IndexChange_Command"
OnItemDataBound="DGrid_ItemDataBound" OnItemCommand="DGrid_ItemCommand"
Width="353px" Font-Size="Smaller" Font-Names="Arial" CellSpacing="1"
CellPadding="3" AutoGenerateColumns="False">
<AlternatingItemStyle
backcolor="#E0E0E0"> said:
<HeaderStyle backcolor="LightYellow"></HeaderStyle>
<Columns>
<asp:BoundColumn DataField="Account_ID"
HeaderText="Account_ID"></asp:BoundColumn>
<asp:BoundColumn DataField="Carrier_Name" HeaderText="Carrier
Name"></asp:BoundColumn>
<asp:BoundColumn DataField="Carrier_Entity" HeaderText="Carrier
Entity"></asp:BoundColumn>
<asp:BoundColumn DataField="First_Name" HeaderText="First
Name"></asp:BoundColumn>
<asp:BoundColumn DataField="Office_Address"
HeaderText="Office_Address"></asp:BoundColumn>
<asp:BoundColumn DataField="Office_City"
HeaderText="Office_City"></asp:BoundColumn>
<asp:BoundColumn DataField="Office_ZipCode"
HeaderText="Office_ZipCode"></asp:BoundColumn>
<asp:BoundColumn DataField="Work_Phone"
HeaderText="Work_Phone"></asp:BoundColumn>
<asp:BoundColumn DataField="Start_Date"
HeaderText="Start_Date"></asp:BoundColumn>
<asp:BoundColumn DataField="End_Date" DataFormatString="{0:d}"
HeaderText="End_Date"></asp:BoundColumn>
<asp:ButtonColumn CommandName="Update" HeaderText="Edit End_Date"
HeaderImageUrl="edit_a.gif" Text="Update to Current Date!" />
</Columns>
</asp:DataGrid>
<P></P>
<asp:Button id= "Button1" text= "Submit"
Enabled="False" runat= "server"
 
Elton,

I've tried it. The Current Date still sets to the End date no matter I
clicked on 'Yes' or 'No' button. Here is the code:

Sub DGrid_ItemDataBound(sender As Object, e As DataGridItemEventArgs)
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType =
ListItemType.AlternatingItem Then
Dim drv As DataRowView = CType(e.Item.DataItem, DataRowView)
Dim EDate As Object = CType(drv.row("End_Date"),Object)
If IsDbNull(EDate) Then
e.Item.Cells(9).Text = "No End Date"
e.Item.Cells(10).Visible=True
e.Item.Cells(10).Tooltip= "Check here to set today as the
end date."
CType(e.Item.Cells(e.Item.Cells.Count - 1).Controls(0),
LinkButton).Attributes.Add("OnClick", "confirm('Are you sure you want to set
Current Date to the End_Date ?');")

Else
e.Item.Cells(10).Visible=False
End If
End If
End Sub

Any suggestions ? Thanks again.

============================
 
Add return before confirm('...')

It's like:

CType(e.Item.Cells(e.Item.Cells.Count - 1).Controls(0),
LinkButton).Attributes.Add("OnClick", "return confirm('Are
you sure you want to set Current Date to the
End_Date ?');")

Elton
 
Elton,

It worked fine with the Return added for the confirmation message box. One
more question: Can we replace the text with Command Argument in
ButtonColumn by the GIF file ?

Here is the last column in my datagrid control :
<asp:ButtonColumn CommandName="Update" HeaderText="Edit End_Date"
HeaderImageUrl="edit_a.gif" Text="Update to Current Date!" />

Thanks you for your help

==============================================================
 
Back
Top