?
Solved

How to get the selected value of a dropdown list in datagrid

Posted on 2004-10-06
6
Medium Priority
?
1,113 Views
Last Modified: 2008-02-26
Hi.

I have some problem in getting the selected value from a dropdown list in a datagrid.

in my current site:
http://uow.aspxconnection.com/datagrid.aspx

You can see, in the datagrid, a dropdown list of every players in a team. This is good. Now, everytime the "More info" is click. I want to display all information about the player selected in the dropdown list not the team information like I have done. What should I do to achieve this.

    <form runat="server">
        <p>
            <asp:Label id="Label1" runat="server" text="Danh sách:">Team</asp:Label>
            <asp:DropDownList id="lstCategory" runat="server"></asp:DropDownList>
            <asp:Button id="Button1" onclick="Button1_Click" runat="server" Text="List"></asp:Button>
        </p>
        <p>
            <asp:DataGrid id="DataGrid1" runat="server" ShowHeader="False">
                <HeaderStyle forecolor="Navy" backcolor="Silver"></HeaderStyle>
            </asp:DataGrid>
        </p>
        <p>
            <asp:DataGrid   id="DataGrid3" runat="server" AutoGenerateColumns="False" Width="288px" Height="63px"
                            CellPadding="5"
                            GridLines="None"
                            HeaderStyle-BackColor="silver"
                            HeaderStyle-HorizontalAlign="center"
                            FooterStyle-BackColor="silver"
                            ShowFooter="True"
                            OnItemCommand="ShowInfo">
                <Columns>
                    <asp:TemplateColumn HeaderText="ID">
                        <ItemTemplate>
                            <%# Container.DataItem("TeamID") %>
                        </ItemTemplate>
                    </asp:TemplateColumn>
                    <asp:TemplateColumn HeaderText="Name">
                        <ItemTemplate>
                            <%# Container.DataItem("TeamName") %>
                        </ItemTemplate>
                    </asp:TemplateColumn>
                    <asp:TemplateColumn HeaderText="Players">
                        <ItemTemplate>
                            <asp:DropDownList id="ddlList" runat="server" DataSource='<%# GetPlayers(Convert.ToInt32(Container.DataItem("TeamID")))%>' DataTextField="PlayerName" DataValueField="PlayerID" ></asp:DropDownList>
                        </ItemTemplate>
                    </asp:TemplateColumn>
                    <Asp:TemplateColumn HeaderText="" Itemstyle-BackColor="lightblue">
                        <ItemTemplate>
                            <Asp:Button id="mmdInfo" Text="More Info" CommandName="Info" runat="serveR" />
                        </ItemTemplate>
                    </Asp:TemplateColumn>
                </Columns>
            </asp:DataGrid>
        </p>
        <p>
            <asp:Label id="lblInfo" runat="server">  <asp:Label>  
        </p>
        <!-- Insert content here -->
    </form>


And this is my code:

    Sub ShowInfo(objSender As Object, objArgs As DataGridCommandEventArgs)

       'runs when any command button in the grid is clicked
       'see if the CommandName of the clicked button was "Info"
       If objArgs.CommandSource.CommandName = "Info" Then

        'I want to get the selected value of the dropdown list in the grid
          lblInfo.Text = "here it is: " &lstCategory.SelectedItem.Text
       End If
0
Comment
Question by:thl01
[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
  • 3
  • 3
6 Comments
 
LVL 28

Accepted Solution

by:
mmarinov earned 500 total points
ID: 12246122
Hi,

use this
    Sub ShowInfo(objSender As Object, objArgs As DataGridCommandEventArgs)

       'runs when any command button in the grid is clicked
       'see if the CommandName of the clicked button was "Info"
       If objArgs.CommandSource.CommandName = "Info" Then
        Dim ddl as DropDownList = CType(objArgs.Item.FindControl("ddlList"), DropDownList )

        'I want to get the selected value of the dropdown list in the grid
          lblInfo.Text = "here it is: " & ddl.SelectedItem.Text
       End If

Regards,
B..M
0
 

Author Comment

by:thl01
ID: 12246154
Hi.

Thanks for your effort. But I am pretty sure that it's not working. The error is that the ddlList in the dataGrid is not considered to be declared.

Something like this : objArgs.Item.Cells(1).Text will provide the text of the cell in the datagrid. But I don't know how to get the value from dropdown list (remember, it is in the datagrid)

Regards,
Hai
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 12246175
yes, i know that it is in the datagrid, and the only way to get a control from a datagrid is to find it by value or if you want to it like this


Dim ddl as DropDownList = CType(objArgs.Item.Cells(2).Controls(0), DropDownList )

you can not use Text property because there are 3 controls in the Cells(2)
one literal , one dropdownlist and again one literal

what exactly error do you receive ?

B..M
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:thl01
ID: 12246233
Hi,

It's great. Sorry for my mistake. I didn't see
 Dim ddl as DropDownList = CType(objArgs.Item.FindControl("ddlList"), DropDownList )
in your reponse. Very fast and good reponse. I love it.
Again Thanks.

By the way. you told me that there are 3 controls in the Cells(2). Could you explain more for me.

Regards,
Hai
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 12246297
when you add a control to a cell in a datagrid, .net framework puts ( i can not explain why ) 2 additional controls - as first and as last control in the collection - these 2 controls are always literal control

sorry for can not say more

B..M
0
 

Author Comment

by:thl01
ID: 12246316
That enough for me up to this point.
Thanks again.
Hai
0

Featured Post

Independent Software Vendors: 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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses
Course of the Month8 days, 16 hours left to enroll

764 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