C# 如何将网格视图的单元格值传递给消息框
有人能帮我吗!当用户单击按钮时,我正在gridview中向用户显示角色和资格记录,在gridview中,我为每一行生成了“删除”按钮。当用户单击“删除”按钮时,我希望将角色和资格单元格值传递到消息框或弹出窗口。 (Iam使用visualstudio 2010和Dotnet 3.5) 我的c#代码:C# 如何将网格视图的单元格值传递给消息框,c#,gridview,C#,Gridview,有人能帮我吗!当用户单击按钮时,我正在gridview中向用户显示角色和资格记录,在gridview中,我为每一行生成了“删除”按钮。当用户单击“删除”按钮时,我希望将角色和资格单元格值传递到消息框或弹出窗口。 (Iam使用visualstudio 2010和Dotnet 3.5) 我的c#代码: protectedvoid OnRowDelete\u grid\u admin(对象发送方,GridViewDeleteEventArgs e) { TableCell cell=grid_admi
protectedvoid OnRowDelete\u grid\u admin(对象发送方,GridViewDeleteEventArgs e)
{
TableCell cell=grid_admin.Rows[e.RowIndex].Cells[0];
字符串CellValue=grid\u admin.SelectedRow.Cells[0]。文本;
字符串CellValue2=grid\u admin.SelectedRow.Cells[1]。文本;
写入(“警报('您确定要删除'+CellValue+Cellvalue2');
}
试试这个
string CellValue=grid_admin.Rows[e.RowIndex].Cells[0].Value;
试试这个
string CellValue=grid_admin.Rows[e.RowIndex].Cells[0].Value;
我认为您的删除控件是一个链接按钮? 那么这可能会起作用:
LinkButton lb = (LinkButton)e.CommandSource;
GridViewRow gvr = (GridViewRow)lb.NamingContainer;
Label lbl1 = gvr.FindControl("NAMEOFLABEL1") as Label;
Label lbl2 = gvr.FindControl("NAMEOFLABEL2") as Label;
Response.Write("<script>alert('Are you sure to delete' + lbl1.Text + lbl2.Text)</script>");
LinkButton lb=(LinkButton)e.CommandSource;
GridViewRow gvr=(GridViewRow)lb.NamingContainer;
标签lbl1=gvr.FindControl(“NAMEOFLABEL1”)作为标签;
标签lbl2=gvr.FindControl(“NAMEOFLABEL2”)作为标签;
Response.Write(“警报('您确定要删除'+lbl1.Text+lbl2.Text');
我认为您的删除控件是一个链接按钮?
那么这可能会起作用:
LinkButton lb = (LinkButton)e.CommandSource;
GridViewRow gvr = (GridViewRow)lb.NamingContainer;
Label lbl1 = gvr.FindControl("NAMEOFLABEL1") as Label;
Label lbl2 = gvr.FindControl("NAMEOFLABEL2") as Label;
Response.Write("<script>alert('Are you sure to delete' + lbl1.Text + lbl2.Text)</script>");
LinkButton lb=(LinkButton)e.CommandSource;
GridViewRow gvr=(GridViewRow)lb.NamingContainer;
标签lbl1=gvr.FindControl(“NAMEOFLABEL1”)作为标签;
标签lbl2=gvr.FindControl(“NAMEOFLABEL2”)作为标签;
Response.Write(“警报('您确定要删除'+lbl1.Text+lbl2.Text');
这是我执行的代码!根据建议的答案,我有这个代码
protected void OnRowDeleting(object sender, GridViewDeleteEventArgs e)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "alertmessage", "javascript:alert('Are You sure to Delete EmpId:" + grid_admin.Rows[e.RowIndex].Cells[2].Text + " from the Role" + grid_admin.Rows[e.RowIndex].Cells[1].Text +"')", true);
}
这是我执行的代码!根据建议的答案,我有这个代码
protected void OnRowDeleting(object sender, GridViewDeleteEventArgs e)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "alertmessage", "javascript:alert('Are You sure to Delete EmpId:" + grid_admin.Rows[e.RowIndex].Cells[2].Text + " from the Role" + grid_admin.Rows[e.RowIndex].Cells[1].Text +"')", true);
}
TableCell cell=grid_admin.Rows[e.RowIndex].Cells[0];
字符串CellValue=cell.Text;
TableCell cell1=grid_admin.Rows[e.RowIndex].Cells[1];
字符串CellValue2=cell1.text;
写入(“警报('您确定要删除'+CellValue+Cellvalue2');
TableCell cell=grid_admin.Rows[e.RowIndex].Cells[0];
字符串CellValue=cell.Text;
TableCell cell1=grid_admin.Rows[e.RowIndex].Cells[1];
字符串CellValue2=cell1.text;
写入(“警报('您确定要删除'+CellValue+Cellvalue2');
谢谢你的回答!但是我没有在gridview中使用任何控件(标签),我想获取单元格值谢谢!但是我没有在gridview中使用任何控件(标签)。我想获取单元格值。我尝试过,但“CellValue”为空!错误“Microsoft JScript运行时错误:'CellValue'未定义”您的错误是Javascript错误,而不是C#错误!!!运行Chrome浏览器,然后检查!!我试过了,但是“CellValue”是空的!错误“Microsoft JScript运行时错误:'CellValue'未定义”您的错误是Javascript错误,而不是C#错误!!!运行Chrome浏览器,然后检查!!你能发布你的gridview吗?你能发布你的gridview吗?你发送的代码也在运行,谢谢!但是如何在屏幕上有两个按钮呢Alert@Hearty两个按钮?很抱歉,您能否详细说明您的要求。是否希望用户选择“是”或“否”来删除单元格值?是!我想我必须写确认而不是警报,这是一个很好的例子,但在这种情况下,用户使用RowCommand
事件来删除记录。您发送的代码也在运行,谢谢!但是如何在屏幕上有两个按钮呢Alert@Hearty两个按钮?很抱歉,您能否详细说明您的要求。是否希望用户选择“是”或“否”来删除单元格值?是!我想我必须写确认而不是警报,这是一个很好的例子,但在这种情况下,用户使用RowCommand
事件来删除记录。