Solved

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

Posted on 2004-10-06
6
1,108 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
  • 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

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

DevOps Toolchain Recommendations

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

Question has a verified solution.

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

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

809 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