'username'在当前上下文中不存在

时间:2016-11-03 15:24:45

标签: c# asp.net

ID位于ItemTemplate内,无法在代码文件中找到。

<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#表示无法找到'userName'。这是我的后端代码:

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

如何通过?

获取userName值

1 个答案:

答案 0 :(得分:0)

问题是ListView可以返回多个项目,因此后面的代码不知道你在谈论哪个itens控件(用户名和ProjectOwner)。

如果你想对每个项目做同样的事情,你可以这样做:

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();
    }
}