使用jquery在gridview中启用禁用复选框

时间:2014-03-12 15:49:27

标签: c# jquery asp.net

我遇到了一个奇怪的情况。我无法在一行中启用复选框。

目前,我在gridview中的模板字段中有一个复选框,我想在查看文档时启用它。

<asp:HyperLinkField DataNavigateUrlFormatString="/documents/{0}" DataNavigateUrlFields="document_name"
    Text="View" Target="_blank" > 
    <ItemStyle HorizontalAlign="Center" />
</asp:HyperLinkField>

<asp:TemplateField HeaderText="Complete" SortExpression="complete">
    <ItemTemplate>
        <asp:CheckBox ID="gv5cbComplete" runat="server"   Checked='<%# Convert.ToBoolean(Eval("complete")) %>'
        OnCheckedChanged="GridView5_OnCheckedChanged" AutoPostBack="True" Enabled="False" />
   </ItemTemplate>
</asp:TemplateField>

在后面的代码中,我通过rowdatabound将onclick事件添加到超链接字段。

string cbid = e.Row.Cells[7].ClientID.ToString();
        ((HyperLink)e.Row.Cells[6].Controls[0]).Attributes.Add("onclick", "enableGV5CheckBoxFunc(\"" + cbid + "\")");

我也从后面的代码中注册了一个javascript jquery函数。

// register script
        StringBuilder cstext = new StringBuilder();
        cstext.Append("<script type=\"text/javascript\">" + Environment.NewLine);
        cstext.Append("function enableGV5CheckBoxFunc(id){" + Environment.NewLine);
        cstext.Append("window.alert(id);" + Environment.NewLine);
        //cstext.Append("$(\"#\" + id).removeAttr(\"disabled\");" + Environment.NewLine);
        cstext.Append("$(\"#\" + id).prop(\"disabled\", false);" + Environment.NewLine);
        cstext.Append("}</script>");
        cs.RegisterClientScriptBlock(cstype, csname1, cstext.ToString(), false);

现在我知道这是相反的。如果启用了复选框并且在jquery中设置了.prop(disabled,true),则在单击超链接时它会禁用复选框。然而,试图做我想要的东西是行不通的。

我需要一些特定的方法吗?

1 个答案:

答案 0 :(得分:0)

非常感谢泰德!他让我朝着正确的方向前进。

我换了

string cbid = e.Row.Cells[7].Controls[0].ClientID.ToString();

string cbid = e.Row.FindControl("gv5cbComplete").ClientID.ToString();

它有效。似乎因为.NET做了一个时髦的添加

<span class="aspNetDisabled">
当您禁用复选框时,复选框周围的

会与ID混淆。

谢谢!

相关问题