Solved

Gridview Editing without Autogenerated Buttons

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

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

628 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