Asp.net中的Button.Enabled=false VS Button.Visible=false

探讨了在客户联系方式表单中,按钮设置为Enabled与Visible=False的区别,分析了各自的优缺点并提出了改进方案。

场境:

     客户联系方式表单,只允许添加,不允许修改,即点修改时将buttonUpdate按牛设置成Enabled=true与设置成Visible=False的区别。

 设置成Enabled后,可以通过js取消disabled,比方在 IE的地址栏中输入javascript:$("#butxx").attr("disabled",false)

 而如果设置成Visible=False后,通过 javascript:$("#xxx").append($("")); 创建一个新按钮时,如果服务器起用了事件验证,那么将报错。

 

 总结:

 Enabled有安全问题,Visiable不够友好(突然少了个按钮)

  建议在ViewState中保存状态,并且,起用ViewState 签名,而在Update事件中验证ViewState中的设置。

 

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Data.SqlClient; using DevExpress.XtraEditors; using DevExpress.XtraEditors.Controls; using System.Threading; namespace book_information { public partial class jczl_floor : Form { internal AF AF = new AF(); public menu menu; public jczl_floor(menu fmz) { InitializeComponent(); menu = fmz; } private void floor_Load(object sender, EventArgs e) { selBindw(); init(); } #region 事件区 private void btsel_Click(object sender, EventArgs e) { try { selBindw(); } catch (Exception ec) { MessageBox.Show("错误:" + ec.StackTrace, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } private void btadd_Click(object sender, EventArgs e) { try { jczl_floor_add fma = new jczl_floor_add(this); fma.Text = "新增层数"; fma.ShowDialog(); return; } catch (Exception ec) { MessageBox.Show("错误:" + ec.StackTrace, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } private void btexcel_Click(object sender, EventArgs e) { try { this.Invoke(new Action(() => this.Enabled = false)); SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.Title = "导出Excel"; saveFileDialog.Filter = "Excel文件(*.xls)|*.xls"; DialogResult dialogResult = saveFileDialog.ShowDialog(this); if (dialogResult == DialogResult.OK) { gridView1.Columns["Operation"].Visible = false; DevExpress.XtraPrinting.XlsExportOptions options = new DevExpress.XtraPrinting.XlsExportOptions(); gridControl1.ExportToXls(saveFileDialog.FileName); DevExpress.XtraEditors.XtraMessageBox.Show("保存成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } this.Invoke(new Action(() => this.Enabled = true)); } catch (Exception ec) { this.Invoke(new Action(() => this.Enabled = true)); MessageBox.Show("错误:" + ec.Message.ToString(), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } private void btn_Click(object sender, EventArgs e) { try { SimpleButton btn = sender as SimpleButton; if (btn.Name == "btfirst") { txtpageIndex.Text = "1"; } if (btn.Name == "btprevious") { if (Convert.ToInt32(txtpageIndex.Text.Trim()) - 1 > 0) txtpageIndex.Text = (Convert.ToInt32(txtpageIndex.Text.Trim()) - 1).ToString(); else txtpageIndex.Text = "1"; } if (btn.Name == "btnext") { if (Convert.ToInt32(txtpageIndex.Text.Trim()) + 1 <= Convert.ToInt32(lblpages.Text.Trim())) txtpageIndex.Text = (Convert.ToInt32(txtpageIndex.Text.Trim()) + 1).ToString(); } if (btn.Name == "btlast") { txtpageIndex.Text = lblpages.Text.Trim(); } Thread threads; threads = new Thread(selBindw); threads.IsBackground = true; threads.Start(); } catch (Exception ec) { MessageBox.Show("错误:" + ec.Message.ToString(), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } private void ButtonEdit_ButtonClick(object sender, ButtonPressedEventArgs e) { string sqlcon = AF.sqlcon; int selectedHandle; selectedHandle = this.gridView1.GetSelectedRows()[0]; if (e.Button.Caption == "编辑") { string name, id,status; name = this.gridView1.GetRowCellValue(selectedHandle, "层号").ToString().Trim(); id = this.gridView1.GetRowCellValue(selectedHandle, "编号").ToString().Trim(); status = this.gridView1.GetRowCellValue(selectedHandle, "状态").ToString().Trim(); jczl_floor_edit fmb = new jczl_floor_edit(this); fmb.Text = "信息修改"; fmb.txtname.Text = name; fmb.id.Text = id; fmb.ddlstatus.Text = status; fmb.ShowDialog(); return; } } #endregion #region 函数区 private void init() { ddlpageSize.Properties.Items.Add("10"); ddlpageSize.Properties.Items.Add("20"); ddlpageSize.Properties.Items.Add("50"); ddlpageSize.Properties.Items.Add("80"); ddlpageSize.Properties.Items.Add("100"); ddlpageSize.Properties.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor; ddlstatus.Properties.Items.Add("在用"); ddlstatus.Properties.Items.Add("停用"); ddlstatus.Properties.Items.Insert(0, "全部"); ddlstatus.SelectedIndex = 0; ddlstatus.Properties.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor; try { this.Invoke(new Action(() => { DevExpress.XtraGrid.Columns.GridColumn NewColumn = this.gridView1.Columns.AddField("Operation"); NewColumn.VisibleIndex = 0; NewColumn.Width = 100; NewColumn.UnboundType = DevExpress.Data.UnboundColumnType.String; NewColumn.Caption = "操作"; NewColumn.OptionsColumn.AllowGroup = DevExpress.Utils.DefaultBoolean.False; DevExpress.XtraEditors.Repository.RepositoryItemButtonEdit buttonEdit = new DevExpress.XtraEditors.Repository.RepositoryItemButtonEdit(); EditorButton editBtn = new EditorButton(ButtonPredefines.Glyph, "编辑"); editBtn.Appearance.ForeColor = Color.Blue; editBtn.Caption = "编辑"; buttonEdit.Buttons.Add(editBtn); buttonEdit.TextEditStyle = TextEditStyles.HideTextEditor; buttonEdit.ButtonClick += ButtonEdit_ButtonClick; this.gridControl1.RepositoryItems.Add(buttonEdit); NewColumn.ColumnEdit = buttonEdit; buttonEdit.Buttons[0].Visible = false; })); } catch (Exception ec) { MessageBox.Show("错误:" + ec.StackTrace, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } public void selBindw() { this.Invoke(new Action(() => this.Enabled = false)); if (!selBind()) { this.Invoke(new Action(() => this.Enabled = true)); MessageBox.Show("查询错误:连接服务器错误!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } this.Invoke(new Action(() => this.Enabled = true)); return; }//统一处理数据查询失败的情况 public bool selBind() { string sqlcon = AF.sqlcon; string pageSize, pageIndex; pageSize = ddlpageSize.Text.Trim(); pageIndex = txtpageIndex.Text.Trim(); string sql = "SELECT*FROM" + "(" + " SELECT TOP(" + pageSize + "*" + pageIndex + ")" + " floorid 编号,floorname 层号,case when status=1 then '在用' else '停用' end 状态,createman 创建人,createtime 创建时间,maintenanceman 维护人,maintenancetime 维护时间," + " ROW_NUMBER() OVER (ORDER BY xh) rownum,COUNT(1) OVER() AS zcount" + " FROM jczl_floor WITH(NOLOCK) WHERE 1=1"; string str = ""; sql += getWhStr(str); sql += ")temp WHERE temp.rownum>(" + pageSize + "*(" + pageIndex + "-1))"; DataTable dt = new DataTable(); if (!AF.execSql(sql, sqlcon, out dt)) { return false; } if (dt.Rows.Count > 0) getDateRow(Convert.ToInt32(dt.Rows[0]["zcount"].ToString())); else lblpages.Text = "0"; if (dt.Rows.Count == 0 && txtpageIndex.Text != "1") { txtpageIndex.Invoke(new Action(() => txtpageIndex.Text = "1")); selBind(); return true; } int gvRow = gridView1.RowCount; gridControl1.Invoke(new Action(() => gridControl1.DataSource = dt)); if (gvRow == 0)//初始化表格结构 { gridControl1.Invoke(new Action(() => gridView1.Columns[0].Width = 80));//编号 gridControl1.Invoke(new Action(() => gridView1.Columns[1].Width = 100));//层号 gridControl1.Invoke(new Action(() => gridView1.Columns[2].Width = 90));//状态 gridControl1.Invoke(new Action(() => gridView1.Columns[3].Width = 90));//创建人 gridControl1.Invoke(new Action(() => gridView1.Columns[4].Width = 140));//创建时间 gridControl1.Invoke(new Action(() => gridView1.Columns[5].Width = 90));//维护人 gridControl1.Invoke(new Action(() => gridView1.Columns[6].Width = 140));//维护时间 gridControl1.Invoke(new Action(() => gridView1.Columns[7].Visible = false)); gridControl1.Invoke(new Action(() => gridView1.Columns[8].Visible = false)); gridControl1.Invoke(new Action(() => gridView1.Columns[0].AppearanceCell.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center)); gridControl1.Invoke(new Action(() => gridView1.Columns[1].AppearanceCell.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Near)); gridControl1.Invoke(new Action(() => gridView1.Columns[2].AppearanceCell.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center)); gridControl1.Invoke(new Action(() => gridView1.Columns[3].AppearanceCell.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center)); gridControl1.Invoke(new Action(() => gridView1.Columns[4].AppearanceCell.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Far)); gridControl1.Invoke(new Action(() => gridView1.Columns[5].AppearanceCell.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center)); gridControl1.Invoke(new Action(() => gridView1.Columns[6].AppearanceCell.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Far)); for (int i = 0; i < gridView1.Columns.Count-1; i++) { gridControl1.Invoke(new Action(() => gridView1.Columns[i].OptionsColumn.AllowEdit = false)); } } return true; } private bool getDateRow(int hshl) { if (Convert.ToInt32(txtpageIndex.Text.Trim()) < 1) { txtpageIndex.Invoke(new Action(() => txtpageIndex.Text = "1")); } if (Math.Ceiling(Convert.ToDouble(hshl) / Convert.ToDouble(ddlpageSize.Text.Trim())) < Convert.ToInt32(txtpageIndex.Text.Trim())) { txtpageIndex.Invoke(new Action(() => txtpageIndex.Text = Math.Ceiling(Convert.ToDouble(hshl) / Convert.ToDouble(ddlpageSize.Text.Trim())).ToString())); selBind(); } lblpages.Invoke(new Action(() => lblpages.Text = Math.Ceiling(Convert.ToDouble(hshl) / Convert.ToDouble(ddlpageSize.Text.Trim())).ToString())); btnpageinfobar.Invoke(new Action(() => btnpageinfobar.Text = "(共" + hshl.ToString().Trim() + "条记录,每页" + ddlpageSize.Text.Trim() + "条,共" + lblpages.Text.Trim() + "页)")); return true; }//调整页码 private string getWhStr(string str) { if (txtname.Text.Trim() != "") { str += "AND floorname LIKE '%" + txtname.Text.Trim() + "%'"; } if (txtid.Text.Trim() != "") { str += "AND floorid LIKE '%" + txtid.Text.Trim() + "%'"; } int isuse; if (ddlstatus.Text.Trim() == "在用") isuse = 1; else isuse = 0; if (ddlstatus.Text.Trim() != "全部" && ddlstatus.Text.Trim() != "") { str += "AND status =" + isuse; } return str; } #endregion } } 用Ajax改写以上代码
08-15
<asp:datagrid id="grid" runat="server" Width="688px" HorizontalAlign="Center" AutoGenerateColumns="False" AllowPaging="True" PageSize="5" meta:resourcekey="gridResource1" > <ItemStyle HorizontalAlign="Left"></ItemStyle> <Columns> <asp:BoundColumn Visible="False" DataField="STAND_ID" HeaderText="STAND_ID" meta:resourcekey="BoundColumnResource1"></asp:BoundColumn> <asp:BoundColumn DataField="CUST_CODE" HeaderText="廠內代碼" meta:resourcekey="BoundColumnResource2"></asp:BoundColumn> <asp:BoundColumn DataField="CUST_NAME" HeaderText="公司名稱" meta:resourcekey="BoundColumnResource3"></asp:BoundColumn> <asp:BoundColumn DataField="VERSION_NAME" HeaderText="版稱" meta:resourcekey="BoundColumnResource4"></asp:BoundColumn> <asp:TemplateColumn HeaderText="修改" meta:resourcekey="BoundColumnResource5"> <HeaderStyle Width="30px"></HeaderStyle> <ItemTemplate> <asp:ImageButton id="BtnEdit" runat="server" ImageUrl="..\images\button_edit1.gif" CommandName="BtnEdit" meta:resourcekey="BtnEditResource1"></asp:ImageButton> </ItemTemplate> </asp:TemplateColumn> <asp:TemplateColumn HeaderText="刪除" meta:resourcekey="BoundColumnResource6"> <HeaderStyle Width="30px"></HeaderStyle> <ItemTemplate> <asp:ImageButton id="BtnDel" runat="server" CommandName="BtnDel" ImageUrl="..\images\button_del1.gif" meta:resourcekey="BtnDelResource1"></asp:ImageButton> </ItemTemplate> </asp:TemplateColumn> </Columns> <PagerStyle Visible="False"></PagerStyle> </asp:datagrid>如何加一个selectindexchanged事件,目前已经有private void grid_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e) { string _STAND_ID=e.Item.Cells[0].Text.Trim(); string _substrance=e.Item.Cells[1].Text.Trim(); if(e.CommandName=="BtnEdit") { if(Page011.Update_right==1) { Response.Redirect("gm_standard_add.aspx?state=update&standid="+_STAND_ID+""); } else { ITSC.Green.Control.MessageBox.Show(this, "No modification permission"); return; } } else if(e.CommandName=="BtnDel") { if(Page011.Delete_right==1) { ITSC.BLL.WMINV_GM_STANDARD_D bll_detail=new ITSC.BLL.WMINV_GM_STANDARD_D(); if(!bll_detail.Exists(int.Parse(_STAND_ID))) { ITSC.BLL.WMINV_GM_STANDARD_M bll=new ITSC.BLL.WMINV_GM_STANDARD_M(); bll.Delete(int.Parse(_STAND_ID)); ITSC.Green.Control.MessageBox.Show(this, "Delete successfully"); BindRow(); } else { ITSC.Green.Control.MessageBox.Show(this, "There is detailed information here. If you want to delete it, please delete the detailed information first, and then delete it"); return; } } else { ITSC.Green.Control.MessageBox.Show(this, "No deletion permission"); return; } } }事件
最新发布
11-14
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值