开发者

How to get cell contents in ASP.NET GridView while handling OnRowEditing?

开发者 https://www.devze.com 2023-01-26 03:21 出处:网络
I have a GridView with a BoundField column and quite a few item templates like the following: <asp:GridView ID=\"gvUsers\" runat=\"server\" AutoGenerateColumns=\"False\" OnRowCommand=\"gvUsers_Row

I have a GridView with a BoundField column and quite a few item templates like the following:

<asp:GridView ID="gvUsers" runat="server" AutoGenerateColumns="False" OnRowCommand="gvUsers_RowCommand" 
OnRowDataBound="gvUsers_RowDataBound" DataKeyNames="UserId" OnRowEditing="gvUsers_OnRowEditing" 
OnRowUpdating="gvUsers_OnRowUpdating" OnRowUpdated="gvUsers_OnRowUpdated"
     DataSourceID="DataSource1" Width="807px" Height="105px" 
    AllowPaging="True" >
<Columns>
    <asp:BoundField DataField="UserName" HeaderText="User Name" 
        SortExpression="UserName" />
    <asp:TemplateField HeaderText="Approver">
        <ItemTemplate>
            <asp:CheckBox ID="cbApprover" runat="server" Enabled="false" />
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Auditor">
        <ItemTemplate>
            <asp:CheckBox ID="cbAuditor" runat="server" Enabled="false" />
        </ItemTemplate>
    </asp:TemplateField>
     <asp:TemplateField>
        <ItemTemplate>
            <asp:LinkButton ID="btnEdit" runat="server" 
                CommandArgument='<%# Eval("UserName") %>' CommandName="Edit" Text="Edit" />
            <asp:Label ID="lblPipe1" runat="server" Text=" | " />
            <asp:LinkButton ID="btnUpdate" runat="server" 
                CommandArgument='<%# Eval("UserName") %>' CommandName="Update" Text="Update" />
            <asp:Label ID="lblPipe" runat="server" Text=" | " />
            <asp:LinkButton ID="btnDelete" runat="server" 
                CommandArgument='<%# Eval("UserName") %>' CommandName="Remove" 
                OnClientClick="return confirm('Are you sure you want to delete this user?');" 
                Text="Delete" />
        </ItemTemplate>
    </asp:TemplateField>
</Columns>

While handling the Edit link button I need to use the value in the BoundField, UserName. Unfortunat开发者_StackOverflow社区ely during the OnRowEditing handler, all strings are empty "". This is also true in the ensuing OnRowDataBound handler for the row in question where e.Row.RowState == DataControlRowState.Edit. Is there a way to obtain this value after the user clicks on the Edit link, and commences the OnRowEditing event?


I arrived at a solution that was a bit more complicated than I was hoping for. I'll show code later but I did the following:

  1. Created a HiddenField to hold the
    value of the cell I wanted to edit in its view state.
  2. In the OnRowDataBound handler, I assigned the value to the
    HiddenField.
  3. The ItemTemplate, as seen above, sends the UserName as an argument. I store this value in the HiddenField.
  4. The OnRowEditing is fired after the Command handler. That is where I read the HiddenField.

If anyone has a simpler solution I would love hear about it.


Could it be that you're using late binding in TemplatedItems without EditItemTemplates?

0

精彩评论

暂无评论...
验证码 换一张
取 消