1

我正在itemtemplate使用表格获取表单视图字段。我需要username进入一个变量,以便我可以删除一个特定row.Username的唯一性。

这是我的代码:

 <ItemTemplate>
    <table>
        <tr>
            <td align="right"><b>User Name:</b></td>
            <td><%# Eval("UserName") %></td>
        </tr>
        <tr>
            <td align="right"><b>Password:</b></td>
            <td><%# Eval("Password") %></td>
        </tr>
        <tr>
            <td align="right"><b>First Name:</b></td>
            <td><%# Eval("FirstName") %></td>
        </tr>
        <tr>
            <td align="right"><b>Last Name:</b></td>
            <td><%# Eval("LastName") %></td>
        </tr>
        <tr>
            <td colspan="2">
                <asp:LinkButton ID="EditButton"
                                    Text="Edit"
                                    CommandName="Edit"
                                    RunAt="server"/>
                &nbsp;
                <asp:LinkButton ID="NewButton"
                                    Text="New"
                                    CommandName="New"
                                    RunAt="server"/>
                &nbsp;
                <asp:LinkButton ID="DeleteButton"
                                    Text="Delete"
                                    CommandName="Delete"
                                    RunAt="server"/>
            </td>
        </tr>
    </table>
</ItemTemplate>

我该怎么做C#

任何帮助将不胜感激。

4

1 回答 1

2

我会改用服务器控件,例如 a Label,然后您可以通过以下方式找到它item.FindControl

aspx:

<tr>
    <td align="right"><b>User Name:</b></td><td>
        <asp:Label Id="LblUserName" runat="server"
             Text='<%# Eval("UserName") %>'></asp:label>
    </td>
</tr>
.......
<asp:LinkButton ID="DeleteButton"
    Text="Delete"
    CommandName="Delete"
    RunAt="server"
    OnCommand="DeleteButton_Command" />

代码隐藏:

void DeleteButton_Command(Object sender, CommandEventArgs e) 
{
    if(e.CommandName == "Delete")
    {
        LinkButton btnDelete = (LinkButton)sender;
        var item = (FormViewItem)btnDelete.NamingContainer;
        Label LblUserName = (Label)item.FindControl("LblUserName");
        String userName = LblUserName.Text;
    }
}
于 2012-09-03T14:16:11.227 回答