Ok, but how do I change the button-name from "edit" to ex "edit this"...? How do I write the codebehind to actually execute a Stored Procedure with a parameter (ID)?
Main Topics
Browse All TopicsI have a gridview with one column called "Advertiser". When the "Edit" button is clicked, the litteral-control shall be replaced with an textbox control, and the "Edit button shall be replaced with "Save" and "Cancel" buttons.
The datagrid is filled correctly with data, but I dont know how to write the "Update" or "Cancel" command.
Here is my code so far:
--------------
ASPX
--------------
<asp:GridView runat="server" id="grdAdvertisers" AutoGenerateColumns="false
<EditRowStyle BackColor="yellow" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton CommandName="Edit" Text="Edit" Width="45px" runat="server" />
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton CommandName="Update" Text="Update" Width="45px" runat="server" />
<asp:LinkButton CommandName="Cancel" Text="Cancel" Width="45px" runat="server" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<%# DataBinder.Eval(Container.
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtAdvertiser" Text='<%# DataBinder.Eval(Container.
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
------------------
ASPX.CS
------------------
private void bindAdvertisers()
{
SqlConnection Conn = new SqlConnection(Variables.Co
SqlCommand cmd = new SqlCommand("usp_Advertiser
cmd.CommandType = CommandType.StoredProcedur
cmd.Connection.Open();
SqlDataReader dr = cmd.ExecuteReader();
this.grdAdvertisers.DataSo
this.grdAdvertisers.DataBi
cmd.Connection.Close();
cmd.Dispose();
Conn.Dispose();
}
protected void grdAdvertisers_RowEditing(
{
//Response.Write(e.NewEdit
}
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
If you need to stick to that kind of layout, then I would suggest something like this example:
<asp:GridView ID="GridView2" runat="server" Style="z-index: 103; left: 144px; position: absolute;
top: 212px" AutoGenerateColumns="False
OnRowEditing="GridView2_Ro
<Columns>
<asp:BoundField DataField="name" HeaderText="Name" />
<asp:BoundField DataField="number" HeaderText="Number" />
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="linkEdit" CommandName="Edit" Text="Edit" Width="45px" runat="server"
OnClick="EditClick" />
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="linkUpdate" CommandName="Update" Text="Update" Width="45px" OnClick="UpdateClick"
runat="server" />
<asp:LinkButton ID="linkCancel" CommandName="Cancel" Text="Cancel" Width="45px" OnClick="CancelClick"
runat="server" />
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
protected void EditClick(object sender, EventArgs e)
{
LinkButton button = (LinkButton)sender;
GridView grid = (GridView)this.FindParent(
int index = GetItemIndex(button.Unique
grid.EditIndex = index;
grid.DataBind();
}
protected void UpdateClick(object sender, EventArgs e)
{
LinkButton button = (LinkButton)sender;
}
protected void CancelClick(object sender, EventArgs e)
{
LinkButton button = (LinkButton)sender;
}
private int GetItemIndex(string input)
{
string pattern = @"ctl(?<index>\d+)";
Match match = Regex.Match(input, pattern);
if (match.Success)
return int.Parse(match.Groups["in
return -1;
}
private Control FindParent(Control ctrl, Type type)
{
while (ctrl.GetType() != type)
ctrl = ctrl.Parent;
return ctrl;
}
protected void GridView2_RowEditing(objec
{
}
protected void GridView2_RowCancelingEdit
{
GridView grid = (GridView)sender;
grid.EditIndex = -1;
grid.DataBind();
}
protected void GridView2_RowUpdating(obje
{
// Execute update SQL.
}
Bob
Business Accounts
Answer for Membership
by: TheLearnedOnePosted on 2006-11-29 at 07:08:39ID: 18037294
In order to get the buttons that you need, you should add CommandField columns through the designer:
<asp:CommandField ShowDeleteButton="True" />
Bob