emmons
Technical User
- Oct 9, 2002
- 114
Hello, can you show me how to stay in edit mode after the update and have the cursor drop to the leftmost textbox in the next row down for more editing? I would be most grateful and so would the library that I am helping. Thanks a lot.
<%@ Page Language="C#" ClassName="EditNamesPage" AutoEventWireup="False"
EnableSessionState="False" Debug="True"%>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.Odbc" %>
<script runat="server">
private const string ConnStr = "Driver={MySQL ODBC 3.51 Driver};" +
"Server=localhost;Database=mydatabase;uid=myuid;pwd=mypwd;option=3";
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
if(!IsPostBack)
BindDataGrid();
}
private void BindDataGrid()
{
using(OdbcConnection con = new OdbcConnection(ConnStr))
using(OdbcCommand cmd = new OdbcCommand("SELECT id, date_format(dates,'%M %e') as Date, date_format(dates,'%W') as Day, FirstName, LastName FROM Names WHERE dates BETWEEN current_date - interval '16' day AND current_date ", con))
{
con.Open();
DataGrid1.DataSource = cmd.ExecuteReader(
CommandBehavior.CloseConnection |
CommandBehavior.SingleResult);
DataGrid1.DataBind();
}
}
private void DoAddName(object sender, EventArgs e)
{
if(IsAddNameValid())
{
HtmlTable2.Visible = false;
using(OdbcConnection con = new OdbcConnection(ConnStr))
using(OdbcCommand cmd = new OdbcCommand(
"INSERT INTO Names(FirstName, LastName) VALUES (?,?)", con))
{
cmd.Parameters.Add("@FirstName", OdbcType.VarChar, 255).Value =
Textbox3.Text.Trim();
cmd.Parameters.Add("@LastName", OdbcType.VarChar, 255).Value =
Textbox4.Text.Trim();
con.Open();
cmd.ExecuteNonQuery();
BindDataGrid();
}
}
}
private void DoUpdateName(int id, string firstName, string lastName)
{
using(OdbcConnection con = new OdbcConnection(ConnStr))
using(OdbcCommand cmd = new OdbcCommand(
"UPDATE Names SET FirstName = ?, LastName = ? WHERE ID = ?", con))
{
cmd.Parameters.Add("@FirstName", OdbcType.VarChar, 255).Value =
firstName;
cmd.Parameters.Add("@LastName", OdbcType.VarChar, 255).Value =
lastName;
cmd.Parameters.Add("@ID", OdbcType.Int).Value =
id;
con.Open();
cmd.ExecuteNonQuery();
}
}
private void DoDeleteName(int id)
{
using(OdbcConnection con = new OdbcConnection(ConnStr))
using(OdbcCommand cmd = new OdbcCommand(
"DELETE FROM Names WHERE ID = ?", con))
{
cmd.Parameters.Add("@ID", OdbcType.Int).Value = id;
con.Open();
cmd.ExecuteNonQuery();
}
}
private bool IsAddNameValid()
{
HtmlTable2.Visible = true;
Requiredfieldvalidator1.Validate();
Requiredfieldvalidator2.Validate();
Regularexpressionvalidator1.Validate();
Regularexpressionvalidator2.Validate();
return (Requiredfieldvalidator1.IsValid &&
Requiredfieldvalidator2.IsValid &&
Regularexpressionvalidator1.IsValid &&
Regularexpressionvalidator2.IsValid);
}
private void DataGrid1_ItemCommand(object sender, DataGridCommandEventArgs e)
{
switch(e.CommandName)
{
case "Delete":
Literal literal1 = (Literal)e.Item.FindControl("Literal1");
DoDeleteName(Convert.ToInt32(literal1.Text));
BindDataGrid();
break;
default:
break;
}
}
private void DataGrid1_EditCommand(object sender, DataGridCommandEventArgs e)
{
HtmlTable1.Visible = false;
HtmlTable2.Visible = false;
DataGrid1.EditItemIndex = e.Item.ItemIndex;
BindDataGrid();
}
private void DataGrid1_UpdateCommand(object sender, DataGridCommandEventArgs e)
{
Literal literal1 = (Literal)e.Item.FindControl("Literal1");
TextBox textbox1 = (TextBox)e.Item.FindControl("TextBox1");
TextBox textbox2 = (TextBox)e.Item.FindControl("TextBox2");
DoUpdateName(Convert.ToInt32(literal1.Text), textbox1.Text.Trim(),
textbox2.Text.Trim());
HtmlTable1.Visible = true;
DataGrid1.EditItemIndex = -1;
BindDataGrid();
}
private void DataGrid1_CancelCommand(object sender, DataGridCommandEventArgs e)
{
HtmlTable1.Visible = true;
DataGrid1.EditItemIndex = -1;
BindDataGrid();
}
</script>
<html>
<head>
<title>Displaying/Inserting/Editing/Deleting Records from
MySQL 'Names' table</title>
<style>
body, td { font: 8pt Tahoma; }
a { color: blue; }
a:hover { color: red; }
pre { display: none; }
.stdErr { color: red; }
.stdInput { font: 8pt Tahoma; width: 100px; }
.stdButton { font: 8pt Tahoma; }
.stdBogus {width: 0px}
</style>
</head>
<body>
<p align="center"><a href="editnames.aspx">"Names" Table</a></p>
<form runat="server">
<aspataGrid ID="DataGrid1" HorizontalAlign="Center" CellPadding="3"
HeaderStyle-BackColor="#F7F7F7" EditItemStyle-BackColor="#F7F7F7"
AutoGenerateColumns="False" DataKeyField="ID"
OnEditCommand="DataGrid1_EditCommand"
OnUpdateCommand="DataGrid1_UpdateCommand"
OnCancelCommand="DataGrid1_CancelCommand"
OnItemCommand="DataGrid1_ItemCommand"
Runat="server">
<Columns>
<asp:TemplateColumn HeaderStyle-Width="30" HeaderText="ID">
<ItemTemplate>
<asp:Literal ID="Literal1" Text='<%#DataBinder.Eval(Container.DataItem, "ID") %>' Runat="server" />
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderStyle-Width="30" HeaderText="Date">
<ItemTemplate>
<asp:Literal ID="Literal2" Text='<%#DataBinder.Eval(Container.DataItem, "date") %>'
Runat="server" />
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderStyle-Width="30" HeaderText="Day">
<ItemTemplate>
<asp:Literal ID="Literal3" Text='<%#DataBinder.Eval(Container.DataItem, "day") %>'
Runat="server" />
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderStyle-Width="100" HeaderText="First Name">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "FirstName") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" Text='<%#DataBinder.Eval(Container.DataItem, "FirstName") %>'
CssClass="stdInput" Runat="server" />
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderStyle-Width="100" HeaderText="Last Name">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "LastName") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="Textbox2" Text='<%#DataBinder.Eval(Container.DataItem, "LastName") %>'
CssClass="stdInput" Runat="server" />
</EditItemTemplate>
</asp:TemplateColumn>
<asp:EditCommandColumn EditText="Edit"
UpdateText="Update" CancelText="Cancel" />
<asp:ButtonColumn CommandName="Delete" Text="" />
</Columns>
</aspataGrid>
<br>
<table id="HtmlTable1" cellspacing="0" cellpadding="3" align="center"
rules="all"
border="0" style="border-collapse:collapse;" runat="server">
<tr style="background-color: white">
<td> </td><td> </td><td> </td>
</tr>
<tr>
<td style="width:100px;">
<asp:TextBox ID="Textbox3" CssClass="stdBogus" Runat="server" />
</td>
<td style="width:100px;">
<asp:TextBox ID="Textbox4" CssClass="stdBogus" Runat="server" />
</td>
<td><a id="HtmlAnchor1" onserverclick="DoAddName" runat="server">
</a></td>
</tr>
</table>
<br>
<table id="HtmlTable2" cellspacing="0" cellpadding="3" align="center"
rules="all"
border="1" style="border-collapse:collapse;" Visible="False"
runat="server">
<tr>
<td style="background-color: #F7F7F7;width: 234px;">Errors</td>
</tr>
<tr>
<td>
<asp:RequiredFieldValidator ID="Requiredfieldvalidator1"
controlToValidate="Textbox3"
Display="None" EnableClientScript="False"
ErrorMessage="*First Name* is a required field"
Runat="server" />
<asp:RequiredFieldValidator ID="Requiredfieldvalidator2"
controlToValidate="Textbox4"
Display="None" EnableClientScript="False"
ErrorMessage="*Last Name* is a required field"
Runat="server" />
<asp:RegularExpressionValidator ID="Regularexpressionvalidator1"
controlToValidate="Textbox3"
ValidationExpression="[\w\s]{1,255}"
Display="None" EnableClientScript="False"
ErrorMessage="*First Name* contains illegal values"
Runat="server" />
<asp:RegularExpressionValidator ID="Regularexpressionvalidator2"
controlToValidate="Textbox4"
ValidationExpression="[\w\s]{1,255}"
Display="None" EnableClientScript="False"
ErrorMessage="*Last Name* contains illegal values"
Runat="server" />
<asp:ValidationSummary ID="ValidationSummary1" DisplayMode="List"
ShowSummary="True" Runat="server" />
</td>
</tr>
</table><br>
</form>
</body>
</html>
<%@ Page Language="C#" ClassName="EditNamesPage" AutoEventWireup="False"
EnableSessionState="False" Debug="True"%>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.Odbc" %>
<script runat="server">
private const string ConnStr = "Driver={MySQL ODBC 3.51 Driver};" +
"Server=localhost;Database=mydatabase;uid=myuid;pwd=mypwd;option=3";
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
if(!IsPostBack)
BindDataGrid();
}
private void BindDataGrid()
{
using(OdbcConnection con = new OdbcConnection(ConnStr))
using(OdbcCommand cmd = new OdbcCommand("SELECT id, date_format(dates,'%M %e') as Date, date_format(dates,'%W') as Day, FirstName, LastName FROM Names WHERE dates BETWEEN current_date - interval '16' day AND current_date ", con))
{
con.Open();
DataGrid1.DataSource = cmd.ExecuteReader(
CommandBehavior.CloseConnection |
CommandBehavior.SingleResult);
DataGrid1.DataBind();
}
}
private void DoAddName(object sender, EventArgs e)
{
if(IsAddNameValid())
{
HtmlTable2.Visible = false;
using(OdbcConnection con = new OdbcConnection(ConnStr))
using(OdbcCommand cmd = new OdbcCommand(
"INSERT INTO Names(FirstName, LastName) VALUES (?,?)", con))
{
cmd.Parameters.Add("@FirstName", OdbcType.VarChar, 255).Value =
Textbox3.Text.Trim();
cmd.Parameters.Add("@LastName", OdbcType.VarChar, 255).Value =
Textbox4.Text.Trim();
con.Open();
cmd.ExecuteNonQuery();
BindDataGrid();
}
}
}
private void DoUpdateName(int id, string firstName, string lastName)
{
using(OdbcConnection con = new OdbcConnection(ConnStr))
using(OdbcCommand cmd = new OdbcCommand(
"UPDATE Names SET FirstName = ?, LastName = ? WHERE ID = ?", con))
{
cmd.Parameters.Add("@FirstName", OdbcType.VarChar, 255).Value =
firstName;
cmd.Parameters.Add("@LastName", OdbcType.VarChar, 255).Value =
lastName;
cmd.Parameters.Add("@ID", OdbcType.Int).Value =
id;
con.Open();
cmd.ExecuteNonQuery();
}
}
private void DoDeleteName(int id)
{
using(OdbcConnection con = new OdbcConnection(ConnStr))
using(OdbcCommand cmd = new OdbcCommand(
"DELETE FROM Names WHERE ID = ?", con))
{
cmd.Parameters.Add("@ID", OdbcType.Int).Value = id;
con.Open();
cmd.ExecuteNonQuery();
}
}
private bool IsAddNameValid()
{
HtmlTable2.Visible = true;
Requiredfieldvalidator1.Validate();
Requiredfieldvalidator2.Validate();
Regularexpressionvalidator1.Validate();
Regularexpressionvalidator2.Validate();
return (Requiredfieldvalidator1.IsValid &&
Requiredfieldvalidator2.IsValid &&
Regularexpressionvalidator1.IsValid &&
Regularexpressionvalidator2.IsValid);
}
private void DataGrid1_ItemCommand(object sender, DataGridCommandEventArgs e)
{
switch(e.CommandName)
{
case "Delete":
Literal literal1 = (Literal)e.Item.FindControl("Literal1");
DoDeleteName(Convert.ToInt32(literal1.Text));
BindDataGrid();
break;
default:
break;
}
}
private void DataGrid1_EditCommand(object sender, DataGridCommandEventArgs e)
{
HtmlTable1.Visible = false;
HtmlTable2.Visible = false;
DataGrid1.EditItemIndex = e.Item.ItemIndex;
BindDataGrid();
}
private void DataGrid1_UpdateCommand(object sender, DataGridCommandEventArgs e)
{
Literal literal1 = (Literal)e.Item.FindControl("Literal1");
TextBox textbox1 = (TextBox)e.Item.FindControl("TextBox1");
TextBox textbox2 = (TextBox)e.Item.FindControl("TextBox2");
DoUpdateName(Convert.ToInt32(literal1.Text), textbox1.Text.Trim(),
textbox2.Text.Trim());
HtmlTable1.Visible = true;
DataGrid1.EditItemIndex = -1;
BindDataGrid();
}
private void DataGrid1_CancelCommand(object sender, DataGridCommandEventArgs e)
{
HtmlTable1.Visible = true;
DataGrid1.EditItemIndex = -1;
BindDataGrid();
}
</script>
<html>
<head>
<title>Displaying/Inserting/Editing/Deleting Records from
MySQL 'Names' table</title>
<style>
body, td { font: 8pt Tahoma; }
a { color: blue; }
a:hover { color: red; }
pre { display: none; }
.stdErr { color: red; }
.stdInput { font: 8pt Tahoma; width: 100px; }
.stdButton { font: 8pt Tahoma; }
.stdBogus {width: 0px}
</style>
</head>
<body>
<p align="center"><a href="editnames.aspx">"Names" Table</a></p>
<form runat="server">
<aspataGrid ID="DataGrid1" HorizontalAlign="Center" CellPadding="3"
HeaderStyle-BackColor="#F7F7F7" EditItemStyle-BackColor="#F7F7F7"
AutoGenerateColumns="False" DataKeyField="ID"
OnEditCommand="DataGrid1_EditCommand"
OnUpdateCommand="DataGrid1_UpdateCommand"
OnCancelCommand="DataGrid1_CancelCommand"
OnItemCommand="DataGrid1_ItemCommand"
Runat="server">
<Columns>
<asp:TemplateColumn HeaderStyle-Width="30" HeaderText="ID">
<ItemTemplate>
<asp:Literal ID="Literal1" Text='<%#DataBinder.Eval(Container.DataItem, "ID") %>' Runat="server" />
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderStyle-Width="30" HeaderText="Date">
<ItemTemplate>
<asp:Literal ID="Literal2" Text='<%#DataBinder.Eval(Container.DataItem, "date") %>'
Runat="server" />
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderStyle-Width="30" HeaderText="Day">
<ItemTemplate>
<asp:Literal ID="Literal3" Text='<%#DataBinder.Eval(Container.DataItem, "day") %>'
Runat="server" />
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderStyle-Width="100" HeaderText="First Name">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "FirstName") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" Text='<%#DataBinder.Eval(Container.DataItem, "FirstName") %>'
CssClass="stdInput" Runat="server" />
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderStyle-Width="100" HeaderText="Last Name">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "LastName") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="Textbox2" Text='<%#DataBinder.Eval(Container.DataItem, "LastName") %>'
CssClass="stdInput" Runat="server" />
</EditItemTemplate>
</asp:TemplateColumn>
<asp:EditCommandColumn EditText="Edit"
UpdateText="Update" CancelText="Cancel" />
<asp:ButtonColumn CommandName="Delete" Text="" />
</Columns>
</aspataGrid>
<br>
<table id="HtmlTable1" cellspacing="0" cellpadding="3" align="center"
rules="all"
border="0" style="border-collapse:collapse;" runat="server">
<tr style="background-color: white">
<td> </td><td> </td><td> </td>
</tr>
<tr>
<td style="width:100px;">
<asp:TextBox ID="Textbox3" CssClass="stdBogus" Runat="server" />
</td>
<td style="width:100px;">
<asp:TextBox ID="Textbox4" CssClass="stdBogus" Runat="server" />
</td>
<td><a id="HtmlAnchor1" onserverclick="DoAddName" runat="server">
</a></td>
</tr>
</table>
<br>
<table id="HtmlTable2" cellspacing="0" cellpadding="3" align="center"
rules="all"
border="1" style="border-collapse:collapse;" Visible="False"
runat="server">
<tr>
<td style="background-color: #F7F7F7;width: 234px;">Errors</td>
</tr>
<tr>
<td>
<asp:RequiredFieldValidator ID="Requiredfieldvalidator1"
controlToValidate="Textbox3"
Display="None" EnableClientScript="False"
ErrorMessage="*First Name* is a required field"
Runat="server" />
<asp:RequiredFieldValidator ID="Requiredfieldvalidator2"
controlToValidate="Textbox4"
Display="None" EnableClientScript="False"
ErrorMessage="*Last Name* is a required field"
Runat="server" />
<asp:RegularExpressionValidator ID="Regularexpressionvalidator1"
controlToValidate="Textbox3"
ValidationExpression="[\w\s]{1,255}"
Display="None" EnableClientScript="False"
ErrorMessage="*First Name* contains illegal values"
Runat="server" />
<asp:RegularExpressionValidator ID="Regularexpressionvalidator2"
controlToValidate="Textbox4"
ValidationExpression="[\w\s]{1,255}"
Display="None" EnableClientScript="False"
ErrorMessage="*Last Name* contains illegal values"
Runat="server" />
<asp:ValidationSummary ID="ValidationSummary1" DisplayMode="List"
ShowSummary="True" Runat="server" />
</td>
</tr>
</table><br>
</form>
</body>
</html>