Link to home
Start Free TrialLog in
Avatar of Artform04
Artform04

asked on

Accessing nested control data

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
Avatar of onemancat
onemancat
Flag of United States of America image

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...
Avatar of Artform04
Artform04

ASKER

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

ASKER CERTIFIED SOLUTION
Avatar of onemancat
onemancat
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

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