求asp.net验证及dataGrid控件用法

antoniusguo 2004-12-22 12:14:51
求asp.net代码
asp.net身份验证代码
匿名用户只能登陆到登陆页面,否则自动跳转到登陆页面
以及dataGrid控件绑定到Sql数据库
用户可以在dataGrid中添加,删除,修改记录,
只作学习用,请用C#写
请把代码发到
antoniusguo@hotmail.com
...全文
176 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
antoniusguo 2004-12-22
  • 打赏
  • 举报
回复
22号8点开始,收到邮件时结贴
nhf80649 2004-12-22
  • 打赏
  • 举报
回复
关注~
vzxq 2004-12-22
  • 打赏
  • 举报
回复
身份验证web.config 中加入
<authentication mode="Forms">
<forms name="admin_manage" loginUrl="login.aspx" protection="All" path="/" />
</authentication>
<authorization>
<deny users="?" />
</authorization>
生活真美好 2004-12-22
  • 打赏
  • 举报
回复
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Data" %>
<html>
<body>
<form method=post runat="server">
<asp:DataGrid runat="server" ID="Datagrid1"
DataKeyField="EmployeeID"
OnEditCommand="Datagrid1_Edit"
OnUpdateCommand="Datagrid1_Update"
OnCancelCommand="Datagrid1_Cancel"
OnDeleteCommand="Datagrid1_Delete"
AutoGenerateColumns="False"
Width="100%"
HeaderStyle-Font-Size="10"
HeaderStyle-Font-Bold="true"
HeaderStyle-ForeColor="Red"
HeaderStyle-BackColor="Yellow"
HeaderStyle-BorderColor="Red"
HeaderStyle-BorderWidth="5"
FooterStyle-BorderColor="Red"
FooterStyle-BorderWidth="5"
ItemStyle-BackColor="LightCyan"
ItemStyle-ForeColor="DarkBlue"
AlternatingItemStyle-BackColor="LightYellow"
AlternatingItemStyle-ForeColor="Maroon">
<Columns>
<asp:EditCommandColumn ItemStyle-Width="25px"
EditText="<img border=0 alt='Edit record' src=Edit.gif>"
CancelText="<img border=0 alt='Cancel changes' src=Cancel.gif>"
UpdateText="<img border=0 alt='Accept changes' src=OK.gif>"
/>
<asp:ButtonColumn ItemStyle-Width="25px" ButtonType="LinkButton"
Text="<img border=0 src=Delete.gif>" CommandName="delete"
/>
<asp:BoundColumn HeaderText="ID" ItemStyle-Width="30px" DataField="EmployeeID" ReadOnly="true" />
<asp:TemplateColumn HeaderText="Title" ItemStyle-Width="50px">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "TitleOfCourtesy") %>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList runat="server" ID="EditTitle" DataSource='<%# TitlesOfCourtesy %>'
SelectedIndex='<%# GetSelectedTitle(DataBinder.Eval(Container.DataItem, "TitleOfCourtesy")) %>' />
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Name">
<ItemTemplate>
<b><%# DataBinder.Eval(Container.DataItem, "LastName") %></b>,
<%# DataBinder.Eval(Container.DataItem, "FirstName") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="EditLastName" Text='<%# DataBinder.Eval(Container.DataItem, "LastName") %>' />
<asp:TextBox runat="server" ID="EditFirstName" Text='<%# DataBinder.Eval(Container.DataItem, "FirstName") %>' />
</EditItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn HeaderText="City" ItemStyle-Width="150px" DataField="City" />
<asp:TemplateColumn HeaderText="USA?" ItemStyle-Width="35px" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:CheckBox runat="server" Enabled="false"
Checked='<%# DataBinder.Eval(Container.DataItem, "Country").ToString() == "USA" %>' />
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
</form>
</body>
</html>

<script runat="server" language="C#">
public string[] TitlesOfCourtesy
{
get {
return new string[4]{"Mr.", "Dr.", "Ms.", "Mrs."};
}
}

int GetSelectedTitle(object title)
{
return Array.IndexOf(TitlesOfCourtesy, title.ToString());
}

void Page_Load()
{
if (!Page.IsPostBack)
BindGrid();
}

void BindGrid()
{
// create the command and the connection
string connString = "server=(local);database=Northwind;uid=sa;pwd=;";
string sql = @"SELECT * FROM Employees";
SqlConnection conn = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand(sql, conn);

// open the connection and get the Reader
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();

// bind the reader to the DataList
Datagrid1.DataSource = reader;
Datagrid1.DataBind();

// close the reader and the connection
reader.Close();
conn.Close();
}

void Datagrid1_Edit(Object sender, DataGridCommandEventArgs e)
{
Datagrid1.EditItemIndex = (int)e.Item.ItemIndex;
BindGrid();
}

void Datagrid1_Cancel(Object sender, DataGridCommandEventArgs e)
{
Datagrid1.EditItemIndex = -1;
BindGrid();
}

void Datagrid1_Update(Object sender, DataGridCommandEventArgs e)
{
// get the ID of the record to update
int empID = (int)Datagrid1.DataKeys[e.Item.ItemIndex];

// get the references to the edit controls
DropDownList title = (DropDownList)e.Item.FindControl("EditTitle");
TextBox lastName = (TextBox)e.Item.FindControl("EditLastName");
TextBox firstName = (TextBox)e.Item.FindControl("EditFirstName");
TextBox city = (TextBox)e.Item.Cells[5].Controls[0];

// create the connection and the UPDATE command
string connString = "server=(local);database=Northwind;uid=sa;pwd=;";
string sql = @"UPDATE Employees SET TitleOfCourtesy = @TitleOfCourtesy,
LastName = @LastName, FirstName = @FirstName, City = @City WHERE EmployeeID = @EmployeeID";
SqlConnection conn = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand(sql, conn);

// create the required parameters
cmd.Parameters.Add(new SqlParameter("@TitleOfCourtesy", SqlDbType.NVarChar, 25));
cmd.Parameters["@TitleOfCourtesy"].Value = title.SelectedItem.Text.Trim();
cmd.Parameters.Add(new SqlParameter("@LastName", SqlDbType.NVarChar, 20));
cmd.Parameters["@LastName"].Value = lastName.Text.Trim();
cmd.Parameters.Add(new SqlParameter("@FirstName", SqlDbType.NVarChar, 10));
cmd.Parameters["@FirstName"].Value = firstName.Text.Trim();
cmd.Parameters.Add(new SqlParameter("@City", SqlDbType.NVarChar, 15));
cmd.Parameters["@City"].Value = city.Text.Trim();
cmd.Parameters.Add(new SqlParameter("@EmployeeID", SqlDbType.Int, 4));
cmd.Parameters["@EmployeeID"].Value = empID;

// execute the command
cmd.Connection.Open();
try {
cmd.ExecuteNonQuery();
}
catch (SqlException) {
// handle exception...
}
finally {
cmd.Connection.Close();
}

// stop the editing and rebind the grid
Datagrid1.EditItemIndex = -1;
BindGrid();
}

void Datagrid1_Delete(Object sender, DataGridCommandEventArgs e)
{
// get the ID of the record to update
int empID = (int)Datagrid1.DataKeys[e.Item.ItemIndex];

// create the connection and the DELETE command
string connString = "server=(local);database=Northwind;uid=sa;pwd=;";
string sql = @"DELETE FROM Employees WHERE EmployeeID = " + empID.ToString();
SqlConnection conn = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand(sql, conn);

// execute the command
cmd.Connection.Open();
try {
cmd.ExecuteNonQuery();
}
catch (SqlException) {
// handle exception...
}
finally {
cmd.Connection.Close();
}

// rebind the grid
Datagrid1.EditItemIndex = -1;
BindGrid();
}
</script>
werwsxwdfwdc 2004-12-22
  • 打赏
  • 举报
回复
看来没人给要这100分我写给你吧
werwsxwdfwdc 2004-12-22
  • 打赏
  • 举报
回复

62,243

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧