Solved

Accessing nested control data

Posted on 2008-10-07
5
593 Views
Last Modified: 2013-12-17
Hi everyone,

I am creating an AJAX enabled ASP.NET page which has a set of options you complete in order to access the final data.  

HYPOTHETICAL STEPS:
1. Enter study number (press enter) - this should load up the relevant protocol ID's from the database
2. Enter the country (used as a filter when button is pressed) for the relevant protocol
3. Press the "get task" button - this looks up a task number for the protocol stored in the database for the country being searched. and loads some data into a gridview after.

I am unsure how to progress now, as I dont know how I can access the value of the "tbCountry" (baring in mind there could be many depending on the number of protocols stored for each study) and use it on the "get task" button click.

Could anyone give me some ideas??

Thanks in advance for your help..


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" EnableViewState="False" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
 
 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title>Recipient Lists For Reports</title>

    <link href="Styles/Site.css" rel="stylesheet" type="text/css" />

</head>

<body>

    <form id="form1" runat="server">

        &nbsp;<cc1:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server"></cc1:ToolkitScriptManager>

        <div style="width:100%;">

            <asp:UpdatePanel ID="UpdatePanel2" runat="server">

                <ContentTemplate>

                    <asp:RadioButtonList ID="RadioButtonList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged" RepeatDirection="Horizontal">

                        <asp:ListItem Value="0">Study Number</asp:ListItem>

                        <asp:ListItem Value="1">Protocol ID</asp:ListItem>

                    </asp:RadioButtonList>

                    <asp:MultiView ID="MultiView1" runat="server">

                        <asp:View ID="vwStudyNum" runat="server">

                            <asp:TextBox ID="tbStudyNumber1" runat="server" OnTextChanged="tbStudyNumber1_TextChanged"></asp:TextBox>

                            <asp:Label ID="Label1" runat="server" Text="Enter study number"></asp:Label>

                            <br /><br />

                            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:CDSConnectionString %>"

                                SelectCommand="SELECT [CPPS_Site], [Study_ID], [Protocol_Name], [CPPS_Job] FROM [Studies] WHERE ([CPPS_Job] = @CPPS_Job)" OnSelected="SqlDataSource1_Selected">

                                <SelectParameters>

                                    <asp:Parameter Name="CPPS_Job" Type="Int32" />

                                </SelectParameters>

                            </asp:SqlDataSource>

                            <asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource1" CellPadding="4" ForeColor="#333333">

                                <ItemTemplate>

                                    <asp:Label ID="Label2" runat="server" Text="Protocol ID: "></asp:Label>

                                    <asp:Label ID="lblProtocolID1" runat="server" Text='<%# Bind("Protocol_Name") %>' Width="200px"></asp:Label>

                                    <asp:TextBox ID="tbCountry" runat="server" CssClass="ital" OnTextChanged="tbCountry_TextChanged"></asp:TextBox>

                                    <asp:Button ID="btnGetTaskNumbers" runat="server" Text="Get task" OnClick="btnGetTaskNumbers_Click" />&nbsp;

                                    <cc1:TextBoxWatermarkExtender ID="TextBoxWatermarkExtender1" runat="server" TargetControlID="tbCountry" WatermarkText="enter country name">

                                    </cc1:TextBoxWatermarkExtender>

                                </ItemTemplate>

                                <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />

                                <AlternatingItemStyle BackColor="#405366" ForeColor="White" />

                                <ItemStyle BackColor="#99A3AD" ForeColor="White" />

                                <SelectedItemStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />

                                <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />

                            </asp:DataList>

                        </asp:View>

                        <asp:View ID="vwProtocolNum" runat="server">

                            <asp:TextBox ID="tbProtocolId2" runat="server" ReadOnly="True"></asp:TextBox>

                            <asp:Label ID="Label4" runat="server" Text="Enter protocol ID"></asp:Label>

                        </asp:View>

                    </asp:MultiView>

                </ContentTemplate>

            </asp:UpdatePanel>

            <br />

            <br />

            <br />

            <asp:UpdatePanel ID="UpdatePanel1" runat="server">

                <ContentTemplate>

                    <asp:GridView ID="grdvUserInfo" runat="server"></asp:GridView>

                </ContentTemplate>

            </asp:UpdatePanel>

        </div>

    </form>

</body>

</html>

Open in new window

screen1.JPG
0
Comment
Question by:Artform04
  • 3
  • 2
5 Comments
 
LVL 6

Expert Comment

by:onemancat
ID: 22660037
I've always found it clumsy to work with web controls within a DataList.  You could certainly iterate through DataList.Controls() hierarchy and find what you need, but I actually just go old school in this scenario and examine the Request.Form values.  
I just make HTML <input type=submit name=...> buttons where the names are GetTask_25643 and GetTask_IMP25643, and the country textboxes as Country_25643 and Country_IMP25643.  Then I iterate through Request.Form when the page is posted back (IsPostBack) and then generally re-direct off to the task screen.  If you're doing a redirect after the user hits the button, there's not really a need for a web control as the state of the form does not need to be persisted.
Anyways, I'm sure someone will shout me down here for using HTML controls and not web controls, but practically speaking, I consistently find them much faster to code in this kind of scenario, and I've faced this kind of problem many, many, many times.
If you want me to elaborate on this approach, let me know...
0
 
LVL 1

Author Comment

by:Artform04
ID: 22660328
Thanks 'onemancat' thats a good idea, but I would rather avoid doing it that way if possible.

I was thinking perhaps using GET values and querystrings now as I have just created an action in the code behind file which can append the protocol_id to the PostBackUrl of the linkButton.

But I am still unsure of how I will get the country value when clicking the linkButton...!

PS i have replaced the commandButton with the LinkButton if you were looking for it in the previous HTML. it is called "lnkGetTask".
    protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)

    {

        LinkButton tmp = (LinkButton) e.Item.FindControl("lnkGetTask");

        Label lbltmp = (Label) e.Item.FindControl("lblProtocolID1");

        tmp.PostBackUrl = "~/Default.aspx?prot=" + lbltmp.Text;

    }

Open in new window

0
 
LVL 6

Accepted Solution

by:
onemancat earned 450 total points
ID: 22660621
I imagine that you could set the Tag property of the get task button to be the country textbox that is associated with it.
  Or, you could just dive into the data list, iterate through all the DataListItem items, and then iterate through all the Controls collection within each item--when you find the button whose Text property is equal to the protocol ID in the query string, then you are in the right item, and then you just need to find the tbCountry textbox control that is also in the Controls collection for that DataListItem.
0
 
LVL 1

Author Comment

by:Artform04
ID: 22667023
Whilst that is a fairly good idea actually, the problem is knowing which button has fired...

if you put a handler on the button in the itemTemplate for the datalist, you cannot know which one it is... there are no "e" or "sender" args.  I also tried to put some code in the DataList1_ItemCommand, but that doesn't seem to get fired on the buttons click.  

Any more suggestions??

The code i was trying to use for the DataList1_ItemCommand event was as follows...
        //get handles on the controls

        LinkButton tmp = (LinkButton)e.Item.FindControl("lnkGetTask");

        TextBox tbCountry = (TextBox)e.Item.FindControl("tbCountry");

        //create the link for the linkButton

        tmp.PostBackUrl =  tmp.PostBackUrl + "&c=" + tbCountry.Text;

Open in new window

0
 
LVL 1

Author Comment

by:Artform04
ID: 22667516
I ended up using the querystring value on the postback when iterating through the datalist controls (searching for the protocol id which i stored in the tooltip as there is no "tag"...) bit of a bodge but it works fine.

Thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

920 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

15 Experts available now in Live!

Get 1:1 Help Now