Solved

Gridview Editing without Autogenerated Buttons

Posted on 2008-10-24
5
860 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
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.…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

734 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