Solved

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

Posted on 2007-11-16
13
244 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
Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

730 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