Link to home
Start Free TrialLog in
Avatar of pfcs_sql_admin
pfcs_sql_admin

asked on

Gridview Editing without Autogenerated Buttons

I really hope I am overlooking something obvious here.

Summary: How do I use a fixed command button (ie not autogenerated) to fire off the built-in editing capabilities of the gridview?

Long-winded details/background: I have (had) a working gridview that did all the editing just as it was supposed to.  However, I discovered that when the user scrolled down to click the autogenerated Edit link for a row "way down the list", the postback would cause the grid to reset to the top.  This would force the user to have to scroll all the way down to the row that was now (properly) open for editing.

I looked all over to fix it, but the only thing was to find a control to click on in the row that I could tie some javascript to in order to use the ScrollIntoView method.  And searching for help on all that has lead to the conclusion that I need to NOT autogenerate the Edit button, but to instead create them for real.

--- End of long-winded details.  :)


So having made the change, I am now getting an error in the RowCommand event where it tries to determine the row index.  

"Dim index As Integer = Convert.ToInt32(e.CommandArgument)"  ==> Input string was not in a correct format

This line works flawlessly for the other command buttons (I have a couple other buttons on the row) and even worked before for the Edit until I made the change.

What am I missing?  At this point, the only thing I have changed has been to add the item/edit template for the buttons, with the commandnames set accordingly ("Edit", "Update", "Cancel") and to set "AutogenerateEditButton" to false.

Any ideas?
Avatar of jorge_toriz
jorge_toriz
Flag of Mexico image

<asp:GridView id="gvProducts" runat="server" DataSourceID="sdsProducts" AutoGenerateColumns="false" DataKeyNames="ProductId">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <%# DataBinder.Eval(Container.DataItem, "Name") %>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox id="txtName" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Name") %>' />
            </EditItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:ImageButton id="cmdEdit" runat="server" CommandName="Edit" ImageUrl="YourImage.jpg"/>
                <asp:ImageButton id="cmdDelete" runat="server" CommandName="Delete" ImageUrl="YourImage.jpg"/>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:ImageButton id="cmdUpdate" runat="server" CommandName="Update" ImageUrl="YourImage.jpg"/>
                <asp:ImageButton id="cmdCancel" runat="server" CommandName="Cancel" ImageUrl="YourImage.jpg"/>
            </EditItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

The important thing is that you must use that command names
Avatar of pfcs_sql_admin
pfcs_sql_admin

ASKER

I am using the exact names.  I probably should have posted this in the original message:

<asp:TemplateField ShowHeader="False">
	<ItemTemplate>
		<asp:Button ID="cmdEdit" runat="server" CommandName="Edit" Text="Edit" />
	</ItemTemplate>
	<EditItemTemplate>
		<asp:Button ID="cmdUpdate" runat="server" CommandName="Update"
			Text="Update" />&nbsp;<asp:Button ID="cmdCancel" runat="server" 
			CommandName="Cancel" Text="Cancel" />
	</EditItemTemplate>
</asp:TemplateField>

Open in new window

Anyone have any ideas?

It's like it is going directly to the RowCommand event without getting the built-in info that it needs (like the CommandArgument).  How can e.CommandName not be correct?  I don't have any control over that, or at least I always thought it was built-in.

Doesn't seem that I can edit, I meant "How can e.CommandArgument not be correct," above.
ASKER CERTIFIED SOLUTION
Avatar of pfcs_sql_admin
pfcs_sql_admin

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial