Solved

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

Posted on 2007-11-16
13
234 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

3 Use Cases for Connected Systems

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

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Vb.net dynamic formulas in runtime 11 63
Syntax error 9 45
Server Error 11 47
ASP.NET 5 Templates 2 66
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

867 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

13 Experts available now in Live!

Get 1:1 Help Now