protected void CreateConfirmButton(DataGrid dg, string strCommand)
{
CreateConfirmButton(dg, strCommand, "Are you sure to delete it ? ");
}
/// <summary>
/// 给 DataGrid 控件实例中的指定 LinkButton 控件添加提交确认
/// </summary>
/// <param name="dg">DataGrid 空间实例,需要已经执行数据邦定</param>
/// <param name="strCommand">控件的命令名</param>
/// <param name="strMessage">在提交确认对话框中显示的提示信息</param>
protected void CreateConfirmButton(DataGrid dg, string strCommand, string
strMessage)
{
ControlCollection cc;
for( int i=0 ; i < dg.Items.Count ; ++i )
{
for( int j=0 ; j < dg.Items[i].Cells.Count ; ++j )
{
cc = dg.Items[i].Cells[j].Controls;
if ( cc.Count <= 0 ) continue;
if ( cc[0] is LinkButton )
{
LinkButton lnkb = (LinkButton)cc[0];
if ( lnkb.CommandName == strCommand )
{
string strScript, strClientID;
HyperLink hlnk = new HyperLink();
strScript = "javascript:__doValidatePostBack('{0}', '', '{1}')";
strClientID = dg.Items[i].Cells[j].Controls[0].ClientID;
strClientID = strClientID.Replace("__", "$_");
hlnk.NavigateUrl = String.Format(strScript, strClientID, strMessage);
hlnk.Text = lnkb.Text;
cc.Clear();
cc.Add(hlnk);
}
}
if ( cc[0] is Button )
{
Button btn = (Button)cc[0];
if ( btn.CommandName == strCommand )
{
string strScript = @"javascript:if ( confirm('{0}') ) return true
; else return false;";
strScript = String.Format(strScript, strMessage);
btn.Attributes.Add("onclick", strScript);
}
}
}
}
}
private void RegisterClientCode()
{
// Form the script to be registered at client side.
string strFormName = ((HtmlForm)this.Controls[1]).Name;
String strScript = @"<Script language=""javascript"">
function __doValidatePostBack(eventTarget, eventArgument, stringMessage
)
{
var theform;
if (window.navigator.appName.toLowerCase().indexOf(""netscape"") > -1
)
{
theform = document.forms[""" + strFormName + @"""];
}
else
{
theform = document." + strFormName + @";
}
theform.__EVENTTARGET.value = eventTarget.split(""$"").join("":"");
theform.__EVENTARGUMENT.value = eventArgument;
if ( confirm(stringMessage + ' ') )
{
theform.submit();
}
else
{
theform.__EVENTTARGET.value = """";
theform.__EVENTARGUMENT.value = """";
}
}
</script>";
if( !this.IsStartupScriptRegistered("ValidatePostBack") )
{
this.RegisterStartupScript("ValidatePostBack", strScript);
}
}