Solved

Accessing nested control data

Posted on 2008-10-07
5
590 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

771 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

14 Experts available now in Live!

Get 1:1 Help Now