显示消息框的命令字段 [英] Command field showing messagebox
问题描述
我有这样的命令字段,
< asp:CommandField ShowEditButton =trueShowDeleteButton =True ItemStyle-Width =10px/>
现在删除我想显示一个确认框,但不知道该怎么做。
抱歉,我不想使用Template字段。
也许某种程度上,我可以使用JS来显示消息,或者也可以在此代码的后面添加方法,
protected void GridView1_RowDeleting(object sender,GridViewDeleteEventArgs e)
我试过了, / b>
protected void GridView1_RowDataBound(object sender,GridViewRowEventArgs e)
{
if(e.Row.RowType = = DataControlRowType.DataRow)
{
//在这个示例中,有3个按钮,第二个是Delete按钮,这就是为什么我们使用索引2
//索引变为0 (按钮)e.Row.Cells [0] .Controls [2])。OnClientClick =按钮#1,1文字(按钮之间的空间),2按钮#2,3文字(空格) return confirm(''你确实要删除吗?');;
}
}
但是我得到的索引超出了限制,因为那里没有任何控制。来源
以下是我的GridView的完整代码我更改了列和其他文本的名称,因此不要混淆,
< asp:UpdatePanel ID =UpdatePanel1runat =server>
< ContentTemplate>
CellPadding =5OnRowDataBound =GridView1_RowDataBoundWidth =800pxAllowPaging = true
PageSize =5GridLines =HorizontalOnPageIndexChanging =GridView1_PageIndexChanging
OnRowCancelingEdit =GridView1_RowCancelingEditOnRowDeleting =GridView1_RowDeleting
OnRowUpdating =GridView1_RowUpdatingOnRowEditing =GridView1_RowEditing >
<列>
< asp:TemplateField ItemStyle-Width =8pxControlStyle-Width =8px>
< ItemTemplate>
< a href =javascript:switchViews('div<%#Eval(UserID)%>','one');>
< img id =imgdiv<%#Eval(UserID)%> alt =点击以显示/隐藏订单border =0
src =/ _ layouts / g1.png/>
< / a>
< / ItemTemplate>
< AlternatingItemTemplate>
< a href =javascript:switchViews('div<%#Eval(UserID)%>','alt');>
< img id =imgdiv<%#Eval(UserID)%> alt =点击以显示/隐藏订单border =0
src =/ _ layouts / g1.png/>
< / a>
< / AlternatingItemTemplate>
< / asp:TemplateField>
< asp:BoundField DataField =UserIDReadOnly =trueHeaderText =User IDItemStyle-Width =120px/>
< asp:BoundField DataField =NameHeaderText =User NameItemStyle-Width =350px/>
< asp:BoundField DataField =CityHeaderText =CityItemStyle-Width =800px/>
< asp:CommandField ShowEditButton =trueShowDeleteButton =TrueItemStyle-Width =10px/>
< asp:TemplateField>
< ItemTemplate>
< tr>
< td colspan =100%>
< div id =div<%#Eval(UserID)%> style =display:none; position:relative;
left:25px;>
< asp:GridView ID =GridView2runat =serverwidth =80%AutoGenerateColumns =false
DataKeyNames =TTTTEmptyDataText =There is not e。> ;
<列>
< asp:HyperLinkField HeaderText =TTTT TitleDataNavigateUrlFields =anotherfield
DataTextField =TTTTDataTextFormatString ={0:c}Target =_ blank/>
< asp:BoundField DataField =orDerDescriptionHeaderText =orDerDescriptionHtmlEncode =False/>
< /列>
< / asp:GridView>
< / div>
< / td>
< / tr>
< / ItemTemplate>
< / asp:TemplateField>
< /列>
< / asp:GridView>
< / ContentTemplate>
< / asp:UpdatePanel>
错误当我尝试回答时
< img src =https://i.stack.imgur.com/HMqqu.pngalt =在这里输入图片描述>
如果删除CommandField在您的网格视图的第二列
protected void GridView1_RowDataBound(object sender,GridViewRowEventArgs e)
{
if(e.RowType == DataControlRowType.DataRow)
{
LinkButton link =(LinkButton)e.Row.Cells [4] .Controls [2];
if(link!= null)
{
link.OnClientClick =return confirm('你确实要删除吗?');
}
}
}
I have a command field like this,
<asp:CommandField ShowEditButton="true" ShowDeleteButton="True" ItemStyle-Width="10px" />
Now on delete I want to display a confirmation box but not sure how can I do it.
sorry but I don't want to use Template field.
Maybe somehow I can use JS to display message or maybe in this code behind method,
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
I tried this,
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//In this sample, there are 3 buttons and the second one is Delete button, that's why we use the index 2
//indexing goes as 0 is button #1, 1 Literal (Space between buttons), 2 button #2, 3 Literal (Space) etc.
((Button)e.Row.Cells[0].Controls[2]).OnClientClick = "return confirm('"Do you really want to delete?');";
}
}
but I am getting index out of bound as there isn't any control there. SOURCE
Here's full code of my GridView I changed names of columns and other text so don't get confused,
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:GridView ID="GridView1" runat="server" DataKeyNames="UserID" AutoGenerateColumns="false"
CellPadding="5" OnRowDataBound="GridView1_RowDataBound" Width="800px" AllowPaging="True"
PageSize="5" GridLines="Horizontal" OnPageIndexChanging="GridView1_PageIndexChanging"
OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDeleting="GridView1_RowDeleting"
OnRowUpdating="GridView1_RowUpdating" OnRowEditing="GridView1_RowEditing">
<Columns>
<asp:TemplateField ItemStyle-Width="8px" ControlStyle-Width="8px">
<ItemTemplate>
<a href="javascript:switchViews('div<%# Eval("UserID") %>', 'one');">
<img id="imgdiv<%# Eval("UserID") %>" alt="Click to show/hide orders" border="0"
src="/_layouts/g1.png" />
</a>
</ItemTemplate>
<AlternatingItemTemplate>
<a href="javascript:switchViews('div<%# Eval("UserID") %>', 'alt');">
<img id="imgdiv<%# Eval("UserID") %>" alt="Click to show/hide orders" border="0"
src="/_layouts/g1.png" />
</a>
</AlternatingItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="UserID" ReadOnly="true" HeaderText="User ID" ItemStyle-Width="120px" />
<asp:BoundField DataField="Name" HeaderText="User Name" ItemStyle-Width="350px" />
<asp:BoundField DataField="City" HeaderText="City" ItemStyle-Width="800px" />
<asp:CommandField ShowEditButton="true" ShowDeleteButton="True" ItemStyle-Width="10px" />
<asp:TemplateField>
<ItemTemplate>
<tr>
<td colspan="100%">
<div id="div<%# Eval("UserID") %>" style="display: none; position: relative;
left: 25px;">
<asp:GridView ID="GridView2" runat="server" Width="80%" AutoGenerateColumns="false"
DataKeyNames="TTTT" EmptyDataText="There isn't e.">
<Columns>
<asp:HyperLinkField HeaderText="TTTT Title" DataNavigateUrlFields="anotherfield"
DataTextField="TTTT" DataTextFormatString="{0:c}" Target="_blank" />
<asp:BoundField DataField="orDerDescription" HeaderText="orDerDescription" HtmlEncode="False" />
</Columns>
</asp:GridView>
</div>
</td>
</tr>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
Error when I try answer
if delete CommandField is on second column of your grid view
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton link = (LinkButton)e.Row.Cells[4].Controls[2];
if (link != null)
{
link.OnClientClick = "return confirm('Do you really want to delete?')";
}
}
}
这篇关于显示消息框的命令字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!