Solved

Gridview Editing without Autogenerated Buttons

Posted on 2008-10-24
5
859 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
5 Comments
 
LVL 15

Expert Comment

by:jorge_toriz
ID: 22798832
<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
ID: 22798949
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
ID: 22812830
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
ID: 22812856
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
ID: 22813184
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

733 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