Solved

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

Posted on 2007-11-16
13
231 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
  • 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
 

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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 500 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This video discusses moving either the default database or any database to a new volume.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

744 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now