Edit Button not working properly.. help please


B

Burak Gunay

Hello everyone,

I am including the code and I am going to describe the issue I am
having. I would appreciate it if you would take a look at this.

Ok, what I am trying to do is to have a dynamic datagrid where we are
pulling the column names and the column data from a dataset. 1st step
is to define a textboxcolumn class as follows

Class myTextBoxColumn
Implements ITemplate
Dim templateType As ListItemType
Dim columnName As String

Sub New(ByVal type As ListItemType, ByVal ColName As String)
templateType = type
columnName = ColName
End Sub

Public Sub InstantiateIn(ByVal container As
System.Web.UI.Control) Implements System.Web.UI.ITemplate.InstantiateIn
Select Case templateType
Case ListItemType.Item
Dim lc As New Literal
AddHandler lc.DataBinding, AddressOf
Me.BindLiteralColumn
container.Controls.Add(lc)
Case ListItemType.EditItem
Dim tb As New TextBox
If columnName = "id" Then
tb.Enabled = False
End If
If columnName <> "Reason" Then
AddHandler tb.DataBinding, AddressOf
Me.BindTextBoxColumn
Else
tb.ID = "txtReason"
tb.TextMode = TextBoxMode.MultiLine
tb.Rows = "4"
tb.Width =
System.Web.UI.WebControls.Unit.Pixel(185)
tb.MaxLength = 200
End If
container.Controls.Add(tb)
End Select
End Sub
Sub BindTextBoxColumn(ByVal sender As Object, ByVal e As
EventArgs)
Dim txtBox As TextBox = CType(sender, TextBox)
Dim container As DataGridItem =
CType(txtBox.NamingContainer, DataGridItem)
txtBox.ID = columnName
txtBox.Text =
Convert.ToString(DataBinder.Eval((CType(container,
DataGridItem)).DataItem, columnName))
End Sub
Sub BindLiteralColumn(ByVal sender As Object, ByVal e As
EventArgs)
Dim lc As Literal = CType(sender, Literal)
Dim container As DataGridItem = CType(lc.NamingContainer,
DataGridItem)
lc.ID = columnName
lc.Text =
Convert.ToString(DataBinder.Eval((CType(container,
DataGridItem)).DataItem, columnName))
End Sub
End Class

and then we pull data from a table and loop through it to fill the
datagrid "dtgValues"

Sub FillGrid()

' get dataset with record values
Dim ds As DataSet = GetRecords(sql)
Dim i, count As Integer
Dim colName As String
' go through the list of columns
count = ds.Tables(0).Columns.Count
If count > 0 Then
For i = 0 To count - 1
colName = ds.Tables(0).Columns(i).ColumnName

Dim col As New TemplateColumn
col.ItemTemplate = New myTextBoxColumn(ListItemType.Item, colName)
col.EditItemTemplate = New myTextBoxColumn(ListItemType.EditItem,
colName)
col.HeaderText = colName
col.HeaderStyle.ForeColor = System.Drawing.Color.White
dtgValues.Columns.Add(col)
Next
' add edit column
Dim colEdit As New EditCommandColumn
colEdit.ButtonType = ButtonColumnType.PushButton
colEdit.UpdateText = "Update"
colEdit.EditText = "Edit"
colEdit.CancelText = "Cancel"
dtgValues.Columns.Add(colEdit)
' bind data
dtgValues.DataSource = ds.Tables(0)
dtgValues.DataBind()
End If

End Sub

now all this works fine, the columns and the values show up. Since this
datagrid gets created dynamically, we need to create the datagrid on
each pageload as shown, so as not to lose the values in the datagrid

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
FillGrid()
End Sub

Ok, the problem is, when the Edit button is clicked, nothing happens,
and if you click it a second time, it automatically goes into Update
mode.

Private Sub dtgValues_EditCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
dtgValues.EditCommand
' Set the EditItemIndex property to the index of the item clicked
' in the DataGrid control to enable editing for that item.
dtgValues.EditItemIndex = e.Item.ItemIndex
Session("editIndex") = e.Item.ItemIndex
' FillGrid(e.Item.ItemIndex)
End Sub

How can we have the edit button work just like a datagrid with all the
columns predefined?.

Thank you,

Burak
 
Ad

Advertisements

C

Chattanooga

How can we have the edit button work just like a datagrid with all the
columns predefined?.

Thank you,

Burak

hi,

i just had a most similar problem using the "PushButton" argument in
the edit column. no go. then i used the regular "LinkButton" and it
worked fine.....

regards,
Chris
 
B

Burak Gunay

Hi Chris,

Thanks for the answer.

Would you also happen to know the answer to the following questions

- how do you dynaically ad one button at the bottom of a datagrid? I
want a "Save" button to appear in the middle of the bottom of the
datagrid. I know you can add a button at the foot of a template column
but is there a way to do this without specifying a column. I guess what
I'd like to do is add a buttom to the footerstyle.

- how do you add an onclick event like

OnClick="javascript: return select_deselectAll (this.checked,
this.id);"

to a dynamically constructed checkbox?

I wrote the following but it is not correct I think

Dim chkAll As New CheckBox
AddHandler chkAll.CheckedChanged, AddressOf Me.HandleCheckedChanged
container.Controls.Add(chkAll)
Sub HandleCheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
' this part is wrong
"javascript: return select_deselectAll (this.checked,
this.id);"
End Sub

Thanks,

Burak
 
Ad

Advertisements

Y

Yogesh

problem with linkbutton

When I add linkbutton it does not show clickable ....

pls send code in C# syntax
 

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