C# 单击按钮时,如果gridview特定列的值低于零,则显示C警报消息

C# 单击按钮时,如果gridview特定列的值低于零,则显示C警报消息,c#,asp.net,C#,Asp.net,我试图在单击按钮时显示警报消息,条件是Gridview特定列的值低于零 现行守则如下: 受保护的无效按钮1\u单击对象发送者,事件参数e { { var countForZero=来自gvEmployeeDetails.Rows中的GridViewRow gr 其中gr.FindControlLiteral4作为Label.Text.Equals0 选择gr.ToList; 如果countForZero>0 { MessageBox MessageBox弹出警报!!!; } BindGrid;

我试图在单击按钮时显示警报消息,条件是Gridview特定列的值低于零

现行守则如下:

受保护的无效按钮1\u单击对象发送者,事件参数e { { var countForZero=来自gvEmployeeDetails.Rows中的GridViewRow gr 其中gr.FindControlLiteral4作为Label.Text.Equals0 选择gr.ToList; 如果countForZero>0 { MessageBox MessageBox弹出警报!!!; } BindGrid; } } 私有void Messageboxstring消息 { 标签lblMessageBox=新标签; lblMessageBox.Text= +环境新线+ 窗口。警报“+消息+”; Page.Controls.AddlblMessageBox; }
您必须将javascript单击事件绑定到按钮。例如,您可以使用Button1.Attributes.Add-in页面加载,请参见以下方法:

如果按钮在GridView中,则必须将单击事件绑定到每行的按钮,您可以在事件中执行此操作

基于OP中的编辑进行编辑,而不是在标签文本中添加脚本标记。如果您注册了脚本标记,则单击按钮后,将在页面加载时执行脚本标记

private void Messagebox(string Message)
{
    String csname1 = "GridButtonScript";        
    Type cstype = this.GetType();    
    // Get a ClientScriptManager reference from the Page class.
    ClientScriptManager cs = Page.ClientScript;

    // Check to see if the startup script is already registered.
    //if (!cs.IsStartupScriptRegistered(cstype, csname1)) //You may or may not need this condition.
    {
      String cstext1 = "alert('" + Message + "');";
      cs.RegisterStartupScript(cstype, csname1, cstext1, true);
    }    
}

是的,它正在使用更新的。我能看到我的成绩

hi按钮不在gridview中。尝试了以下操作,但出现了错误,您遇到了什么错误,我根据您问题中的更改更新了我的答案。您好,这是我的当前代码。我应该保持不变并添加你的还是?用我的替换你的Messagebox函数,如果你没有得到预期的结果,那么调试并让我知道发生了什么。
void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {    
        Button Button1 = (Button)e.Row.FindControls("Button1");
        //Similarly get the elements from GridView as I go button and see if criteria need alert on button and bind event.
       if(conditionBasedOnOtherColumnValues)
            Button1.Attributes.Add("onclick",  "window.alert('" + Message + "')"; 
    } 
}
private void Messagebox(string Message)
{
    String csname1 = "GridButtonScript";        
    Type cstype = this.GetType();    
    // Get a ClientScriptManager reference from the Page class.
    ClientScriptManager cs = Page.ClientScript;

    // Check to see if the startup script is already registered.
    //if (!cs.IsStartupScriptRegistered(cstype, csname1)) //You may or may not need this condition.
    {
      String cstext1 = "alert('" + Message + "');";
      cs.RegisterStartupScript(cstype, csname1, cstext1, true);
    }    
}