Web Venture 101


Finding controls inside ItemTemplates inside a FormView inside a LoginView

, posted: 26-Apr-2006 21:41

Consider this:

<asp:LoginView ID="LoginView1" runat="server">

  <LoggedInTemplate>

    <asp:FormView ID="MyFormView" runat="server" DataSourceID="SqlDataSource1"

DefaultMode="readOnly" DataKeyNames="BlahId">

      <ItemTemplate>

...
      </
ItemTemplate>

      <EditItemTemplate>

        <asp:ListBox ID="ListBox1" runat="server" DataSourceID="SqlDataSource2" DataTextField="Name"

DataValueField="BlahBlahId" Rows="10" AutoPostBack="True" >

        </asp:ListBox>

        <asp:Button ID="UpdateButton" runat="server" Text="Save" CommandName="Update" />

      </EditItemTemplate>

    </asp:FormView>

  </LoggedInTemplate>

  <AnonymousTemplate>

...
  </AnonymousTemplate>

</asp:LoginView>


A couple of issues here.

You can't declaratively use ListBox1 as a control parameter in dataSource for another control on the page, or one of the updateParameters for a dataSource. So I did it programatically.

However programatically you can't simply use LoginView1.FindControl("ListBox1") to get a reference to the listbox (to get its SelectedValue for example, to set as the update or select parameter of another dataSource).

You need to bury through the controls hierarchy by index it would seem, for example:


(ListBox)LoginView1.Controls[0].Controls[1].FindControl("ListBox1");

I'm sure there must be a better way.  Anyideas?



Other related posts:
Keys, Values, and NewValues when Inserting, Updating, Deleting with GridView, DetailsView, FormView and ObjectDataSource
Creating Typed Datasets - member names cannot be the same as their enclosing type
Implicit conversion from data type sql_variant to uniqueidentifier is not allowed. Use the CONVERT function to run this query






Comment by Paul, on 13-Jul-2006 15:55

Did you manage to sort out how to drill down into the LoginView control? I have a hyperlink control within the AnonymousTemplate and in my code behind set the hyperlink to an email address read from the web.config but at the moment can't reach the control..


Comment by Shaun Latham, on 12-Sep-2006 22:36

I use this method when referencing a control within another control, in this case the Login control:



(C#)

HyperLink hlMyOrders = (HyperLink)LoginView2.FindControl("hlMyOrders");

hlMyOrders.Visible = true;



Regards

Shaun.


Comment by Lars Johansen, on 27-Jan-2007 02:49

This works for settings focus on the UserName textbox inside the Loginview. Thx. C# protected void Page_PreRender(object sender, EventArgs e) { TextBox Mycon = (TextBox) LoginView1.Controls[0].Controls[1].FindControl("UserName"); SetFocus(Mycon); } Regards Lars


Add a comment

Please note: comments that are inappropriate or promotional in nature will be deleted. E-mail addresses are not displayed, but you must enter a valid e-mail address to confirm your comments.

Are you a registered Geekzone user? Login to have the fields below automatically filled in for you and to enable links in comments. If you have (or qualify to have) a Geekzone Blog then your comment will be automatically confirmed and shown in this blog post.

Your name:

Your e-mail:

Your webpage:

ptorrsmith's profile

Peter Torr Smith
Wellington
New Zealand


I've been working on a web venture idea for the last year or so, so have now resigned from my job to go at it full-time.

Here's how I go, with business and technology challenges!!