链接按钮有一些问题

发布于 2024-09-27 21:39:42 字数 4395 浏览 5 评论 0原文

我有一个 ASP.NET 应用程序,其中包含一个 gridview。该网格视图包含 4 个复选框模板列和 2 个链接按钮模板列。如果我单击第一个复选框,则两个链接按钮都应启用,否则它们应处于禁用模式。该功能运行良好。但我的问题是,在加载表单时,它会检查第一列是否被选中。如果未选中该复选框,链接按钮将处于禁用模式。但勾选此复选框后,它会启用,但没有重定向的链接。我的代码如下所示。

 protected void DGDocuments_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemIndex == -1) return;

        BindCheckBox(e.Item, "chkRead");
        BindCheckBox(e.Item, "chkCreate");
        BindCheckBox(e.Item, "chkUpdate");
        BindCheckBox(e.Item, "chkDelete");

        CheckBox chkID = (CheckBox)e.Item.FindControl("chkRead");
        if (!chkID.Checked)
        {
            LinkButton lnkPermission = (LinkButton)e.Item.FindControl("lnkFieldPermssion");
            LinkButton lnkSetRules = (LinkButton)e.Item.FindControl("lnkAddRules");

            lnkPermission.Enabled = false;
            lnkSetRules.Enabled = false;
        }
    }

在设计器页面中:

 <asp:TemplateColumn HeaderText="Read" ItemStyle-HorizontalAlign="Center">
     <ItemTemplate>
         <asp:CheckBox ID="chkRead" runat="server" Text='<%# Eval("Read") %>' onclick="javascript:EnablePermissoin(this,5,6);" />
     </ItemTemplate>
 </asp:TemplateColumn>
 <asp:TemplateColumn HeaderText="Rules" ItemStyle-HorizontalAlign="Center" ItemStyle-Font-Bold="true">
     <ItemTemplate>
         <asp:LinkButton ID="lnkAddRules" Text="Add Rules" runat="server"  CommandName="cmdSetRules"  />
     </ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Field Permission" ItemStyle-HorizontalAlign="Center" ItemStyle-Font-Bold="true">
    <ItemTemplate>
        <asp:LinkButton ID="lnkFieldPermssion" Text="Set" runat="server" CommandName="cmdFieldPermission" />
    </ItemTemplate>
</asp:TemplateColumn>

Javascript是:

function EnablePermissoin(chkB, cellNumber1, cellNumber2) {
        var IsChecked = chkB.checked;
        if (IsChecked) {

            var cell1 = chkB.parentElement.parentElement.cells[cellNumber1];
            for (i = 0; i < cell1.childNodes.length; i++) {
                if (cell1.childNodes[i].tagName == "A") {
                    cell1.childNodes[i].disabled = false;

                }
            }
            var cell2 = chkB.parentElement.parentElement.cells[cellNumber2];
            for (i = 0; i < cell2.childNodes.length; i++) {
                if (cell2.childNodes[i].tagName == "A") {
                    cell2.childNodes[i].disabled = false;
                }

            }

        }
        else {
            var cell1 = chkB.parentElement.parentElement.cells[cellNumber1];
            for (i = 0; i < cell1.childNodes.length; i++) {
                if (cell1.childNodes[i].tagName == "A") {

                    cell1.childNodes[i].disabled = true;
                }

            }
            var cell2 = chkB.parentElement.parentElement.cells[cellNumber2];
            for (i = 0; i < cell2.childNodes.length; i++) {
                if (cell2.childNodes[i].tagName == "A") {

                    cell2.childNodes[i].disabled = true;
                }
            }
        }
    }

这是从浏览器的查看源代码获得的代码,没有禁用表单加载时的链接按钮:

<td align="center" style="font-weight:bold;">
    <a id="DGDocuments_ctl23_lnkAddRules" href="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;DGDocuments$ctl23$lnkAddRules&quot;, &quot;&quot;, true, &quot;&quot;, &quot;&quot;, false, true))">Add Rules</a>
</td><td align="center" style="font-weight:bold;">
    <a id="DGDocuments_ctl23_lnkFieldPermssion" href="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;DGDocuments$ctl23$lnkFieldPermssion&quot;, &quot;&quot;, true, &quot;&quot;, &quot;&quot;, false, true))">Set</a>
</td>

如果我在加载时禁用链接按钮,这将是从视图源代码获得的代码:

<td align="center" style="font-weight:bold;">
    <a id="DGDocuments_ctl23_lnkAddRules" disabled="disabled">Add Rules</a>
</td><td align="center" style="font-weight:bold;">
    <a id="DGDocuments_ctl23_lnkFieldPermssion" disabled="disabled">Set</a>
</td>

请帮忙我来解决这个问题。提前致谢。

I have one ASP.NET application which includes one gridview. This gridview contains 4 template columns of checkboxes and 2 template columns of link buttons. If I click on the first checkbox, then both of the link buttons should be enabled, otherwise they should be in disabled mode. This functionality is working fine. But my problem is, at the time of form loading, it will check whether the first column is checked or not. If the checkbox is not checked, the link buttons will be in disabled mode. But after the checking of this checkbox, it will enabled, but there is no link to redirect. My code is shown below.

 protected void DGDocuments_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemIndex == -1) return;

        BindCheckBox(e.Item, "chkRead");
        BindCheckBox(e.Item, "chkCreate");
        BindCheckBox(e.Item, "chkUpdate");
        BindCheckBox(e.Item, "chkDelete");

        CheckBox chkID = (CheckBox)e.Item.FindControl("chkRead");
        if (!chkID.Checked)
        {
            LinkButton lnkPermission = (LinkButton)e.Item.FindControl("lnkFieldPermssion");
            LinkButton lnkSetRules = (LinkButton)e.Item.FindControl("lnkAddRules");

            lnkPermission.Enabled = false;
            lnkSetRules.Enabled = false;
        }
    }

In designer page:

 <asp:TemplateColumn HeaderText="Read" ItemStyle-HorizontalAlign="Center">
     <ItemTemplate>
         <asp:CheckBox ID="chkRead" runat="server" Text='<%# Eval("Read") %>' onclick="javascript:EnablePermissoin(this,5,6);" />
     </ItemTemplate>
 </asp:TemplateColumn>
 <asp:TemplateColumn HeaderText="Rules" ItemStyle-HorizontalAlign="Center" ItemStyle-Font-Bold="true">
     <ItemTemplate>
         <asp:LinkButton ID="lnkAddRules" Text="Add Rules" runat="server"  CommandName="cmdSetRules"  />
     </ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Field Permission" ItemStyle-HorizontalAlign="Center" ItemStyle-Font-Bold="true">
    <ItemTemplate>
        <asp:LinkButton ID="lnkFieldPermssion" Text="Set" runat="server" CommandName="cmdFieldPermission" />
    </ItemTemplate>
</asp:TemplateColumn>

Javascript is:

function EnablePermissoin(chkB, cellNumber1, cellNumber2) {
        var IsChecked = chkB.checked;
        if (IsChecked) {

            var cell1 = chkB.parentElement.parentElement.cells[cellNumber1];
            for (i = 0; i < cell1.childNodes.length; i++) {
                if (cell1.childNodes[i].tagName == "A") {
                    cell1.childNodes[i].disabled = false;

                }
            }
            var cell2 = chkB.parentElement.parentElement.cells[cellNumber2];
            for (i = 0; i < cell2.childNodes.length; i++) {
                if (cell2.childNodes[i].tagName == "A") {
                    cell2.childNodes[i].disabled = false;
                }

            }

        }
        else {
            var cell1 = chkB.parentElement.parentElement.cells[cellNumber1];
            for (i = 0; i < cell1.childNodes.length; i++) {
                if (cell1.childNodes[i].tagName == "A") {

                    cell1.childNodes[i].disabled = true;
                }

            }
            var cell2 = chkB.parentElement.parentElement.cells[cellNumber2];
            for (i = 0; i < cell2.childNodes.length; i++) {
                if (cell2.childNodes[i].tagName == "A") {

                    cell2.childNodes[i].disabled = true;
                }
            }
        }
    }

This is the code obtained from view source of the browser, without disabling the link button on form loading:

<td align="center" style="font-weight:bold;">
    <a id="DGDocuments_ctl23_lnkAddRules" href="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("DGDocuments$ctl23$lnkAddRules", "", true, "", "", false, true))">Add Rules</a>
</td><td align="center" style="font-weight:bold;">
    <a id="DGDocuments_ctl23_lnkFieldPermssion" href="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("DGDocuments$ctl23$lnkFieldPermssion", "", true, "", "", false, true))">Set</a>
</td>

If I disable the link button on loading, this will be the code obtained from the view source:

<td align="center" style="font-weight:bold;">
    <a id="DGDocuments_ctl23_lnkAddRules" disabled="disabled">Add Rules</a>
</td><td align="center" style="font-weight:bold;">
    <a id="DGDocuments_ctl23_lnkFieldPermssion" disabled="disabled">Set</a>
</td>

Please help me to solve this. Thanks in advance.

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

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

发布评论

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

评论(2

饮湿 2024-10-04 21:39:42

看起来,当您禁用 LinkBut​​ton 服务器端时,它不会为 a 标记生成 onclick 事件处理程序。因此,一旦您通过 JavaScript 启用 LinkBut​​ton,它就不知道如何回发。我建议要么正常渲染 LinkBut​​ton,然后通过 JavaScript 禁用它,要么将复选框的 AutoPostback 设置为 True 并在服务器端启用。

It looks like when you disable the LinkButton server-side, it doesn't generate the onclick event handler for the a tag. So, once you enable the LinkButton through JavaScript, it doesn't know how to post back. I would suggest either rendering the LinkButton normally and then disabling it through JavaScript or setting AutoPostback to True for the checkbox and do the enabling server-side.

绝對不後悔。 2024-10-04 21:39:42

You are going to have to reconsider your solution. LinkButtons simply generate an <A> tag in the HTML. An <A> tag cannot be "disabled", so when you set a LinkButton to be Disabled, ASP.NET removes the HREF from the tag so that clicking it does nothing. I should point out that your JavaScript for disabling the <A> tag does not work - it makes the <A> tag look disabled, but it is still clickable.

For this to work client side, you will need your JavaScript function to add and remove the HREF from the <A> tag. Other options include doing everything server side, so that ASP.NET handles the removal and addition of the HREF, or switching to a different control, such as a regular asp:Button, which can be enabled and disabled.

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