0

The ID is inside the ItemTemplate and it cannot be found in the code file.

<form id="form1" runat="server">
    <asp:SqlDataSource ID="searchresults" runat="server"
                       ConnectionString='<%$ ConnectionStrings:AgileDatabaseConnection %>'
                       SelectCommand=" SELECT [userID], [userName], [firstName],[lastName],[password], [email] FROM [Users] WHERE ([email] LIKE '%' + @email + '%')">
        <selectparameters>
            <asp:querystringparameter querystringfield="searchquery" name="email" type="String"></asp:querystringparameter>
        </selectparameters>
    </asp:SqlDataSource>

    <asp:ListView ID="displayitems" runat="server" DataSourceID="searchresults">
        <itemtemplate>
            <asp:label runat="server" associatedcontrolid="projectOwner" cssclass="col-md-2 control-label">Project Owner:</asp:label>
            <div class="col-md-10">
                <asp:TextBox runat="server" ID="ProjectOwner" CssClass="form-control" /><br />
                <asp:label runat="server" associatedcontrolid="projectOwner" cssclass="col-md-2 control-label">Scrum Master:</asp:label>
                <div class="col-md-10">
                    <asp:SqlDataSource ID="ScrumMaster" runat="server" ConnectionString='<%$ ConnectionStrings:AgileDatabaseConnection %>' SelectCommand="SELECT userName FROM [Users]"></asp:SqlDataSource>
                    <asp:dropdownlist runat="server" id="usertype" DataSourceID="ScrumMaster" DataTextField="userName"></asp:dropdownlist><br />
                </div>
            </div>

            <asp:label runat="server" cssclass="col-md-2 control-label">Email:</asp:label>
            <asp:Label Text='<%#Eval("email") %>' runat="server" ID="emaillabel" /><br />
            <asp:label runat="server" cssclass="col-md-2 control-label">UserName:</asp:label>
            <asp:Label Text='<%#Eval("userName") %>' runat="server" ID="username" /><br />

            <asp:label runat="server" cssclass="col-md-2 control-label">UserID:</asp:label>
            <asp:Label runat="server" Text='<%#Eval("userID") %>' ID="UserID" CssClass="form-control" />
            <br />
            <div class="actions"></div>

            <asp:Button Text="Add" runat="server" class="btn pull-right" ID="uploadbutton" OnClick="add_Click"></asp:Button>
            </div>
        </itemtemplate>
    </asp:ListView>
    <emptydatatemplate>

        <span>No users match  <asp:Label Text='' runat="server" ID="email" /> .</span>

    </emptydatatemplate>
</form>

C# says it can't find 'userName'. Here's my backend code:

string ownerName = ProjectOwner.Text;
string IDuser = username.Text;
string IDdata = Session["userID"].ToString();

How do I get the userName value through?

Ortund
  • 8,095
  • 18
  • 71
  • 139
  • the other fields are well resolved? – McNets Nov 03 '16 at 15:35
  • there is a second SqlDataSource inside ListView – McNets Nov 03 '16 at 15:38
  • Hi sorry i didn't post that the projectowner isn't resolved either. – Ryan McSorley Nov 03 '16 at 15:39
  • 1
    The error is about Text='<%#Eval("userName") %>', not about ID=username – McNets Nov 03 '16 at 15:40
  • the name 'ProjectOwner' does not exist in the current context the name 'username' does not exist in the current context – Ryan McSorley Nov 03 '16 at 15:42
  • Inside a list view there are a lot of ProjectOwners and UserNames controls, you cannot use it as an unique control identifier, you should iterate through ListView items at server side. – McNets Nov 03 '16 at 15:48
  • See, we moved the code on the front end around, which did resolve the c# but then there was issues with the item template on the front end and when I resolved the itemtemplate issue on the front end the projectowner and username isn't resolved in the back end. – Ryan McSorley Nov 03 '16 at 15:55
  • First, use only one DataSource, then take a look at this post, you'll find some help. http://stackoverflow.com/questions/9208528/how-to-find-control-in-itemtemplate-of-the-listview-from-code-behind-of-the-user – McNets Nov 03 '16 at 16:02
  • and another example from MSDN. https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listview.itemdatabound.aspx – McNets Nov 03 '16 at 16:03
  • Is this code behind inside the `ListView`'s `ItemDataBound`? – Felipe Deguchi Nov 03 '16 at 16:22

1 Answers1

0

The problem is that ListView can return more then one item, so code behind doesn't know about which of the itens controls (username and ProjectOwner) are you talking about.

If you want to do the same thing for EACH item, you can do like this:

protected void displayitems_ItemDataBound(object sender, ListViewItemEventArgs e)
{
    if (e.Item.ItemType == ListViewItemType.DataItem)
    {
        string ownerName = ((TextBox)e.Item.FindControl("ProjectOwner")).Text;
        string IDuser = ((Label)e.Item.FindControl("username").Text;
        string IDdata = Session["userID"].ToString();
    }
}
Felipe Deguchi
  • 586
  • 1
  • 7
  • 25
  • We are now getting errors under the FindControl 'System.Web.UI.WebControls.ListItemViewEventArgs' does not contain a definition for FindControl and no extension method 'FindControl' accepting a first argument of type 'System.Web.UI.WebControls.ListItemViewEventArgs' could be found – Ryan McSorley Nov 03 '16 at 17:07
  • System.Web.UI.WebControls.ListItemViewEventArgs' does not contain a definition for FindControl and no extension method 'FindControl' accepting a first argument of type 'System.Web.UI.WebControls.ListItemViewEventArgs' could be found – Ryan McSorley Nov 03 '16 at 17:14
  • Sorry, forgot to add one thing. Will edit, just a sec – Felipe Deguchi Nov 03 '16 at 17:16
  • Anytime. Glad I could help. – Felipe Deguchi Nov 03 '16 at 17:32