簡體   English   中英

在選中/取消選中同一行的 CheckBox 上的 GridViewRow 中啟用 DropDownList(使用任何 c#、jquery、javascript)

[英]Enable a DropDownList in a GridViewRow on CheckBox checked/unchecked of the same row(using any of c#,jquery,javascript)

我有一個帶有 CheckBox 和 DropDownList 作為模板字段列的 GridView。 當檢查該行的復選框時,我嘗試啟用行的DropdownList。

這是我的aspx:

<asp:GridView runat="server" ID="GdvCPRetailerMap" AutoGenerateColumns="False" Font-Size="Small" CssClass="grid" BackColor="White" BorderWidth="0px" CellPadding="4" Width="100%" AllowSorting="True" SkinID="GVSalesManager" GridLines="none" AllowPaging="true" PageSize="10" PagerStyle-ForeColor="#0066cc" PagerStyle-CssClass="gvPagerCss" PagerStyle-Font-Underline="true" PagerStyle-Wrap="true" OnPageIndexChanging="GdvCPRetailerMap_PageIndexChanging" OnRowDataBound="GdvCPRetailerMap_RowDataBound">
    <Columns>
        <asp:BoundField ReadOnly="true" HeaderText="S.No" DataField="S.No." SortExpression="SNo">
            <ItemStyle HorizontalAlign="Center" Width="2%" />
            <HeaderStyle HorizontalAlign="Center" Font-Bold="true" Width="2%"/>
        </asp:BoundField>
        <asp:TemplateField ItemStyle-Width="3%" ItemStyle-HorizontalAlign="Center">
            <ItemTemplate>
                <asp:CheckBox ID="chkRow" runat="server" OnCheckedChanged="chkRow_CheckedChanged"/>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Channel Partner-1" HeaderStyle-Font-Bold="true" ItemStyle-Width="10%" ItemStyle-HorizontalAlign="Center">
            <ItemTemplate>
                <asp:DropDownList ID="ddlCP1" Enabled="false" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlCP1_SelectedIndexChanged"></asp:DropDownList>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Channel Partner-2" HeaderStyle-Font-Bold="true" ItemStyle-Width="10%" ItemStyle-HorizontalAlign="Center">
            <ItemTemplate>
                <asp:DropDownList ID="ddlCP2" Enabled="false" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlCP2_SelectedIndexChanged"></asp:DropDownList>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

我試過在我的 CheckBox OnCheckedChanged 事件中這樣做:

protected void chkRow_CheckedChanged(object sender, EventArgs e)
{
    CheckBox ck1 = (CheckBox)sender;

    GridViewRow grow =(GridViewRow)ck1.NamingContainer;
    DropDownList ddlR = (DropDownList)grow.Cells[6].FindControl("ddlCP1");

    if (ck1.Checked == true)
        ddlR.Enabled = true;
    else
        ddlR.Enabled = false;
}

但是這段代碼似乎什么也沒發生。 斷點甚至不會被擊中。

有人可以指導我在正確的方向上如何啟用 CheckBox 被選中的 GridViewRow 的 DropDownList 嗎?以及是否有任何 jquery 或 javascript 代碼來實現相同的輸出?

看起來您可能需要做的就是確保 CheckBox 導致回發。 您可以通過將屬性AutoPostBack設置為 true 來做到這一點。

<asp:CheckBox ID="chkRow" runat="server" 
    OnCheckedChanged="chkRow_CheckedChanged"
    AutoPostBack="true" />

正如AutoPostBack文檔所述:

獲取或設置一個值,該值指示 CheckBox 狀態在單擊時是否自動回發到服務器。

還:

適當的價值

類型:System.Boolean

單擊時自動將 CheckBox 控件的狀態發布到服務器,為 true; 否則為假。 默認值為假。

之后,無需對特定單元格執行 FindControl 調用。 您可以簡單地在 GridViewRow 本身上調用它。

protected void chkRow_CheckedChanged(object sender, EventArgs e)
{
    CheckBox ck1 = (CheckBox)sender;

    GridViewRow grow =(GridViewRow)ck1.NamingContainer;
    DropDownList ddlR = (DropDownList)grow.FindControl("ddlCP1");

    if (ck1.Checked == true)
        ddlR.Enabled = true;
    else
        ddlR.Enabled = false;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM