public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Panel1.Visible = false;
}
protected void Button1_Click(object sender, EventArgs e)
{
Panel1.Visible = true;
Button4.Enabled = true;
this.TextBox1.ReadOnly = false;
}
protected void Button2_Click(object sender, EventArgs e)
{
//取得修改后的值
String adminid = Request.Params["TextBox1"].ToString();
String adminname = Request.Params["TextBox2"].ToString();
String adpwd = Request.Params["TextBox3"].ToString();
String adbeizhu = Request.Params["TextBox4"].ToString(); //根据textbox1域是否为只读来判断编辑状态
//如果不只读(说明是“新增”状态)
if (!this.TextBox1.ReadOnly)
{
//置插入命令的参数
Parameter param = SqlDataSource1.InsertParameters["adminid"];
param.DefaultValue = adminid;
param = SqlDataSource1.InsertParameters["adminname"];
param.DefaultValue = adminname;
param = SqlDataSource1.InsertParameters["adpwd"];
param.DefaultValue = adpwd;
param = SqlDataSource1.InsertParameters["adbeizhu"];
param.DefaultValue = adbeizhu;
SqlDataSource1.Insert();
}
if (this.TextBox1.ReadOnly)
{
//置修改命令的参数
Parameter param =
SqlDataSource1.UpdateParameters["adminid"];
param.DefaultValue = adminid ;
param = SqlDataSource1.UpdateParameters["adminname"];
param.DefaultValue =adminname ;
param = SqlDataSource1.UpdateParameters["adpwd"];
param.DefaultValue = adpwd ;
param = SqlDataSource1.UpdateParameters["adbeizhu"];
param.DefaultValue = adbeizhu ;
SqlDataSource1.Update();
}
this.Panel1.Visible = false;
}
protected void Button4_Click(object sender, EventArgs e)
{
this.TextBox1.Text="";
this.TextBox2.Text="";
this.TextBox3.Text = "";
this.TextBox4.Text = "";
Panel1.Visible = false;
} protected void Button3_Click(object sender, EventArgs e)
{
//置删除命令的参数
String adminid = Request.Params["TextBox1"].ToString();
System.Web.UI.WebControls.Parameter param =
SqlDataSource1.DeleteParameters["adminid"];
param.DefaultValue = adminid;
//调用SqlDataSource的删除事件,删除数据库中的记录
SqlDataSource1.Delete();
//删除完成,将Panel1置为不可见
this.Panel1.Visible = false;
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
//置Panel1为可见
this.Panel1.Visible = true;
//置“删除”按钮可见。因为也许在“新增”时已经将其置为不可见。
this.Button3.Visible = true; //从GridView1的当前行取原值
GridViewRow row = GridView1.SelectedRow;
String adminid= row.Cells[0].Text;
String adminname = row.Cells[1].Text;
String adpwd= row.Cells[2].Text;
String adbeizhu = row.Cells[3].Text;
//为修改部分置初值
//textbox1为主键字段,不允许修改
this.TextBox1.ReadOnly = true ;
this.TextBox1.Text = adminid;
this.TextBox2.Text =adminname ;
this.TextBox3.Text =adpwd ;
this.TextBox4.Text = adbeizhu ;
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{ }
}
大家看一下这段代码有什么问题
页面控件有gridview1 button1是添加管理员,button2是保存,button3删除,button4是取消添加时String adminid = Request.Params["TextBox1"].ToString();出现错误未将对象引用设置到对象的实例。
删除时String adminid = Request.Params["TextBox1"].ToString();出现错误未将对象引用设置到对象的实例。
{
protected void Page_Load(object sender, EventArgs e)
{
Panel1.Visible = false;
}
protected void Button1_Click(object sender, EventArgs e)
{
Panel1.Visible = true;
Button4.Enabled = true;
this.TextBox1.ReadOnly = false;
}
protected void Button2_Click(object sender, EventArgs e)
{
//取得修改后的值
String adminid = Request.Params["TextBox1"].ToString();
String adminname = Request.Params["TextBox2"].ToString();
String adpwd = Request.Params["TextBox3"].ToString();
String adbeizhu = Request.Params["TextBox4"].ToString(); //根据textbox1域是否为只读来判断编辑状态
//如果不只读(说明是“新增”状态)
if (!this.TextBox1.ReadOnly)
{
//置插入命令的参数
Parameter param = SqlDataSource1.InsertParameters["adminid"];
param.DefaultValue = adminid;
param = SqlDataSource1.InsertParameters["adminname"];
param.DefaultValue = adminname;
param = SqlDataSource1.InsertParameters["adpwd"];
param.DefaultValue = adpwd;
param = SqlDataSource1.InsertParameters["adbeizhu"];
param.DefaultValue = adbeizhu;
SqlDataSource1.Insert();
}
if (this.TextBox1.ReadOnly)
{
//置修改命令的参数
Parameter param =
SqlDataSource1.UpdateParameters["adminid"];
param.DefaultValue = adminid ;
param = SqlDataSource1.UpdateParameters["adminname"];
param.DefaultValue =adminname ;
param = SqlDataSource1.UpdateParameters["adpwd"];
param.DefaultValue = adpwd ;
param = SqlDataSource1.UpdateParameters["adbeizhu"];
param.DefaultValue = adbeizhu ;
SqlDataSource1.Update();
}
this.Panel1.Visible = false;
}
protected void Button4_Click(object sender, EventArgs e)
{
this.TextBox1.Text="";
this.TextBox2.Text="";
this.TextBox3.Text = "";
this.TextBox4.Text = "";
Panel1.Visible = false;
} protected void Button3_Click(object sender, EventArgs e)
{
//置删除命令的参数
String adminid = Request.Params["TextBox1"].ToString();
System.Web.UI.WebControls.Parameter param =
SqlDataSource1.DeleteParameters["adminid"];
param.DefaultValue = adminid;
//调用SqlDataSource的删除事件,删除数据库中的记录
SqlDataSource1.Delete();
//删除完成,将Panel1置为不可见
this.Panel1.Visible = false;
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
//置Panel1为可见
this.Panel1.Visible = true;
//置“删除”按钮可见。因为也许在“新增”时已经将其置为不可见。
this.Button3.Visible = true; //从GridView1的当前行取原值
GridViewRow row = GridView1.SelectedRow;
String adminid= row.Cells[0].Text;
String adminname = row.Cells[1].Text;
String adpwd= row.Cells[2].Text;
String adbeizhu = row.Cells[3].Text;
//为修改部分置初值
//textbox1为主键字段,不允许修改
this.TextBox1.ReadOnly = true ;
this.TextBox1.Text = adminid;
this.TextBox2.Text =adminname ;
this.TextBox3.Text =adpwd ;
this.TextBox4.Text = adbeizhu ;
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{ }
}
大家看一下这段代码有什么问题
页面控件有gridview1 button1是添加管理员,button2是保存,button3删除,button4是取消添加时String adminid = Request.Params["TextBox1"].ToString();出现错误未将对象引用设置到对象的实例。
删除时String adminid = Request.Params["TextBox1"].ToString();出现错误未将对象引用设置到对象的实例。
解决方案 »
- asp.net 水晶报表 该报表不包含表是什么错
- asp.net callback问题
- 大虾进 求解 B/S dsoframer.ocx office中间件怎么给它双击事件
- DataGrid中显示数据的问题
- 连接对象的问题
- ASP.NET 怎样统计每页的流量
- 各位听说VS2003中有自带的MD5算法,谁知道怎么用吗?还有Web.config上传服务器的时候怎么配置是最好的呢?
- 求教高人:下载安装的问题。
- 点击一个<asp:button>后弹出一个文件选择窗口,选择文件后获取路径。这个功能怎么实现?
- 50高分请教高手,服务器端实时监控,客户端打开网页来控制服务器端的监控!
- ajax
- 如何引用百度引擎?
肯定是Request.Params["TextBox1"].ToString();为空,跟踪一下看看呢
换个方式取TESTBOX1的 值