reference to controls in template item

Discussion in 'ASP .Net' started by Dariusz Tomon, Apr 3, 2007.

  1. Hello,

    The situation is like follow:

    I've got GridView1 correlated with DetailsView1 (it's normal scenario). In
    DetailView1 I've got CRUD operations enabled. For create and edit opeartion
    I've 2 ItemTemplates. One of them is DropDownList2 (with AutoPostBack
    enabled) and 2-nd one is GridView2 (dependent on DropDownList2). All I want
    is to have GridView2 updated when I change option in DropDownList2.
    I thought that I can do that in code behind (Selected event of
    DropDownList2) by changing SqlDataSource which is bound to GridView2. But I
    don't know how to refer to these "embedded" controls.

    Best Regards

    D.T.
     
    Dariusz Tomon, Apr 3, 2007
    #1
    1. Advertising

  2. Czesc Darek,

    What about FindControl() method?:

    DropDownList dropDownList = (DropDownList)
    DetailView1.FindControl("DropDownList1")
    string selectedValue = dropDownListSelectedValue;

    or use asp:ControlParameter in sqldatasource control. The only problem with
    the second approach (in this case) is you have to provide container(s)
    control id as well:

    <asp:SqlDataSource runat="server" ID="SqlDataSource1">
    <SelectParameters>
    <asp:ControlParameter ControlID="DetailsView1$DropDwonList1"
    PropertyName="SelectedValue"/>
    </SelectParameters>
    </asp:SqlDataSource>

    Hope this helps
    --
    Milosz


    "Dariusz Tomon" wrote:

    > Hello,
    >
    > The situation is like follow:
    >
    > I've got GridView1 correlated with DetailsView1 (it's normal scenario). In
    > DetailView1 I've got CRUD operations enabled. For create and edit opeartion
    > I've 2 ItemTemplates. One of them is DropDownList2 (with AutoPostBack
    > enabled) and 2-nd one is GridView2 (dependent on DropDownList2). All I want
    > is to have GridView2 updated when I change option in DropDownList2.
    > I thought that I can do that in code behind (Selected event of
    > DropDownList2) by changing SqlDataSource which is bound to GridView2. But I
    > don't know how to refer to these "embedded" controls.
    >
    > Best Regards
    >
    > D.T.
    >
    >
    >
     
    =?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=, Apr 3, 2007
    #2
    1. Advertising

  3. Thank you for your advice.
    Actually I solved the problem very alike :)

    protected void ddl_DeviceType_SelectedIndexChanged(object sender, EventArgs
    e)

    {

    string a = DetailsView1.FindControl("ddl_DeviceType").ID.ToString();

    DropDownList ddl = new DropDownList();

    ddl = (DropDownList)DetailsView1.FindControl("ddl_DeviceType");

    a = ddl.SelectedItem.ToString();

    }



    Best Regards



    D.T.


    "Milosz Skalecki [MCAD]" <> wrote in message
    news:...
    > Czesc Darek,
    >
    > What about FindControl() method?:
    >
    > DropDownList dropDownList = (DropDownList)
    > DetailView1.FindControl("DropDownList1")
    > string selectedValue = dropDownListSelectedValue;
    >
    > or use asp:ControlParameter in sqldatasource control. The only problem
    > with
    > the second approach (in this case) is you have to provide container(s)
    > control id as well:
    >
    > <asp:SqlDataSource runat="server" ID="SqlDataSource1">
    > <SelectParameters>
    > <asp:ControlParameter ControlID="DetailsView1$DropDwonList1"
    > PropertyName="SelectedValue"/>
    > </SelectParameters>
    > </asp:SqlDataSource>
    >
    > Hope this helps
    > --
    > Milosz
    >
    >
    > "Dariusz Tomon" wrote:
    >
    >> Hello,
    >>
    >> The situation is like follow:
    >>
    >> I've got GridView1 correlated with DetailsView1 (it's normal scenario).
    >> In
    >> DetailView1 I've got CRUD operations enabled. For create and edit
    >> opeartion
    >> I've 2 ItemTemplates. One of them is DropDownList2 (with AutoPostBack
    >> enabled) and 2-nd one is GridView2 (dependent on DropDownList2). All I
    >> want
    >> is to have GridView2 updated when I change option in DropDownList2.
    >> I thought that I can do that in code behind (Selected event of
    >> DropDownList2) by changing SqlDataSource which is bound to GridView2. But
    >> I
    >> don't know how to refer to these "embedded" controls.
    >>
    >> Best Regards
    >>
    >> D.T.
    >>
    >>
    >>
     
    Dariusz Tomon, Apr 3, 2007
    #3
  4. Czesc Milosz,

    Zauwazylem adres wp.pl wiec mozna po polsku chociaz lista jest eng (ale jak
    sie wczytaja to pewnie zatrybia :)
    Ok w kazdym razie taka sprawa:

    Mam nested gridview w DetailsView ktora pozwala mi wybrac konkretny rekord.
    Ale jak robie sortowanie po kolumnie to wywal mi taki blad:

    **********************************************

    Databinding methods such as Eval(), XPath(), and Bind() can only be used in
    the context of a databound control.
    Description: An unhandled exception occurred during the execution of the
    current web request. Please review the stack trace for more information
    about the error and where it originated in the code.

    Exception Details: System.InvalidOperationException: Databinding methods
    such as Eval(), XPath(), and Bind() can only be used in the context of a
    databound control.

    Source Error:


    [No relevant source lines]


    Source File: c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET
    Files\zasobyit2\2486c2d0\3d86e288\App_Web_wslajwmy.0.cs Line: 0

    Stack Trace:


    [InvalidOperationException: Databinding methods such as Eval(), XPath(), and
    Bind() can only be used in the context of a databound control.]
    System.Web.UI.Page.GetDataItem() +1996914
    ASP.infohardware_aspx.__DataBinding__control127(Object sender, EventArgs
    e) in c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET
    Files\zasobyit2\2486c2d0\3d86e288\App_Web_wslajwmy.0.cs:0
    System.Web.UI.Control.OnDataBinding(EventArgs e) +99
    System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable
    data) +51
    System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments,
    DataSourceViewSelectCallback callback) +29
    System.Web.UI.WebControls.DataBoundControl.PerformSelect() +149
    System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +70
    System.Web.UI.WebControls.GridView.DataBind() +4
    System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +82
    System.Web.UI.WebControls.GridView.OnPreRender(EventArgs e) +24
    System.Web.UI.Control.PreRenderRecursiveInternal() +77
    System.Web.UI.Control.PreRenderRecursiveInternal() +161
    System.Web.UI.Control.PreRenderRecursiveInternal() +161
    System.Web.UI.Control.PreRenderRecursiveInternal() +161
    System.Web.UI.Control.PreRenderRecursiveInternal() +161
    System.Web.UI.Control.PreRenderRecursiveInternal() +161
    System.Web.UI.Control.PreRenderRecursiveInternal() +161
    System.Web.UI.Control.PreRenderRecursiveInternal() +161
    System.Web.UI.Control.PreRenderRecursiveInternal() +161
    System.Web.UI.Page.ProcessRequestMain(Boolean
    includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1360

    --------------------------------------------------------------------------------
    Version Information: Microsoft .NET Framework Version:2.0.50727.42; ASP.NET
    Version:2.0.50727.42


    ******************************************

    Dodam ze reprezentacja tego pola w templateitem ale w widoku "normalnym" bez
    edycji jest labelka.

    Masz moze jakis pomysl jak to obejsc? Idealnie chcialbym uzyskac
    wspolzaleznosc pomiedzy 2 osadzonymi gridview tak aby wybranie rekordu z
    jedego powodowalo zawezenie wynikow 2-giego. Oczywiscie te 2 kontrolgi sa
    embedded.

    Pozdrawiam

    Darek


    "Milosz Skalecki [MCAD]" <> wrote in message
    news:...
    > Czesc Darek,
    >
    > What about FindControl() method?:
    >
    > DropDownList dropDownList = (DropDownList)
    > DetailView1.FindControl("DropDownList1")
    > string selectedValue = dropDownListSelectedValue;
    >
    > or use asp:ControlParameter in sqldatasource control. The only problem
    > with
    > the second approach (in this case) is you have to provide container(s)
    > control id as well:
    >
    > <asp:SqlDataSource runat="server" ID="SqlDataSource1">
    > <SelectParameters>
    > <asp:ControlParameter ControlID="DetailsView1$DropDwonList1"
    > PropertyName="SelectedValue"/>
    > </SelectParameters>
    > </asp:SqlDataSource>
    >
    > Hope this helps
    > --
    > Milosz
    >
    >
    > "Dariusz Tomon" wrote:
    >
    >> Hello,
    >>
    >> The situation is like follow:
    >>
    >> I've got GridView1 correlated with DetailsView1 (it's normal scenario).
    >> In
    >> DetailView1 I've got CRUD operations enabled. For create and edit
    >> opeartion
    >> I've 2 ItemTemplates. One of them is DropDownList2 (with AutoPostBack
    >> enabled) and 2-nd one is GridView2 (dependent on DropDownList2). All I
    >> want
    >> is to have GridView2 updated when I change option in DropDownList2.
    >> I thought that I can do that in code behind (Selected event of
    >> DropDownList2) by changing SqlDataSource which is bound to GridView2. But
    >> I
    >> don't know how to refer to these "embedded" controls.
    >>
    >> Best Regards
    >>
    >> D.T.
    >>
    >>
    >>
     
    Dariusz Tomon, Apr 3, 2007
    #4
  5. Czolem Darek,

    Musze przyznac ze troche zamieszales pomimo ze dziala :)
    1. nie musisz inicjalizowac zmiennej poprzez tworzenie nowej instancji
    dropdownlist skoro ona juz istnieje i zostanie zwrocona z metody FindControl()
    2. wiekszosc zdazen przekazuje referencje do obiektu ktorego zdazenie
    dotyczy z paramnetrze sender (w tym przypadku bedzie to referencja do
    3. podobnie jak w pkt.1, nie musisz inicjalizowac stringa ID controlki

    Powinno byc mniej wiecej tak:

    protected void ddl_DeviceType_SelectedIndexChanged(object sender, EventArgs e)
    {
    DropDownList ddl = (DropDownList) sender;
    string value = ddl.SelectedValue;
    }

    albo

    protected void ddl_DeviceType_SelectedIndexChanged(object sender, EventArgs e)
    {
    string a;
    DropDownList ddl = (DropDownList)
    DetailsView1.FindControl("ddl_DeviceType");
    a = ddl.SelectedValue.ToString();
    }

    Widze ze cos Ci jeszcze tam nie dziala (nastepny post) wiec moze bedzie
    szybciej jak podeslesz mi kod na maila.

    Uszanowanie
    --
    Milosz


    "Dariusz Tomon" wrote:

    > Thank you for your advice.
    > Actually I solved the problem very alike :)
    >
    > protected void ddl_DeviceType_SelectedIndexChanged(object sender, EventArgs
    > e)
    >
    > {
    >
    > string a = DetailsView1.FindControl("ddl_DeviceType").ID.ToString();
    >
    > DropDownList ddl = new DropDownList();
    >
    > ddl = (DropDownList)DetailsView1.FindControl("ddl_DeviceType");
    >
    > a = ddl.SelectedItem.ToString();
    >
    > }
    >
    >
    >
    > Best Regards
    >
    >
    >
    > D.T.
    >
    >
    > "Milosz Skalecki [MCAD]" <> wrote in message
    > news:...
    > > Czesc Darek,
    > >
    > > What about FindControl() method?:
    > >
    > > DropDownList dropDownList = (DropDownList)
    > > DetailView1.FindControl("DropDownList1")
    > > string selectedValue = dropDownListSelectedValue;
    > >
    > > or use asp:ControlParameter in sqldatasource control. The only problem
    > > with
    > > the second approach (in this case) is you have to provide container(s)
    > > control id as well:
    > >
    > > <asp:SqlDataSource runat="server" ID="SqlDataSource1">
    > > <SelectParameters>
    > > <asp:ControlParameter ControlID="DetailsView1$DropDwonList1"
    > > PropertyName="SelectedValue"/>
    > > </SelectParameters>
    > > </asp:SqlDataSource>
    > >
    > > Hope this helps
    > > --
    > > Milosz
    > >
    > >
    > > "Dariusz Tomon" wrote:
    > >
    > >> Hello,
    > >>
    > >> The situation is like follow:
    > >>
    > >> I've got GridView1 correlated with DetailsView1 (it's normal scenario).
    > >> In
    > >> DetailView1 I've got CRUD operations enabled. For create and edit
    > >> opeartion
    > >> I've 2 ItemTemplates. One of them is DropDownList2 (with AutoPostBack
    > >> enabled) and 2-nd one is GridView2 (dependent on DropDownList2). All I
    > >> want
    > >> is to have GridView2 updated when I change option in DropDownList2.
    > >> I thought that I can do that in code behind (Selected event of
    > >> DropDownList2) by changing SqlDataSource which is bound to GridView2. But
    > >> I
    > >> don't know how to refer to these "embedded" controls.
    > >>
    > >> Best Regards
    > >>
    > >> D.T.
    > >>
    > >>
    > >>

    >
    >
    >
     
    =?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=, Apr 6, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?QWJoaXNoZWs=?=
    Replies:
    1
    Views:
    1,378
    =?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=
    Mar 24, 2006
  2. =?Utf-8?B?SmVyb2QgSGF0bGV5?=

    GridView add multiple controls to edit item template

    =?Utf-8?B?SmVyb2QgSGF0bGV5?=, Oct 7, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    1,072
    =?Utf-8?B?SmVyb2QgSGF0bGV5?=
    Oct 8, 2006
  3. mrstephengross
    Replies:
    2
    Views:
    417
    mrstephengross
    Sep 7, 2005
  4. Vijaya

    Aligning the controls in the item template of the datagrid

    Vijaya, Dec 15, 2003, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    204
    Kelly Leahy
    Dec 18, 2003
  5. Scott M.

    Handling Events Of DataGrid Template Item Controls - UNANSWERED

    Scott M., Nov 15, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    5
    Views:
    211
    Steven Cheng[MSFT]
    Nov 16, 2004
Loading...

Share This Page