Logo 
Search:

Asp.net Forum

Ask Question   UnAnswered
Home » Forum » Asp.net       RSS Feeds

Button in datagrid

  Asked By: Kristopher    Date: Oct 19    Category: Asp.net    Views: 1246
  

i'm trying for hours to get a button in a datagrid. When clicked this button i
want to take a value of this table (movieName) and use it for redirection...
I've done some similar work before, but now it simply don't work... i don't know
what the reason is...
Also the editCommand & co doesn't work, i guess this is the same problem...

somebody can see what i'm doing wrong?
Has it smth to do with the dataview i use?

code snippet:


<asp:datagrid id="DataGrid1" EnableViewState="True" runat="server"
AutoGenerateColumns="False" OnEditCommand="DoItemEdit"
OnUpdateCommand="DoItemUpdate" OnDeleteCommand="DoItemDelete"
OnCancelCommand="DoItemCancel" OnItemCommand="ShowInfo">

<Columns>
<asp:BoundColumn Visible="False" DataField="movieID"
HeaderText="movieID"></asp:BoundColumn>
<asp:BoundColumn Visible="False" DataField="userID"
HeaderText="userID"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Titel">
<ItemTemplate>
<asp:Label ID="lblmovieName" Text='<%#
DataBinder.Eval(Container.DataItem, "movieName") %>' Runat="server"/>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtmovieName" Width="300" Text='<%
DataBinder.Eval(Container.DataItem, "movieName") %>' Runat="server"/>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Aantal CD's">
<ItemTemplate>
<asp:Label Text='<%# DataBinder.Eval(Container.DataItem,
"movieAantalCD") %>' Runat="server" ID="lblmovieAantalCD"/>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtmovieAantalCD" Width="25" Text='<%
DataBinder.Eval(Container.DataItem, "movieAantalCD") %>' Runat="server"/>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="FrameRate">
<ItemTemplate>
<asp:Label Text='<%# DataBinder.Eval(Container.DataItem,
"movieFrameRate") %>' Runat="server" ID="lblmovieFrameRate"/>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtmovieFrameRate" Width="70" Text='<%
DataBinder.Eval(Container.DataItem, "movieFrameRate") %>' Runat="server"/>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Codec">
<ItemTemplate>
<asp:Label Text='<%# DataBinder.Eval(Container.DataItem,
"movieCodec") %>' Runat="server" ID="lblmovieCodec"/>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtmovieCodec" Width="150" Text='<%
DataBinder.Eval(Container.DataItem, "movieCodec") %>' Runat="server"/>
</EditItemTemplate>
</asp:TemplateColumn>


<asp:TemplateColumn>
<ItemTemplate>
<asp:Button Text="Info!" id="btnInfo" CommandName="Info"
Runat="server"></asp:Button>
</ItemTemplate>
</asp:TemplateColumn>



<asp:EditCommandColumn ButtonType="PushButton" UpdateText="Update"
HeaderText="Bewerk" CancelText="Cancel"
EditText="Bewerk"></asp:EditCommandColumn>
<asp:ButtonColumn Visible="False" Text="Delete"
ButtonType="PushButton" CommandName="Delete"></asp:ButtonColumn>
</Columns>
</asp:datagrid>

CODE BEHIND:


Protected Sub BindGrid()
Dim mySelection As String = Request.QueryString("Letter")
Dim findString As String
If (mySelection = "0..9") Then
findString = "movieName LIKE '0*'"
findString += "OR movieName LIKE '2*'"
findString += "OR movieName LIKE '3*'"
findString += "OR movieName LIKE '4*'"
findString += "OR movieName LIKE '5*'"
findString += "OR movieName LIKE '6*'"
findString += "OR movieName LIKE '7*'"
findString += "OR movieName LIKE '8*'"
findString += "OR movieName LIKE '9*'"
Else
findString = "movieName LIKE '" & mySelection & "*'"
End If

MyDS1 = CType(Session("myDataSet"), DataSet)
Dim MyDataTable As DataTable = MyDS1.Tables("FilmTabel")
Dim objDataView As New DataView(MyDataTable)
objDataView.RowFilter = findString
DataGrid1.DataSource = objDataView
DataGrid1.DataBind()
DataGrid1.Visible = True

End Sub
Public Sub ShowInfo(ByVal objS As System.Object, ByVal objArgs As
DataGridCommandEventArgs)

If objArgs.CommandSource.commandname = "Info" Then

Dim myName As String = objArgs.Item.Cells(2).Text()
TextBox1.Text = "test" 'myName
Response.Redirect("../webFrom2.aspx?movie=" & myName)

End If
End Sub

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
'stop in session om round-trip te vermijden bij iedere postback
BindList()
If Session("init") Then
SqlSelectCommand1.Parameters("@userID").Value = "1"
'maak een tabel aan om in de dataSet te steken
Dim myTable As DataTable = New DataTable("FilmTabel")
'voeg toe aan dataset
MyDS1.Tables.Add(myTable)
'Vull de dataTable met waarden uit DataBase
SqlDataAdapter1.Fill(myTable)
Session("myDataSet") = MyDS1
DataGrid1.Visible = False
Session("init") = False
Else
BindGrid()
End If
End Sub


Sub DoItemEdit(ByVal objSource As Object, ByVal objARgs As
DataGridCommandEventArgs)
'note: eerst datagrid binden, dan pas de index meegeven, omdat anders de
'edit uitgevoerd wordt, maar de gegevens nog niet aanwezig zijn in de dataset
DataGrid1.EditItemIndex = -1
BindGrid()
DataGrid1.EditItemIndex = objARgs.Item.ItemIndex
DataGrid1.Columns(7).Visible = True

End Sub

Sub DoItemUpdate(ByVal objSource As Object, ByVal objARgs As
DataGridCommandEventArgs)
DataGrid1.EditItemIndex = -1
BindGrid()
DataGrid1.Columns(7).Visible = False
End Sub

Sub DoItemDelete(ByVal objSource As Object, ByVal objARgs As
DataGridCommandEventArgs)
DataGrid1.EditItemIndex = -1
B indGrid()
DataGrid1.EditItemIndex = objARgs.Item.ItemIndex
DataGrid1.Columns(7).Visible = False
End Sub

Sub DoItemCancel(ByVal objSource As Object, ByVal objARgs As
DataGridCommandEventArgs)

DataGrid1.EditItemIndex = -1
BindGrid()
DataGrid1.Columns(7).Visible = False

End Sub

Share: 

 

7 Answers Found

 
Answer #1    Answered By: Shobhana R.     Answered On: Oct 19

I struggled with this for a while myself and found the easiest answer is
to make the buttoncolumn ButtonType="PushButton" ... As I understand it,
Hyperlinks buttons don't raise postback unless you alter the clientside
code during Render (or at least I couldn't get them to)

 
Answer #2    Answered By: Carl Woods     Answered On: Oct 19

Still the same problem...
i was already working with a <itemtemplate> with in it a <asp:button> , have
changed it by a buttoncolumn, but this is the same...
The page  is in both cases post back but don't interact on my button...

my code  see under...

 
Answer #3    Answered By: Adal Fischer     Answered On: Oct 19

Don't know, haven;t used a dtagrid - I tend to stick with datalist and custom
paging.

All I can suggest is that you put a regular <asp:button in somewhere with
commandname="Edit" ... just to test  it.

See what happens I suppose

 
Answer #4    Answered By: Devlan Jones     Answered On: Oct 19

Having said that this doesn't look right

DataGrid1.EditItemIndex = -1
BindGrid()
DataGrid1.EditItemIndex = objARgs.Item.ItemIndex

it should be

DataGrid1.EditItemIndex = -1
DataGrid1.EditItemIndex = objARgs.Item.ItemIndex
BindGrid()

else you don't bind till th enext time round, well you do, but you bind to -1.

In your current code  click the same edit button  twice and it will work  ( I
suspect).

 
Answer #5    Answered By: Heru Chalthoum     Answered On: Oct 19

i've followed your instructions for this the editpart but have still a
problem...

when i click on the button  edit, i get the edit text  fields but in stead of the
real name of the movie i get <% DataBinder.eval(...)%> in the textbox
How can i avoid that?

BTW: first problem  is solved, but i don't know exactly how, i changed some stuff
like handles datagrid1.itemcommand, and now it works just fine

<asp:TemplateColumn HeaderText="Titel">
<ItemTemplate>
<asp:Label ID="lblmovieName" Text='<%#
DataBinder.Eval(Container.DataItem, "movieName") %>' Runat="server"/>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtmovieName" Width="300" Text='<%
DataBinder.Eval(Container.DataItem, "movieName") %>' Runat="server"/>
</EditItemTemplate>
</asp:TemplateColumn>

Having said that this doesn't look right

DataGrid1.EditItemIndex = -1
BindGrid()
DataGrid1.EditItemIndex = objARgs.Item.ItemIndex

it should be

DataGrid1.EditItemIndex = -1
DataGrid1.EditItemIndex = objARgs.Item.ItemIndex
BindGrid()

else you don't bind till th enext time round, well you do, but you bind to -1.

In your current code  click the same edit button twice and it will work  ( I
suspect).

 
Answer #6    Answered By: Murad Bashara     Answered On: Oct 19

When use databinding the enclosing tags are <%# %>
So you should have this

<%# DataBinder.Eval(Container.DataItem, "movieName") %>

 
Answer #7    Answered By: Juan Reynolds     Answered On: Oct 19

I've still got a kind of a similar problem:
when using this commandbutton in the datagrid  i want to get the name in the
third column...
but this gives an empty string
When using cells(2) there is no problem  at all, but there i'm using a
boundcolumn...


Public Sub ShowInfo(ByVal objSender As System.Object, ByVal objArgs As
DataGridCommandEventArgs) ' Handles DataGrid1.ItemCommand
If objArgs.CommandSource.commandname = "Info" Then

Dim myName As string  = objArgs.Item.Cells(3).Text
Response.Redirect("./webForm1.aspx?myMovie=" & myName)

End If
End Sub
my cell(3):

<asp:TemplateColumn HeaderText="Titel">
<ItemTemplate>
<asp:Label ID="lblmovieName"
Text='<%#DataBinder.Eval(Container.DataItem, "movieName") %>' Runat="server"/>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtmovieName" Width="300" Text='<%#
DataBinder.Eval(Container.DataItem, "movieName") %>' Runat="server"/>
</EditItemTemplate>
</asp:TemplateColumn>

 
Didn't find what you were looking for? Find more on Button in datagrid Or get search suggestion and latest updates.




Tagged: