Wrox Programmer Forums
|
ASP.NET 4.5 General Discussion For ASP.NET 4.5 discussions not relating to a specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 4.5 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 16th, 2014, 01:10 PM
Registered User
 
Join Date: May 2014
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Finding control in Item Template

Frustration level - high
How do you get the data that comes back from a select statement insid a list control. I can get controls in insert and update templates. But how on earth do you find a control in the Item template? And if that is not possible, how can you get that returned data of a specific field?

Anybody?

Reason is, I want to take that data that is returned from an SQL query (inside a ListView control) and use that to drive a Grid View.

Last edited by Bruce Weber; May 16th, 2014 at 02:43 PM..
 
Old May 16th, 2014, 05:26 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

I am not sure I understand what you're asking. What are you trying to accomplish? Can you elaborate?

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old May 16th, 2014, 06:31 PM
Registered User
 
Join Date: May 2014
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Imar,
First I am new to web programming, but not to programming, so bear with me a bit. I am trying to build a web site for my organization. One thing I am trying to do is build a page that has a dropdown list linked to a listview to show instructions. That info stays static for the duration. Below that I have another listview that displays a mailing address. I want to incrementally walk through the addresses. Below that I want to have a Grid Control that displays a list of people with related info that is tied to the address.

The problem comes from trying to access the various controls from within the ItemTemplate. I can easily find the controls within the Insert and Update templates using FindControl, but I can't seem to figure out how to find the bound controls within the ItemTemplate.

It is frustrating to figure out how to do a work-around when I shouldn't need to... or... Help, Please

By the way, I like your beginning ASP.Nety 4.5, and it is really helpful, even though I'm still on XP. I am reluctant to changet to Windows 7 or 8 because I don't want to spend$$ to upgrand my Delphi (Code Gear) software.

Last edited by Bruce Weber; May 16th, 2014 at 06:38 PM..
 
Old May 16th, 2014, 06:34 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Can you post the relevant code along with a description about what you expect to access from where?

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old May 16th, 2014, 06:36 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

BTW, searching Google for gridview ItemTemplate FindControl brings up lots of useful stuff, including:

http://stackoverflow.com/questions/6...ld-of-gridview
http://stackoverflow.com/questions/1...late-confusion

Did you see these, and did they help?

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old May 16th, 2014, 06:50 PM
Registered User
 
Join Date: May 2014
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Code:
  Program<asp:DropDownList ID="ProgramDropDownList" runat="server" 
      AutoPostBack="True" DataSourceID="SqlDataSourceProgams" 
      DataTextField="Description" DataValueField="Id"></asp:DropDownList>
   <asp:SqlDataSource ID="SqlDataSourceProgams" runat="server" 
      ConnectionString="<%$ ConnectionStrings:GOPConnectionString %>" 
      SelectCommand="GetCallPrograms" SelectCommandType="StoredProcedure">
   </asp:SqlDataSource><br />
   <asp:ListView ID="ScriptListView" runat="server" 
      DataSourceID="SqlDataSourceCallScripts">
      <ItemTemplate>
         <td runat="server" style="">
         <table>
            <tr>
               <td align="center">LiveScript</td>
               <td align="center">Answering Machine Script</td>
            </tr>
            <tr>
               <td>
                  <asp:TextBox ID="LiveScriptTextBox" runat="server" 
                     ReadOnly="true" 
                     Width="425"
                     Height="150"
                     TextMode="MultiLine" 
                     Text='<%# Eval("LiveScript") %>' />
               </td>
               <td>
                  <asp:TextBox ID="TextBox1" runat="server"
                     ReadOnly="true" 
                     Width="425"
                     Height="150"
                     TextMode="MultiLine"
                     Text='<%# Eval("AnsweringMachineScript") %>' />
               </td>
            </tr>
         </table>
         </td>
      </ItemTemplate>
      <LayoutTemplate>
         <table runat="server" border="0" style="">
            <tr ID="itemPlaceholderContainer" runat="server">
               <td ID="itemPlaceholder" runat="server">
               </td>
            </tr>
         </table>
         <div style="">
         </div>
      </LayoutTemplate>
   </asp:ListView>
   <asp:SqlDataSource ID="SqlDataSourceCallScripts" runat="server" 
      ConnectionString="<%$ ConnectionStrings:GOPConnectionString %>" 
      SelectCommand="GetProgramCallScripts" SelectCommandType="StoredProcedure">
      <SelectParameters>
         <asp:ControlParameter ControlID="ProgramDropDownList" Name="Id" 
            PropertyName="SelectedValue" Type="Int32" />
      </SelectParameters>
   </asp:SqlDataSource>
<!---------------->
      <asp:DropDownList ID="PlaceDropDownList" runat="server" 
         DataSourceID="SqlDataSourcetest" DataTextField="Place" 
         DataValueField="Place" ontextchanged="PlaceDropDownList_TextChanged" 
         ondatabound="PlaceDropDownList_DataBound">
      </asp:DropDownList>
      <asp:Label ID="PlaceLabel1" runat="server" Text="0"></asp:Label>
      <asp:SqlDataSource ID="SqlDataSourcetest" runat="server" 
         ConnectionString="<%$ ConnectionStrings:GOPConnectionString %>" 
         SelectCommand="GetCallPlace" SelectCommandType="StoredProcedure">
         <SelectParameters>
            <asp:ControlParameter ControlID="ProgramDropDownList" Name="Program" 
               PropertyName="SelectedValue" Type="Int32" />
         </SelectParameters>
      </asp:SqlDataSource>
   <asp:ListView ID="AddressListView" runat="server" 
      DataSourceID="SqlDataSourceCallPlace" onprerender="AddressListView_PreRender">
      <ItemTemplate>
         <span style="">
            Remaining: &nbsp;<asp:Label ID="RemainingLabel" Font-Bold="false" runat="server" Text='<%# Eval("Remaining") %>' />
            Address: &nbsp;<asp:Label ID="AddressLabel" runat="server" Font-Bold="false" Text='<%# Eval("Address") %>' />
            <br /> 
            <asp:Button ID="Button2" UseSubmitBehavior="true" OnClick="Button2_Click" runat="server" Text="Button2" /> 
         </span>
      </ItemTemplate>
      <LayoutTemplate>
         <div ID="itemPlaceholderContainer" runat="server" style="">
            <span runat="server" id="itemPlaceholder" />
         </div>
         <div style="">
         </div>
      </LayoutTemplate>
   </asp:ListView>
   <asp:SqlDataSource ID="SqlDataSourceCallPlace" runat="server" 
      ConnectionString="<%$ ConnectionStrings:GOPConnectionString %>" 
      SelectCommand="GetCallPlace2" SelectCommandType="StoredProcedure">
      <SelectParameters>
         <asp:ControlParameter ControlID="PlaceDropDownList" Name="Place" 
            PropertyName="SelectedValue" Type="Int32" />
      </SelectParameters>
   </asp:SqlDataSource>
   <asp:TextBox ID="PlaceTextBox" AutoPostBack="true" runat="server" Text="0"></asp:TextBox>
   <asp:Button ID="Button1"  runat="server" Text="Button1" 
         onclick="Button1_Click" UseSubmitBehavior="False" /> 
   <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
      DataSourceID="SqlDataSourcePlaceVoters" >
      <Columns>
         <asp:BoundField DataField="Name" HeaderText="Name" ReadOnly="True" 
            SortExpression="Name" />
         <asp:BoundField DataField="Gender" HeaderText="Gender" 
            SortExpression="Gender" />
         <asp:BoundField DataField="Age" HeaderText="Age" ReadOnly="True" 
            SortExpression="Age" />
         <asp:BoundField DataField="Party" HeaderText="Party" SortExpression="Party" />
         <asp:BoundField DataField="RegDate" HeaderText="RegDate" ReadOnly="True" 
            SortExpression="RegDate" />
         <asp:BoundField DataField="Phone" HeaderText="Phone" SortExpression="Phone" />
         <asp:BoundField DataField="GeneralHistory" HeaderText="GeneralHistory" 
            SortExpression="GeneralHistory" />
         <asp:BoundField DataField="PrimaryHistory" HeaderText="PrimaryHistory" 
            SortExpression="PrimaryHistory" />
      </Columns>
   </asp:GridView>
   <asp:SqlDataSource ID="SqlDataSourcePlaceVoters" runat="server" 
      ConnectionString="<%$ ConnectionStrings:GOPConnectionString %>" 
      SelectCommand="GetPlaceVoters" SelectCommandType="StoredProcedure">
      <SelectParameters>
         <asp:ControlParameter ControlID="PlaceTextBox" DefaultValue="0" Name="PlaceId" 
            PropertyName="Text" Type="Int32" />
      </SelectParameters>
   </asp:SqlDataSource>
--------------------------------
What you see here is attempts of work arounds.. probably irrelevant at this point
__________________________________________
public partial class PC_Events : System.Web.UI.Page
{
   int NextPlace = 0;
   int LastPlace = 0;
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void GridView1_Load(object sender, EventArgs e)
    {
     
    }

    protected void AddressListView_PreRender(object sender, EventArgs e)
    {
       
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
       int x = Convert.ToInt32(PlaceTextBox.Text);
       x = x + 1;
       PlaceTextBox.Text = x.ToString();
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
       int x = Convert.ToInt32(PlaceLabel1.Text);
       x = x + 1;
       PlaceLabel1.Text = x.ToString();
    }

    protected void PlaceDropDownList_TextChanged(object sender, EventArgs e)
    {
      
    }
    protected void PlaceDropDownList_DataBound(object sender, EventArgs e)
    {
       PlaceLabel1.Text = PlaceDropDownList.Text;
    }
 
Old May 17th, 2014, 07:17 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Did you take a look at the first link I posted? It seems that handling RowDataBound (or maybe RowCreated) would do the trick. Inside of that handler, you need to look at the RowType and see if it's a DataControlRowType.DataRow before you try to access the controls using FindControl.

Hope this helps,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!





Similar Threads
Thread Thread Starter Forum Replies Last Post
Finding root of tree: the context item is undefined WilliamYou XSLT 2 December 12th, 2012 04:18 PM
Gridview Item Template dotnetDeveloper ASP.NET 3.5 Basics 0 February 15th, 2010 02:01 PM
Item Template Query rsm42 ASP.NET 1.0 and 1.1 Basics 0 December 22nd, 2006 07:55 AM
edit item template in a datagrid akshay144 ASP.NET 1.0 and 1.1 Professional 0 October 25th, 2006 05:05 AM
Finding the first item EstherMStrom XSLT 9 September 30th, 2004 11:17 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.