Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Select Case e.Item.ItemType - not breaking into when binding data.

Posted on 2007-11-16
13
Medium Priority
?
256 Views
Last Modified: 2013-11-26
I have the following datagrid:
<asp:DataGrid DataKeyField="FIXTUREID" ID="dgFixtures" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:TemplateColumn HeaderText="HomeTeam">
            <ItemTemplate>
                <input type="hidden" id="hidHomeTeamID" runat="server" value='<%# Container.DataItem("HOMETEAMID")%>' name="hidHomeTeamID"/>
                <asp:Label ID="lblHomeTeam" runat="server" Text='<%# Container.DataItem("HOMETEAM") %>'></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:DropDownList ID="ddHomeTeam" runat="server"></asp:DropDownList>
            </EditItemTemplate>
        </asp:TemplateColumn>
        <asp:TemplateColumn headertext="">
                  <ItemTemplate>
                        <asp:LinkButton id="lnkEdit" Runat="server" Text="Edit" CommandName="Edit"></asp:LinkButton>
                  </ItemTemplate>            
                  <EditItemTemplate>
                      <asp:LinkButton id="lnkSave" Runat="server" Text="Save" ></asp:LinkButton>
                  </EditItemTemplate>                        
            </asp:TemplateColumn>
            <asp:TemplateColumn headertext="">
                  <ItemTemplate>
                        <asp:LinkButton id="lnkDelete" Runat="server" Text="Delete" CommandName="Delete"></asp:LinkButton>
                  </ItemTemplate>
                  <EditItemTemplate>
                      <asp:LinkButton id="lnkCancel" Runat="server" Text="Cancel" ></asp:LinkButton>
                  </EditItemTemplate>      
                  <FooterTemplate>
                        <asp:LinkButton id="lnkAdd" Runat="server" Text="Add" ></asp:LinkButton>
                  </FooterTemplate>
            </asp:TemplateColumn>
    </Columns>
</asp:DataGrid>


I then have:
 Protected Sub dgFixtures_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgFixtures.ItemDataBound
        Dim strHomeTeams As String = ""

        strHomeTeams = "SELECT * FROM TEAMS"

        Select Case e.Item.ItemType
            Case ListItemType.Item
                CType(e.Item.FindControl("lnkEdit"), LinkButton).Text = "Edit"
                CType(e.Item.FindControl("lnkDelete"), LinkButton).Text = "Delete"
            Case ListItemType.EditItem
                CType(e.Item.FindControl("lnkSave"), LinkButton).Text = "Save"
                CType(e.Item.FindControl("lnkCancel"), LinkButton).Text = "Cancel"

                'Home Team
                Dim ddHomeTeam As DropDownList
                Dim dsHomeTeam As New SqlDataSource
                Dim strHomeTeamID As String = CType(e.Item.FindControl("hidHomeTeamID"), HtmlInputHidden).Value
                ddHomeTeam = e.Item.FindControl("ddHomeTeam")

                dsHomeTeam.ConnectionString = ConfigurationManager.ConnectionStrings.ToString
                dsHomeTeam.SelectCommand = "SELECT * FROM TEAMS"
                dsHomeTeam.SelectCommandType = SqlDataSourceCommandType.Text

                ddHomeTeam.DataSource = dsHomeTeam
                ddHomeTeam.DataBind()
             
            Case ListItemType.Footer
        End Select
    End Sub

However it never steps into:
 Case ListItemType.Item or
  Case ListItemType.EditItem

when i click the edit link.

Can any1 tell me why as im still getting to grips with this properly..

thanks in advance.
0
Comment
Question by:scm0sml
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 6
13 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 20300836
dgFixtures_ItemDataBound is called when dgFixtures is bound to a data source.  Where does that happen?

Bob
0
 

Author Comment

by:scm0sml
ID: 20300886
in a function that gets called everytime on page load.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 20300922
1) Binding the DataGrid to a data source every time the page loads will cause other problems.

2) When the DataGrid is bound, and the DataGrid.DataBind method is called, the ItemDatabound should fire.

Bob
0
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 

Author Comment

by:scm0sml
ID: 20322190
I have:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        DisplayFixturesData()        
    End Sub

Private Sub DisplayFixturesData()
        dsFixtures.ConnectionString = "blahblah"
        dsFixtures.SelectCommand = GetFixtureSQL()'returns a sql string
        dsFixtures.SelectCommandType = SqlDataSourceCommandType.Text

        dgFixtures.DataSource = dsFixtures
        dgFixtures.DataBind()
    End Sub


Protected Sub dgFixtures_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgFixtures.ItemDataBound
        Dim strHomeTeams As String = ""

        strHomeTeams = "SELECT * FROM TEAMS"

        Select Case e.Item.ItemType
            Case ListItemType.Item
                CType(e.Item.FindControl("lnkEdit"), LinkButton).Text = "Edit"
                CType(e.Item.FindControl("lnkDelete"), LinkButton).Text = "Delete"
            Case ListItemType.EditItem
                CType(e.Item.FindControl("lnkSave"), LinkButton).Text = "Save"
                CType(e.Item.FindControl("lnkCancel"), LinkButton).Text = "Cancel"
etc etc

Why isn't it stepping into here? - Case ListItemType.Item
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 20322231
Try something like this:
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
      If Not Page.IsPostBack Then
        DisplayFixturesData()    
      End If    
    End Sub

Open in new window

0
 

Author Comment

by:scm0sml
ID: 20322318
still not hitting the edititem.

does:
<asp:TemplateColumn headertext="">
                  <ItemTemplate>
                        <asp:LinkButton id="lnkEdit" Runat="server" Text="Edit" CommandName="Edit"></asp:LinkButton>
                  </ItemTemplate>            
                  <EditItemTemplate>
                      <asp:LinkButton id="lnkSave" Runat="server" Text="Save" ></asp:LinkButton>
                  </EditItemTemplate>                        
            </asp:TemplateColumn>

look ok to you? - CommandName="Edit"?
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 20322466
Hmmm...

1) If you have 2.0, why aren't you using the GridView?  

2) If you have an ItemTemplate and an EditItemTemplate, why are you trying to change buttons in the ItemDataBound?

3) The GridView can handle that stuff automagically.

Bob
0
 

Author Comment

by:scm0sml
ID: 20322519
the reason for it was because i want to be able to populate dropdowns with information from a database.

so say for argument sake i had a fixture and wanted to change the away team, i would click edit the label would turn into a dropdown populated with teams from the database and from their i can choose a new one...........

whenever i used the gridview it would just populate a textbox with the value of the label and i needed more than this.

is it possible to populate a dropdown  from the database using a gridview?
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 20322973
If you have a DropDownList in an EditItemTemplate, then you can bind that to an SqlDataSource:

<EditItemTemplate>
    <asp:DropDownList ID="ddHomeTeam" runat="server" DataSourceID="Teams" />
    <asp:SqlDataSource ID="Teams" ... />
</EditItemTemplate>

Bob
0
 

Author Comment

by:scm0sml
ID: 20323349
so we are not talking gridviews now?
you will have to ezxcuse me ive not done much with .net and am still getting to grips.

there is no <edititemtemplate available in grdiview?
0
 

Author Comment

by:scm0sml
ID: 20323482
this is a really useful link for what we are trying to do here.

http://msdn2.microsoft.com/en-gb/library/bb426884.aspx
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 2000 total points
ID: 20327128
I am still talking about using a GridView, and that code example was only a snippet from a TemplateField with the EditItemTemplate defined.  

Another way is to build a DataTable, and then bind each DropDownList control for each GridView row to a DataView created from the single DataTable in the RowDataBound event handler.  The key here is that you wouldn't want to bind the DropDownList to the same data source.

Bob
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question