Home All Groups Group Topic Archive Search About

GridView with dropdown: asp.net 2.0

Author
22 Dec 2005 11:43 PM
sck10
Hello,

I am using the following in a GridView.  Currently, I am using a dropdown
that pulls the Name of the person, given the Employee ID.  I am doing this
in both the ItemTemplate and the EditTemplate.  This makes sense for the
EditTemplate becuase the user can then change the Employee using the
dropdown box.  What I am trying to figure out is how to show the Employee
Name in the ItemTemplate without having to use a DropDownList Box.  Is there
a way to reference the stored procedure (below) without using a
dropdownlist?
--
Thanks in advance,

sck10


<!--  Datasource: Dropdownlist: Contact Employee ID -->
  <asp:SqlDataSource ID="dsEmployeeIDDLL" runat="server"
ConnectionString="<%$ ConnectionStrings:cnnSvctech %>"
SelectCommand="sp_web_LabTrack" SelectCommandType="StoredProcedure">
    <SelectParameters>
      <asp:Parameter DefaultValue="FindContactDLL" Name="strParm01"
Type="String" />
      <asp:Parameter DefaultValue="NoParameter" Name="strParm02"
Type="String" />
      <asp:Parameter DefaultValue="NoParameter" Name="strParm03"
Type="String" />
      <asp:Parameter DefaultValue="NoParameter" Name="strParm04"
Type="String" />
    </SelectParameters>
  </asp:SqlDataSource>

<asp:TemplateField
  HeaderText="Activity"
  HeaderStyle-Font-Bold="true"
  HeaderStyle-ForeColor="black"
  ItemStyle-Width="70%"
  ItemStyle-VerticalAlign="Top" >
  <ItemTemplate>
  <asp:DropDownList id="ddlEmployeeID1" DataSourceID="dsEmployeeIDDLL"
Runat="Server"
DataTextField="ContactName" DataValueField="EmployeeID" SelectedValue='<%#
Bind("EmployeeID") %>'/>
<%--<asp:Label ID="lblEmployeeID" Text='<%# Eval("EmployeeID") %>'
Runat="Server"/>--%>
  </ItemTemplate>
  <EditItemTemplate>
  <asp:DropDownList id="ddlEmployeeID" DataSourceID="dsEmployeeIDDLL"
Runat="Server"
DataTextField="ContactName" DataValueField="EmployeeID" SelectedValue='<%#
Bind("EmployeeID") %>'/>
  </EditItemTemplate>
</asp:TemplateField>

Author
23 Dec 2005 7:08 AM
Steven Cheng[MSFT]
Hi Sck10,

Welcome.
I think for your scenario, the DropDownList control is necessary  because
we need it to pickup all the sub item list from DataBase and  mapped the
curernt row's subitem index to it's Text property...  However, if you do
need to avoid diplaying the text through DropDownList, we can consider put
another separate Label control in the TemplateColumn's Itemtemplate
together with the DropDownList, and use the Label control's PreRender event
to set Text value from the DropDownList's Selected Text .....  (the
dropDownList's Visible is set to False...).  below is some sample code
snippet:

<ItemTemplate>
                         <asp:DropDownList ID="lstCategory" runat="server"
DataSourceID="SqlDataSource2" DataTextField="CategoryName"
                            DataValueField="CategoryID" SelectedValue='<%#
Bind("CategoryID") %>' Visible="False">
                        </asp:DropDownList>&nbsp;
                        <asp:Label ID="Label1" runat="server"
OnPreRender="Label1_PreRender" Text="Label"></asp:Label>

               </ItemTemplate>
                </asp:TemplateField>


protected void Label1_PreRender(object sender, EventArgs e)
    {
        Label lbl = sender as Label;

        DropDownList lst = lbl.NamingContainer.FindControl("lstCategory")
as DropDownList;

        if (lst != null)
        {
            lbl.Text = lst.SelectedItem.Text;
        }
    }


Hope helps. Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)




--------------------
| From: "sck10" <sck10@online.nospam>
| Subject: GridView with dropdown: asp.net 2.0
| Date: Thu, 22 Dec 2005 17:43:45 -0600
| Lines: 55
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1506
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1506
| Message-ID: <eZutNG1BGHA.2***@tk2msftngp13.phx.gbl>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| NNTP-Posting-Host: 189.202.185.135.in-addr.arpa 135.185.202.189
| Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
| Xref: TK2MSFTNGXA02.phx.gbl
microsoft.public.dotnet.framework.aspnet:366682
Show quote
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| Hello,
|
| I am using the following in a GridView.  Currently, I am using a dropdown
| that pulls the Name of the person, given the Employee ID.  I am doing this
| in both the ItemTemplate and the EditTemplate.  This makes sense for the
| EditTemplate becuase the user can then change the Employee using the
| dropdown box.  What I am trying to figure out is how to show the Employee
| Name in the ItemTemplate without having to use a DropDownList Box.  Is
there
| a way to reference the stored procedure (below) without using a
| dropdownlist?
| --
| Thanks in advance,
|
| sck10
|
|
| <!--  Datasource: Dropdownlist: Contact Employee ID -->
|   <asp:SqlDataSource ID="dsEmployeeIDDLL" runat="server"
| ConnectionString="<%$ ConnectionStrings:cnnSvctech %>"
| SelectCommand="sp_web_LabTrack" SelectCommandType="StoredProcedure">
|     <SelectParameters>
|       <asp:Parameter DefaultValue="FindContactDLL" Name="strParm01"
| Type="String" />
|       <asp:Parameter DefaultValue="NoParameter" Name="strParm02"
| Type="String" />
|       <asp:Parameter DefaultValue="NoParameter" Name="strParm03"
| Type="String" />
|       <asp:Parameter DefaultValue="NoParameter" Name="strParm04"
| Type="String" />
|     </SelectParameters>
|   </asp:SqlDataSource>
|
| <asp:TemplateField
|   HeaderText="Activity"
|   HeaderStyle-Font-Bold="true"
|   HeaderStyle-ForeColor="black"
|   ItemStyle-Width="70%"
|   ItemStyle-VerticalAlign="Top" >
|   <ItemTemplate>
|   <asp:DropDownList id="ddlEmployeeID1" DataSourceID="dsEmployeeIDDLL"
| Runat="Server"
| DataTextField="ContactName" DataValueField="EmployeeID" SelectedValue='<%#
| Bind("EmployeeID") %>'/>
| <%--<asp:Label ID="lblEmployeeID" Text='<%# Eval("EmployeeID") %>'
| Runat="Server"/>--%>
|   </ItemTemplate>
|   <EditItemTemplate>
|   <asp:DropDownList id="ddlEmployeeID" DataSourceID="dsEmployeeIDDLL"
| Runat="Server"
| DataTextField="ContactName" DataValueField="EmployeeID" SelectedValue='<%#
| Bind("EmployeeID") %>'/>
|   </EditItemTemplate>
| </asp:TemplateField>
|
|
|

AddThis Social Bookmark Button