Solved

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

Posted on 2004-10-06
6
1,105 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

762 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

19 Experts available now in Live!

Get 1:1 Help Now