Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2004-10-06
6
Medium Priority
?
1,115 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
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!

 

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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Screencast - Getting to Know the Pipeline
Suggested Courses

610 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