转自http://blog.csdn.net/yiqijinbu/article/details/8432099 csdn上的博客被封了,进不去,博客园中留着备份以后会有用的
在使用服务器端控件的情况下,不能在页面上直接使用在js中用confirm进行删除判断
操作方式:在页面放置两个删除按钮控件,一个是可见的,用于触发后台的confirm判断,一个是隐藏的真正删除按钮,
我们还需要一个隐藏控件,用于保存confirm的选择结果
源码如下:
aspx界面:
- <cc2:DcjetButton UseSubmitBehavior="false" OnClick="btnDelete_Click" Text="删 除" runat="server"
- ID="btnDelete" />
- <!--隐藏控件存值-->
- <cc2:DcjetButton UseSubmitBehavior="false" OnClick="btnHiddenDelete_Click" Width="0px"
- Height="0px" ID="btnHiddenDelete" runat="server" />
- <asp:HiddenField ID="hidField" runat="server" />
- <asp:HiddenField ID="hidFieldOid" runat="server" />
后台:
- /// <summary>
- /// 获取选中行的OID
- /// </summary>
- /// <returns></returns>
- private string GetSelectedOid()
- {
- string strOid = string.Empty;
- for (int i = 0; i < this.gvInfo.Rows.Count; i++)
- {
- CheckBox chk = (CheckBox)this.gvInfo.Rows[i].FindControl("chkSelected");
- if (chk.Checked == true)
- {
- strOid += chk.ToolTip + "|";
- }
- }
- if (!string.IsNullOrEmpty(strOid))
- {
- strOid = strOid.Substring(0, strOid.Length - 1);
- }
- return strOid;
- }
- /// <summary>
- /// 删除弹框
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- protected void btnDelete_Click(object sender, EventArgs e)
- {
- strOid = GetSelectedOid();
- this.hidFieldOid.Value = strOid;
- if (string.IsNullOrEmpty(strOid))
- {
- ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('请选择您要删除的[" + this.PageReportName + "]!');", true);
- return;
- }
- string js = string.Format("document.getElementById('{0}').value=confirm('确定要删除已选的记录?');document.getElementById('{1}').click();", this.hidField.ClientID, this.btnHiddenDelete.ClientID);
- ClientScript.RegisterStartupScript(GetType(), "confirm", js, true);
- }
- /// <summary>
- /// 真正的删除操作
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- protected void btnHiddenDelete_Click(object sender, EventArgs e)
- {
- if (bool.Parse(this.hidField.Value) == true)
- {
- strOid = this.hidFieldOid.Value;
- HbViewCostManageDao hbcDao = new HbViewCostManageDao(DataConstCm.PARA_PLAT_DB_TYPE, objPageParaMessage.CompanyDBConn);
- hbcDao.Delete(strOid);
- DataLoadInfo(GetModelInfo(), strSorting);
- }
- }