简体   繁体   English

使用asp.net在gridview中选中复选框时更改div显示

[英]change div display while checkbox checked in gridview using asp.net

I have gridview that binded to database. 我有绑定到数据库的gridview There is checkbox in it that I want change display attribute of another div with checkbox checked value. 我有一个checkbox ,我想用checkbox选中值更改另一个div display属性。 This is my back-end code in asp.net, but it isn't work! 这是我在asp.net中的后端代码,但是不起作用! What can I do to solve it? 我该怎么解决?

protected void gv_sourceGalleryPic_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "checkPic")
    {
        CheckBox chkItem = (CheckBox)gv_sourceGalleryPic.FindControl("ck_checkGalleryPic_copyMove");

        if (chkItem.Checked == true)
        {
            div1.Style.Add(HtmlTextWriterStyle.Display, "block");
        }
    }
}

As i know, Asp.Net CheckBoxes do not have attributes named CommandName and CommandArgument . 据我所知,Asp.Net CheckBoxes没有名为CommandNameCommandArgument属性。 Therefore you can not use RowCommand event which is going to be fired when you change checked. 因此,您不能使用RowCommand事件,该事件将在您更改选中状态时触发。 You should use some simple javascript codes instead of it like example code below: 您应该使用一些简单的javascript代码,而不是下面的示例代码:

function hidediv(chk) {
        var element = document.getElementById("div1");
        if (chk.checked) 
        { element.style.display = 'none'; }
    }

And then fire this function on change event of all checkboxes like this: 然后在所有复选框的更改事件上触发此函数,如下所示:

onchange="hidediv(this);"

By the way, if you want to change display a div section only, it would be better to use html input tag instead of asp.net checkbox. 顺便说一句,如果您只想更改显示div部分,最好使用html输入标签而不是asp.net复选框。

尝试找到Div

HtmlGenericControl div1= (HtmlGenericControl )gv_sourceGalleryPic.FindControl("div1");

You can use OnCheckedChanged event for checkbox instead onRowCommand like this: 您可以将OnCheckedChanged事件用于checkbox而不是onRowCommand如下所示:

Markup 标记

<asp:GridView runat="server" ID="gv_sourceGalleryPic">
  <Columns>
    <asp:TemplateField>
      <ItemTemplate>
        <asp:CheckBox ID="ck_checkGalleryPic_copyMove" runat="server"
            OnCheckedChanged="chkBox_CheckedChanged" AutoPostBack="true" />
      </ItemTemplate>
    </asp:TemplateField>
  </Columns>
</asp:GridView>

Code behind 后面的代码

protected void chkBox_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox chk = sender as CheckBox;

        if (chk.Checked)
        {
            div1.Style.Add(HtmlTextWriterStyle.Display, "block");
        }           
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM