Solved

Gridview Editing without Autogenerated Buttons

Posted on 2008-10-24
5
855 Views
Last Modified: 2012-06-21
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?
0
Comment
Question by:pfcs_sql_admin
  • 4
5 Comments
 
LVL 15

Expert Comment

by:jorge_toriz
Comment Utility
<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
0
 

Author Comment

by:pfcs_sql_admin
Comment Utility
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

0
 

Author Comment

by:pfcs_sql_admin
Comment Utility
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.

0
 

Author Comment

by:pfcs_sql_admin
Comment Utility
Doesn't seem that I can edit, I meant "How can e.CommandArgument not be correct," above.
0
 

Accepted Solution

by:
pfcs_sql_admin earned 0 total points
Comment Utility
I finally found my own solution which I will include in case someone else has this problem:
--------
my guess is that it doesn't automatically assign the CommandArgument in template fields.
try binding the CommandArgument of the button manually:
CommandArgument='<%# Container.DataItemIndex %>'
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now