确认删除弹出窗口并显示记录计数

发布于 2024-10-02 04:08:34 字数 1737 浏览 1 评论 0原文

一般信息:

Aspx 页面包含一个 Ascx 用户控件。在用户控件内部,转发器包含在视图内,包含在多视图内。 Asp.Net 2.0 框架/C#

详细信息:

我有一个显示记录的转发器(在 ascx 用户控件内),第一列是一个复选框。如果选中,该行将被删除。 在中继器之外,我有一个按钮可以删除所有选中的行。

一切正常,但被要求添加一个弹出的“确认删除”消息,其中包括如果用户在弹出窗口中单击“确定”则将删除的记录数。

像这样的东西: “您将要删除 8 条记录”。

目前我的按钮如下所示:

    <asp:Button ID="btnDeleteAllRecords" runat="server" Text="Delete all Checked Records" Onclick="btnDeleteAllRecords_Click" OnClientClick="javascript:GetCbCount();" />

我有这个 javascript 代码块:

<script type="text/javascript">
function GetCbCount()   
{
var cb = document.getElementById("rptrVoicemail").getElementsByTageName("input");
  var cbCount;   
for(i = 0;  i < cb.lenght; i++)   
{   
    if(cb[i].type == "checkbox")   
    {   
        if(cb[i].checked)   
        {   
            cbCount =  cbCount + 1;   
        }               
    }       
}
return confirm('You are about to delete' + cbCount + 'records.');

} 
</script>

当我单击按钮时,我得到:

错误: 'document.getElementById(...)' 为 null 或不是对象 这条线上:

var cb = document.getElementById("rptrVoicemail").getElementsByTageName("input");

为什么 JS 没有看到我的中继器?是因为它被埋在 MultiView 中吗? JS如何修正才能让弹窗显示消息中的记录数?

更新: 我将脚本更改为:

function GetCbCount(){ 
   var inpt = document.getElementById("vmDiv"); 
   var checkboxes = inpt.getElementsByTagName("input"); 
   var cbCount; 
   for(i = 0; i<checkboxes.lenght;i++){ 
      if (checkboxes[i].type == "checkbox" && checkboxes[i].checked){ 
         cbCount = cbCount + 1;
      } 
   } 
   return confirm('You are about to delete ' + cbCount + ' Voicemails.'); 
} 

General Info:

Aspx page holds an Ascx User control. Inside the User control, the Repeater is contained inside a View, contained inside a Multiview.
Asp.Net 2.0 framework / C#

Details:

I have a repeater (inside an ascx user control) that shows records, and the first column is a checkbox. If checked, that row will be deleted.
OUtside the repeater, I have a button that will deleted all rows that are checked.

Everything works fine, but have been asked to add a pop up "confirm delete" message that includes the number of records that will be deleted if the user clicks "Ok" on the pop up.

Something like:
"You are about to delete 8 records".

Currently my button looks like this:

    <asp:Button ID="btnDeleteAllRecords" runat="server" Text="Delete all Checked Records" Onclick="btnDeleteAllRecords_Click" OnClientClick="javascript:GetCbCount();" />

I have this javascript code block:

<script type="text/javascript">
function GetCbCount()   
{
var cb = document.getElementById("rptrVoicemail").getElementsByTageName("input");
  var cbCount;   
for(i = 0;  i < cb.lenght; i++)   
{   
    if(cb[i].type == "checkbox")   
    {   
        if(cb[i].checked)   
        {   
            cbCount =  cbCount + 1;   
        }               
    }       
}
return confirm('You are about to delete' + cbCount + 'records.');

} 
</script>

When I click my button I'm getting:

Error: 'document.getElementById(...)' is null or not an object
on this line:

var cb = document.getElementById("rptrVoicemail").getElementsByTageName("input");

Why is the JS not seeing my repeater? Is it because it's buried inside a MultiView? How can the JS be corrected so that the pop up will show the record count in the message?

UPDATE:
I changed the script to:

function GetCbCount(){ 
   var inpt = document.getElementById("vmDiv"); 
   var checkboxes = inpt.getElementsByTagName("input"); 
   var cbCount; 
   for(i = 0; i<checkboxes.lenght;i++){ 
      if (checkboxes[i].type == "checkbox" && checkboxes[i].checked){ 
         cbCount = cbCount + 1;
      } 
   } 
   return confirm('You are about to delete ' + cbCount + ' Voicemails.'); 
} 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

抹茶夏天i‖ 2024-10-09 04:08:34

这应该可行:

document.getElementById('<%= rptrVoicemail.ClientID %>').getElementsByTageName("input");

另一种方法是这个返回 ClientID 的小脚本。您甚至可以将其添加到包含的 JS 文件中。

function GetClientId(strid)
{
     var count=document.forms[ 0 ].length ;
     var i = 0 ;
     var eleName;
     for (i = 0 ;  i < count ;  i++ )
     {
       eleName = document.forms [ 0 ].elements[ i ].id;
       pos=eleName.indexOf( strid ) ;
       if(pos >= 0)  break;          
     }
    return eleName;
 }

找到此处

This should work:

document.getElementById('<%= rptrVoicemail.ClientID %>').getElementsByTageName("input");

Another approach is this little script that returns the ClientID. You could add it even to an included JS-file.

function GetClientId(strid)
{
     var count=document.forms[ 0 ].length ;
     var i = 0 ;
     var eleName;
     for (i = 0 ;  i < count ;  i++ )
     {
       eleName = document.forms [ 0 ].elements[ i ].id;
       pos=eleName.indexOf( strid ) ;
       if(pos >= 0)  break;          
     }
    return eleName;
 }

Found here.

ゞ记忆︶ㄣ 2024-10-09 04:08:34

如果您使用母版页或嵌套控件(在 ascx、视图等内部),框架将更改使用元素呈现的 ID。

如果您执行“查看源代码”或使用 FireBug,您可能会看到 rptrVoicemail 变成类似 ctl00_ContentPlaceHolder1_someUserControl_ctl00_multiViewID_ctl28_rptrVoicemail 的内容。

您可以使用 getElementById('<%= rptrVoicemail.ClientID %>') 获取将在客户端呈现的元素的 ID。

编辑:为了帮助调试,做这样的事情......你明白了。
<代码>
var rptr = document.getElementById('<%= rptrVoicemail.ClientID %>');
rptr.borderColor = '粉红色'; // 绘制边框以检查它是否是正确的元素

If you are using a master page or nesting controls (inside ascx, view, etc.) the framework will change the IDs that are rendered with elements.

If you do a "View Source" or use FireBug, you might see that rptrVoicemail became something like ctl00_ContentPlaceHolder1_someUserControl_ctl00_multiViewID_ctl28_rptrVoicemail.

You can use getElementById('<%= rptrVoicemail.ClientID %>') to get at the ID of the element as it would be rendered on the client.

Edit: To help debug, do something like this... you get the point.

var rptr = document.getElementById('<%= rptrVoicemail.ClientID %>');
rptr.borderColor = 'pink'; // draw a border to check it's the right element

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文