Solved

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

Posted on 2007-11-16
13
248 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
Quiz: What Do These Organizations Have In Common?

Hint: Their teams ended up taking quizzes, too.

 

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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

688 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