0
<asp:TemplateField HeaderText="Team Leader">
    <ItemTemplate>
        <asp:Label ID="gvuser_teamleader" runat="server" Text='<%# Bind("TeamLeaderID")  %>'></asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
        <asp:TextBox ID="txtuserteamleader" runat="server" Width="100px" Text='<%# Eval("TeamLeaderID") %>' CssClass="textboxstyle roundedcorner aligncenter gradientskyblue"></asp:TextBox>
        <asp:ListBox ID="listboxuserteamleader" runat="server" Width="110px" AutoPostBack="true" OnSelectedIndexChanged="listboxuserteamleader_SelectedIndexChanged" CssClass="textboxstyle roundedcorner aligncenter gradientskyblue"></asp:ListBox>
        <asp:DropDownExtender ID="DropDownExtender3" runat="server" TargetControlID="txtuserteamleader" DropDownControlID="listboxuserteamleader"></asp:DropDownExtender>                                    
    </EditItemTemplate>
</asp:TemplateField>

当我尝试触发选定的列表框索引更改事件并尝试将列表框选定值绑定到文本框时,出现此错误。此外,这两个控件都在 gridview 编辑项模板字段内。

我的代码:

protected void listboxuserteamleader_SelectedIndexChanged(object sender, EventArgs e)
    {
        foreach (GridViewRow gvr in gvusers.Rows)
        {
            TextBox txtuserteamleader = (TextBox)gvusers.FindControl("txtuserteamleader");
            ListBox listboxuserteamleader = (ListBox)gvusers.FindControl("listboxuserteamleader");
            txtuserteamleader.Text = listboxuserteamleader.SelectedValue.ToString();
        }
    }
4

2 回答 2

2

只需尝试以下代码:

protected void listboxuserteamleader_SelectedIndexChanged(object sender, EventArgs e)
{
foreach (GridViewRow gvr in gvusers.Rows)
{
    TextBox txtuserteamleader = (TextBox)gvr.FindControl("txtuserteamleader");
    ListBox listboxuserteamleader = (ListBox)gvr.FindControl("listboxuserteamleader");
    if(txtuserteamleader !=null && listboxuserteamleader !=null)
     {
       txtuserteamleader.Text = listboxuserteamleader.SelectedValue.ToString();
     }
 }
}

实际上你的问题是什么:你已经为每一行创建了gridview“gvusers”的实例作为gvr......所以在foreach中你必须使用该实例而不是“gvusers”......在这里你犯了错误......

就这样

于 2013-10-25T05:09:50.300 回答
0
try this:

protected void listboxuserteamleader_SelectedIndexChanged(object sender, EventArgs e)
{
    foreach (GridViewRow gvr in gvusers.Rows)
    {
        TextBox txtuserteamleader = (TextBox)gvr.FindControl("txtuserteamleader");
        ListBox listboxuserteamleader = (ListBox)gvr.FindControl("listboxuserteamleader");
        txtuserteamleader.Text = listboxuserteamleader.SelectedValue.ToString();
    }
}

当您遍历 GridView 的行时,您必须在该行内搜索控件,因此您犯的错误是您在整个 GridView 中搜索它,其中每一行都有此控件。所以你需要连续搜索它。

于 2013-10-25T05:07:16.880 回答