前台
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="add.aspx.cs" Inherits="Uphold_add" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>无标题页</title> </head> <body> <form id="form1" runat="server"> <div> <table cellspacing="0" cellpadding="0" width="100%" border="0"> <tr> <td width="30%" align="right" style="height: 25px"> IP </td> <td width="*" align="left" style="height: 25px"> <asp:TextBox ID="txtIP" runat="server" Width="200px"></asp:TextBox> </td> </tr> <tr> <td width="30%" align="right" style="height: 25px"> 调号 </td> <td width="*" align="left" style="height: 25px"> <asp:DropDownList ID="DropDownLis2" runat="server" DataTextField="text " DataValueField="id " Width="206px"> </asp:DropDownList> </td> </tr> <tr> <td height="25" width="30%" align="right"> 车号 </td> <td height="25" width="*" align="left"> <asp:TextBox ID="txtCODE" runat="server" Width="200px"></asp:TextBox> </td> </tr> <tr> <td height="25" width="30%" align="right"> 钩计划号 </td> <td height="25" width="*" align="left"> <asp:TextBox ID="txtGJHH" runat="server" Width="200px"></asp:TextBox> </td> </tr> <tr> <td height="25" colspan="2"> <div align="center"> <asp:Button ID="btnAdd" runat="server" Text="· 提交 ·" OnClick="btnAdd_Click"></asp:Button> <asp:Button ID="btnCancel" runat="server" Text="· 重填 ·" OnClick="btnCancel_Click"></asp:Button> </div> </td> </tr> </table> </div> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDeleting="GridView1_RowDeleting" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" Width="70%" OnRowDataBound="GridView1_RowDataBound"> <Columns> <asp:BoundField DataField="IP" HeaderText="IP" ReadOnly="True"> <ItemStyle Width="20%" /> </asp:BoundField> <asp:TemplateField HeaderText="调号"> <EditItemTemplate> <asp:HiddenField ID="djc_text" runat="server" Value='<%# Eval("name") %>' /> <%-- <asp:DropDownList ID="DropDownLis1" runat="server" DataTextField="text " DataValueField="id " DataSource="<%# getList()%> "> </asp:DropDownList>--%> <asp:DropDownList ID="DropDownList1" runat="server" DataValueField='<%# Bind("name") %>'/> </EditItemTemplate> <ItemStyle Wrap="False" /> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Bind("name") %>'></asp:Label> </ItemTemplate> <ControlStyle Width="97%" /> </asp:TemplateField> <asp:BoundField DataField="code" HeaderText="车号" ReadOnly="True"> <ItemStyle Width="20%" /> </asp:BoundField> <asp:BoundField DataField="GJHH" HeaderText="钩计划号" ReadOnly="True"> <ItemStyle Width="20%" /> </asp:BoundField> <asp:CommandField HeaderText="编辑" ShowEditButton="True"> <ItemStyle Width="12%" /> </asp:CommandField> <asp:CommandField HeaderText="删除" ShowDeleteButton="True"> <ItemStyle Width="8%" /> </asp:CommandField> </Columns> </asp:GridView> </form> </body> </html> 后台
using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; //using System.Data.SqlClient; public partial class Uphold_add : System.Web.UI.Page { protected void btnAdd_Click(object sender, EventArgs e) { string strErr = ""; if (string.IsNullOrEmpty(this.txtIP.Text)) { strErr += "IP不能为空!//n"; } if (this.DropDownLis2.Text.Trim()=="请选择") { strErr += "请选择调号!//n"; } if (string.IsNullOrEmpty(this.txtCODE.Text)) { strErr += "车号不能为空!//n"; } if (string.IsNullOrEmpty(this.txtGJHH.Text)) { strErr += "钩计划号不能为空!//n"; } if (strErr != "") { Response.Write("<script language='javascript'>alert('" + strErr + "');</script>"); //MessageBox.Show(this, strErr); return; } string IP = this.txtIP.Text.Trim(); string NAME = this.DropDownLis2 .Text .Trim(); string CODE = this.txtCODE.Text.Trim(); string GJHH = this.txtGJHH.Text.Trim(); Maticsoft.Model.LTJ_DCJML model = new Maticsoft.Model.LTJ_DCJML(); model.ID = System.Guid.NewGuid().ToString(); model.IP = IP; model.NAME = NAME; model.CODE = CODE; model.GJHH = GJHH; Maticsoft.BLL.LTJ_DCJML bll = new Maticsoft.BLL.LTJ_DCJML(); if (CODE != "段备") { foreach (DataRow rows in bll.GetAllList().Tables[0].Rows) { if (rows["code"].ToString() == CODE) { Response.Write("<script language='javascript'>alert('车号里有重复数据/"" + CODE + "/"');</script>"); return; } } } bll.Add(model); ShowInfo(this.GridView1); cls(); } /// <summary> /// 清除 /// </summary> private void cls() { this.txtIP.Text = null; //this.txtNAME.Text = null; this.txtCODE.Text = null; this.txtGJHH.Text = null; } protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { ShowInfo(this.GridView1); getDDList(this.DropDownLis2); } } /*修改代码-显示 */ private void ShowInfo(GridView gridview) { Maticsoft.BLL.LTJ_DCJML bll = new Maticsoft.BLL.LTJ_DCJML(); gridview.DataSource = bll.GetList(" 1=1 "); gridview.DataKeyNames = new string[] { "ID" }; gridview.DataBind(); } protected void btnCancel_Click(object sender, EventArgs e) { cls(); } /// <summary> /// 删除 /// </summary> /// <param name="ID"></param> private void delInfo(string ID) { Maticsoft.BLL.LTJ_DCJML bll = new Maticsoft.BLL.LTJ_DCJML(); bll.Delete(ID); } /// <summary> /// 执行删除 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) { delInfo(this.GridView1.DataKeys[e.RowIndex].Value.ToString()); ShowInfo(this.GridView1); } /// <summary> /// 执行编辑 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) { GridView1.EditIndex = e.NewEditIndex; ShowInfo(this.GridView1); this.btnAdd.Visible = false; } protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { GridView1.EditIndex = -1; ShowInfo(this.GridView1); this.btnAdd.Visible = true; } protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { Maticsoft.Model.LTJ_DCJML model = new Maticsoft.Model.LTJ_DCJML(); model.ID = this.GridView1.DataKeys[e.RowIndex].Value.ToString(); // model.NAME = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim(); //(((DropDownList))GridView1.Rows[e.RowIndex].FindControl("ddlXL")).SelectedItem.Text; model.NAME = ((DropDownList)GridView1.Rows[e.RowIndex].FindControl("DropDownList1")).SelectedItem.Text; Maticsoft.BLL.LTJ_DCJML bll = new Maticsoft.BLL.LTJ_DCJML(); string strErr = ""; if (model.NAME == "请选择") { strErr += "请选择调号!//n"; } if (strErr != "") { // MessageBox.Show(this, strErr); Response.Write("<script language=/"javascript/">alert(/"" + strErr + "/");</script>"); return; } bll.Updateone(model); this.GridView1.EditIndex = -1; ShowInfo(this.GridView1); this.btnAdd.Visible = true; } public DataTable getList() { DataTable dt = new DataTable(); dt.Columns.Add("id ", typeof(string)); dt.Columns.Add("text ", typeof(string)); Maticsoft.DAL.GIS_DCJ dal = new Maticsoft.DAL.GIS_DCJ(); DataRow dr = dt.NewRow(); foreach (DataRow list in dal.GetList().Tables[0].Rows) { dr = dt.NewRow(); dr["id "] = list["dcj"].ToString(); dr["text "] = list["dcj"].ToString(); dt.Rows.Add(dr); } return dt; } /// <summary> /// 绑定DropDownLis数据 /// </summary> public void getDDList(DropDownList dd) { Maticsoft.DAL.GIS_DCJ dal = new Maticsoft.DAL.GIS_DCJ(); dd.Items.Clear(); dd.Items.Add(new ListItem("请选择")); foreach (DataRow list in dal.GetList().Tables[0].Rows) { dd.Items .Add(new ListItem(list["dcj"].ToString())); } } protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (((DropDownList)e.Row.FindControl("DropDownList1")) != null) { DropDownList evaluateresult = new DropDownList(); evaluateresult = (DropDownList)e.Row.FindControl("DropDownList1");//.selecteditem.text; // Response.Write("<script language=/"javascript/">alert(/"" + ((HiddenField)e.Row.FindControl("djc_text")).Value.ToString() + "/");</script>"); getDDList(evaluateresult); evaluateresult.Items.FindByText(((HiddenField)e.Row.FindControl("djc_text")).Value).Selected = true; // Response.Write("<script language=/"javascript/">alert(/"" + evaluateresult.Items[evaluateresult.SelectedIndex].Text.ToString() + "/");</script>"); } } } 源代码下载地址:http://download.csdn.net/source/535871