Solved

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

Posted on 2004-10-06
6
1,111 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 125 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

Industry Leaders: 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

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
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…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

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