链接按钮有一些问题
我有一个 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("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>
如果我在加载时禁用链接按钮,这将是从视图源代码获得的代码:
<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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来,当您禁用 LinkButton 服务器端时,它不会为 a 标记生成 onclick 事件处理程序。因此,一旦您通过 JavaScript 启用 LinkButton,它就不知道如何回发。我建议要么正常渲染 LinkButton,然后通过 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.
您将不得不重新考虑您的解决方案。 LinkButtons 只是简单地生成一个 按钮。 HTML 中的标记。 标记无法“禁用”,因此当您将 LinkButton 设置为“禁用”时,ASP.NET 将从标记中删除 HREF,以便单击它不会执行任何操作。我应该指出的是,您的 JavaScript 用于禁用 。标签不起作用 - 它使 标签看起来已禁用,但仍然可以单击。
为了使其在客户端工作,您将需要 JavaScript 函数来添加和删除 中的 HREF。标签。其他选项包括在服务器端执行所有操作,以便 ASP.NET 处理 HREF 的删除和添加,或者切换到不同的控件,例如可以启用和禁用的常规 asp:Button。
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.