<html>
<head>
<title>GridView UpdateRow Example</title>
<link rel="stylesheet" href="/shared/netdemos.css">
<script runat="server" language="C#">
void updateRow ( Object src, EventArgs e ) {
// programmatically update the current record in edit mode.
msgsGrid.UpdateRow ( msgsGrid.EditIndex, true );
updateRowButton.Enabled = false;
}
void enableUpdateButton ( Object src, GridViewCommandEventArgs e ) {
// enable the updateRowButton only when the GridView control is in edit mode.
updateRowButton.Enabled = e.CommandName == "Edit";
}
</script>
</head>
<body>
<!-- #include virtual="~/shared/top.inc" -->
<div class="header"><h2>GridView <span class="hilite">UpdateRow</span> Example</h2></div>
<hr size=1 width=92%>
<form runat="server">
<div align="center">
<p><asp:button id="updateRowButton"
text="Update Row"
enabled="false"
onclick="updateRow"
runat="server" /></p>
<asp:gridview id="msgsGrid" runat="server"
datasourceid="messages"
datakeynames="MessageID"
width=80% cellpadding=5
allowpaging
autogeneratecolumns="false"
autogenerateeditbutton
onrowcommand="enableUpdateButton">
<headerstyle backcolor="steelblue"
forecolor="beige" />
<rowstyle verticalalign="top" />
<editrowstyle backcolor="lightgreen" />
<columns>
<asp:boundfield headertext="From"
datafield="messagefrom" />
<asp:boundfield headertext="Email"
datafield="email" />
<asp:boundfield headertext="Subject"
datafield="messagesubject" />
<asp:boundfield headertext="Message"
datafield="messagebody"
itemstyle-width=200
itemstyle-wrap />
</columns>
</asp:gridview>
<asp:sqldatasource id="messages" runat="server"
selectcommand="SELECT * FROM aspx_messages"
updatecommand="UPDATE aspx_messages SET
MessageFrom=@MessageFrom, Email=@Email, MessageSubject=@MessageSubject, MessageBody=@MessageBody
WHERE MessageID=@MessageID"
connectionstring="<%$ ConnectionStrings:aspnet %>" />
</div>
</form>
<hr size=1 width=92%>
<!-- #include virtual="~/shared/viewsrc.inc" -->
</body>
</html>