复选框GridView的启用和禁用 [英] CheckBox Gridview Enable and Disable

查看:394
本文介绍了复选框GridView的启用和禁用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个GridView,其中的复选框开始禁用。我想,让他们当我点击编辑按钮这也是在GridView。这里的标记

 < ASP:GridView控件ID =grd_Book code=服务器的DataSourceID =sqldatasource1
的AutoGenerateColumns =FALSEonrowcommand =grd_Book code_RowCommand1
onrowdatabound =grd_Book code_RowDataBound>
<柱体和GT;
    < ASP:BoundField的数据字段=图书code的HeaderText =图书code/>
    < ASP:BoundField的数据字段=mag_name的HeaderText =名称/>
    < ASP:BoundField的数据字段=DISPLAY_DATE的HeaderText =显示日期/>
   < ASP:的TemplateField的HeaderText =PC>
        <&ItemTemplate中GT;
            < ASP:复选框ID =CheckBox1=服务器选中='<%#的eval(82_PC)的ToString()==1?真:假%GT;'启用=FALSE/>
        < / ItemTemplate中>
     < / ASP:的TemplateField>
     < ASP:的TemplateField的HeaderText =电子阅读器>
        <&ItemTemplate中GT;
            < ASP:复选框ID =CheckBox2=服务器选中='<%#的eval(83_eReader)的ToString()==1?真:假%GT;'启用=FALSE/>
        < / ItemTemplate中>
    < / ASP:的TemplateField>
     < ASP:的TemplateField的HeaderText =平板>
        <&ItemTemplate中GT;
            < ASP:复选框ID =CheckBox3=服务器选中='<%#的eval(84_Tablet)的ToString()==1?真:假%GT;'启用=FALSE/>
        < / ItemTemplate中>
    < / ASP:的TemplateField>
     < ASP:的TemplateField的HeaderText =移动>
        <&ItemTemplate中GT;
            < ASP:复选框ID =CheckBox4=服务器选中='<%#的eval(85_Mobile)的ToString()==1?真:假%GT;'启用=FALSE/>
        < / ItemTemplate中>
    < / ASP:的TemplateField>
     < ASP:的TemplateField的HeaderText =无>
        <&ItemTemplate中GT;
            < ASP:复选框ID =CheckBox5=服务器选中='<%#的eval(86_None)的ToString()==1?真:假%GT;'启用=FALSE/>
        < / ItemTemplate中>
   < / ASP:的TemplateField>
    < ASP:CommandField中ShowEditButton =真/>
< /专栏>

和那么这里,我试图用code。基本上,当我打的编辑按钮,我想自己要启用的复选框。无论出于何种原因,该复选框不是在页面加载时全部启用备份。我刚开始时试图启用Checkbox1点击后的编辑按钮,但最终要启用所有5复选框。

 保护无效grd_Book code_RowCommand1(对象发件人,GridViewCommandEventArgs E)
    {
        如果(e.CommandName ==编辑)
        {
            INT指数= Convert.ToInt32(e.CommandArgument);            GridViewRow行= grd_Book code.Rows [指数]            复选框CHK =(复选框)row.FindControl(CheckBox1);
            chk.Enabled = TRUE;
        }
    }


解决方案

如果您希望编辑的控制会比标准控制不同,你应该使用EditItemTemplate模板。这将允许编辑行有不同的控制,价值观,等等......当行的模式变化。

例如:

 <柱体和GT;
            < ASP:的TemplateField的HeaderText =PC>
                <&ItemTemplate中GT;
                    < ASP:复选框ID =CheckBox1=服务器选中='<%#的eval(82_PC)的ToString()==1?真:假%GT;'启用=FALSE/>
                < / ItemTemplate中>
                <&EditItemTemplate的GT;
                    < ASP:复选框ID =CheckBox1=服务器选中=真启用=FALSE/>
                < / EditItemTemplate中>
            < / ASP:的TemplateField>
        < /专栏>

I have a gridview where the checkboxes start off disabled. I want to enable them when I click the edit button that's also in the gridview. Here's the markup

<asp:GridView ID="grd_Bookcode" runat="server" DataSourceID="sqldatasource1" 
autogeneratecolumns="False" onrowcommand="grd_Bookcode_RowCommand1" 
onrowdatabound="grd_Bookcode_RowDataBound">
<Columns>
    <asp:BoundField DataField="BookCode" HeaderText="Book Code"/>
    <asp:BoundField DataField="mag_name" HeaderText="Name"/>
    <asp:BoundField DataField="display_date" HeaderText="Display Date"/>
   <asp:TemplateField HeaderText = "PC">
        <ItemTemplate>
            <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Eval("82_PC").ToString() == "1" ? true:false %>' Enabled="false" />
        </ItemTemplate>
     </asp:TemplateField>
     <asp:TemplateField HeaderText="eReader">
        <ItemTemplate>
            <asp:CheckBox ID="CheckBox2" runat="server" Checked='<%# Eval("83_eReader").ToString() == "1" ? true:false %>' Enabled="false" />
        </ItemTemplate>
    </asp:TemplateField>
     <asp:TemplateField HeaderText="Tablet">
        <ItemTemplate>
            <asp:CheckBox ID="CheckBox3" runat="server" Checked='<%# Eval("84_Tablet").ToString() == "1" ? true:false %>' Enabled="false"/>
        </ItemTemplate>
    </asp:TemplateField>
     <asp:TemplateField HeaderText="Mobile">
        <ItemTemplate>
            <asp:CheckBox ID="CheckBox4" runat="server" Checked='<%# Eval("85_Mobile").ToString() == "1" ? true:false %>' Enabled="false" />
        </ItemTemplate>
    </asp:TemplateField>
     <asp:TemplateField HeaderText="None">
        <ItemTemplate>
            <asp:CheckBox ID="CheckBox5" runat="server" Checked='<%# Eval("86_None").ToString() == "1" ? true:false %>' Enabled="false" />
        </ItemTemplate>
   </asp:TemplateField>
    <asp:CommandField ShowEditButton="True" />
</Columns>

And then here's the code that I'm trying to use. Basically, when I hit the edit button, I want the checkboxes themselves to be enabled. For whatever reason, the checkbox isn't enabled at all when the page loads back up. I just started off trying to enable "Checkbox1" after the edit button is clicked but eventually want to enable all 5 checkboxes.

 protected void grd_Bookcode_RowCommand1(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Edit")
        {
            int index = Convert.ToInt32(e.CommandArgument);

            GridViewRow row = grd_Bookcode.Rows[index];

            CheckBox chk = (CheckBox)row.FindControl("CheckBox1");
            chk.Enabled = true;


        }
    }

解决方案

If you want the Edit control to be different than the standard control, you should use the "EditItemTemplate". This will allow the edit row to have different controls, values, etc... when the row's mode changes.

Example:

        <Columns>
            <asp:TemplateField HeaderText="PC">
                <ItemTemplate>
                    <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Eval("82_PC").ToString() == "1" ? true:false %>' Enabled="false" />
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:CheckBox ID="CheckBox1" runat="server" Checked="true" Enabled="false" />
                </EditItemTemplate>
            </asp:TemplateField>
        </Columns>

这篇关于复选框GridView的启用和禁用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆