簡體   English   中英

嘗試檢測gridview的哪一行處於編輯模式,然后在選中復選框字段時調整文本框

[英]Trying to detect which row of gridview is in edit mode, then when a checkboxfield is checked adjust a textbox

我一直在研究實現此目的的幾種不同方法。 當前的一個嘗試使用jquery。

//Find each edit link
var chks = $('#ContentPlaceHolder1_GVJobList').find('a');
//For each edit link bind click with function
$.each(chks, function () { $(this).on('click', function () {
    // trying to get the calling element
    var $self = $(this);
    // trying to get the parent(cell) then parent(row) then find the checkbox
    alert($($self).parent().parent().find('input:checkbox').val());
}) });

一段時間以來一直是一個問題,因此將不勝感激。 我願意使用任何可以實現這一目標的方法。

<asp:GridView ID="GVJobList" runat="server" AutoGenerateColumns="False" DataSourceID="SDSJobsDetails" OnRowCommand="GV_JobList_RowCommand" DataKeyNames="InvoiceID">
        <Columns>
            <asp:TemplateField HeaderText="Parts/Services">
                <ItemTemplate>
                    <asp:Button ID="B_EditJob" runat="server" CommandName="EditJob" Text="update" CommandArgument='<%# Bind("InvoiceID") %>' CausesValidation="False" UseSubmitBehavior="False" />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:CommandField ShowEditButton="True" />
            <asp:BoundField DataField="InvoiceID" HeaderText="InvoiceID" InsertVisible="False" ReadOnly="True" SortExpression="InvoiceID" />
            <asp:BoundField DataField="FK_CustomerID" HeaderText="FK_CustomerID" SortExpression="FK_CustomerID" />
            <asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
            <asp:BoundField DataField="TotalCostExGST" HeaderText="TotalCostExGST" SortExpression="TotalCostExGST" />
            <asp:BoundField DataField="TotalGST" HeaderText="TotalGST" SortExpression="TotalGST" />
            <asp:BoundField DataField="TotalCost" HeaderText="TotalCost" SortExpression="TotalCost" />
            <asp:BoundField DataField="AmountPaid" HeaderText="AmountPaid" SortExpression="AmountPaid" />
            <asp:BoundField DataField="Date_Booked" HeaderText="Date_Booked" SortExpression="Date_Booked" />
            <asp:BoundField DataField="Date_Time_Scheduled" HeaderText="Date_Time_Scheduled" SortExpression="Date_Time_Scheduled" />
            <asp:BoundField DataField="Date_Completed" HeaderText="Date_Completed" SortExpression="Date_Completed" />
            <asp:BoundField DataField="Date_Paid" HeaderText="Date_Paid" SortExpression="Date_Paid" />
            <asp:CheckBoxField DataField="completed" HeaderText="completed" SortExpression="completed" />
            <asp:CheckBoxField DataField="PaidInFull" HeaderText="PaidInFull" SortExpression="PaidInFull" />
            <asp:CheckBoxField DataField="PartPaid" HeaderText="PartPaid" SortExpression="PartPaid" />
            <asp:TemplateField HeaderText="Invoice">
                <ItemTemplate>
                    <asp:Button ID="B_CreateInvoice" runat="server" CommandName="CreateInvoice" Text="Create Invoice" CommandArgument='<%# Bind("InvoiceID") %>'  CausesValidation="False" UseSubmitBehavior="False" />
                    <asp:Button ID="B_ViewInvoice" runat="server" CommandName="ViewInvoice" Text="View Invoice" CommandArgument='<%# Bind("InvoiceID") %>'  CausesValidation="False" UseSubmitBehavior="False" />
                    <asp:Button ID="B_SendInvoice" runat="server" CommandName="SendInvoice" Text="eMail Invoice" CommandArgument='<%# Bind("InvoiceID") %>'  CausesValidation="False" UseSubmitBehavior="False" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

因此,DJ KRAZE的建議沒有足夠的信息對我有用。 但是我發現一個關鍵詞組合,谷歌放棄了部分答案。

protected void chkChanged(object sender, EventArgs e)
{
    GridViewRow row = (GridViewRow) (sender as CheckBox).Parent.Parent;
    CheckBox chk = (sender as CheckBox);
    TextBox comp = row.Cells[11].Controls[0] as TextBox;
    TextBox totalAmt = row.Cells[7].Controls[0] as TextBox;
    TextBox paid = row.Cells[8].Controls[0] as TextBox;
    TextBox datePaid = row.Cells[12].Controls[0] as TextBox;

    if (chk.ID.Equals("CB_Completed") && chk.Checked)
    {
        comp.Text = DateTime.Now.ToString();
    }
    else if (chk.ID.Equals("CB_Completed") && !chk.Checked)
    {
        comp.Text = "";
    }

    if (chk.ID.Equals("CB_PaidInFull") && chk.Checked)
    {
       datePaid.Text = DateTime.Now.ToString();
       paid.Text = totalAmt.Text;
    }
    else if (chk.ID.Equals("CB_PaidInFull") && !chk.Checked)
    {
        datePaid.Text = "";
        paid.Text = "";
    }
}

和加價

<asp:UpdatePanel>
    <ContentTemplate>
        <asp:GridView ID="GVJobList" runat="server" AutoGenerateColumns="False" DataSourceID="SDSJobsDetails" OnRowCommand="GV_JobList_RowCommand" DataKeyNames="InvoiceID">
            <Columns>
                <asp:TemplateField HeaderText="Parts/Services">
                    <ItemTemplate>
                        <asp:Button ID="B_EditJob" runat="server" CommandName="EditJob" Text="update" CommandArgument='<%# Bind("InvoiceID") %>' CausesValidation="False" UseSubmitBehavior="False" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:CommandField ShowEditButton="True" />
                <asp:BoundField DataField="InvoiceID" HeaderText="InvoiceID" InsertVisible="False" ReadOnly="True" SortExpression="InvoiceID" />
                <asp:BoundField DataField="FK_CustomerID" HeaderText="FK_CustomerID" SortExpression="FK_CustomerID" />
                <asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
                <asp:BoundField DataField="TotalCostExGST" HeaderText="TotalCostExGST" SortExpression="TotalCostExGST" />
                <asp:BoundField DataField="TotalGST" HeaderText="TotalGST" SortExpression="TotalGST" />
                <asp:BoundField DataField="TotalCost" HeaderText="TotalCost" SortExpression="TotalCost" />
                <asp:BoundField DataField="AmountPaid" HeaderText="AmountPaid" SortExpression="AmountPaid" />
                <asp:BoundField DataField="Date_Booked" HeaderText="Date_Booked" SortExpression="Date_Booked" />
                <asp:BoundField DataField="Date_Time_Scheduled" HeaderText="Date_Time_Scheduled" SortExpression="Date_Time_Scheduled" />
                <asp:BoundField DataField="Date_Completed" HeaderText="Date_Completed" SortExpression="Date_Completed" />
                <asp:BoundField DataField="Date_Paid" HeaderText="Date_Paid" SortExpression="Date_Paid" />
                <asp:TemplateField HeaderText="Completed">
                    <ItemTemplate>
                        <asp:CheckBox ID="CB_Completed" Checked='<%# Bind("completed") %>' runat="server" Enabled="False" />
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:CheckBox ID="CB_Completed" Checked='<%# Bind("completed") %>' CommandName="comp" runat="server" Enabled="True" OnCheckedChanged="chkChanged" AutoPostBack="True" />
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="PaidInFull">
                    <ItemTemplate>
                        <asp:CheckBox ID="CB_PaidInFull" Checked='<%# Bind("PaidInFull") %>' runat="server" Enabled="False" />
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:CheckBox ID="CB_PaidInFull" Checked='<%# Bind("PaidInFull") %>' CommandName="full" runat="server" Enabled="True" OnCheckedChanged="chkChanged" AutoPostBack="True" />
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="PartPaid">
                    <ItemTemplate>
                        <asp:CheckBox ID="CB_PartPaid" Checked='<%# Bind("PartPaid") %>' runat="server" Enabled="False" />
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:CheckBox ID="CB_PartPaid" Checked='<%# Bind("PartPaid") %>' CommandName="part" runat="server" Enabled="True" OnCheckedChanged="chkChanged" AutoPostBack="True" />
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Invoice">
                    <ItemTemplate>
                        <asp:Button ID="B_CreateInvoice" runat="server" CommandName="CreateInvoice" Text="Create Invoice" CommandArgument='<%# Bind("InvoiceID") %>' CausesValidation="False" UseSubmitBehavior="False" />
                        <asp:Button ID="B_ViewInvoice" runat="server" CommandName="ViewInvoice" Text="View Invoice" CommandArgument='<%# Bind("InvoiceID") %>' CausesValidation="False" UseSubmitBehavior="False" />
                        <asp:Button ID="B_SendInvoice" runat="server" CommandName="SendInvoice" Text="eMail Invoice" CommandArgument='<%# Bind("InvoiceID") %>' CausesValidation="False" UseSubmitBehavior="False" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </ContentTemplate>
</asp:UpdatePanel>

這意味着有一些到服務器的調用,有時它可能很慢,但是可以完成工作。 也許有一天我會重新審視整個事情,然后嘗試使用javascript或jquery對其進行處理。

基本上,我用模板字段和復選框替換了checkboxfield。 這使我在不處於編輯模式時仍能保持外觀不變,而asp.net可以為我完成啟用工作。 這使我可以比較ID。 然后,我將AutoPostBack與CheckedChange一起使用來檢測帖子之間復選框的更改,這是服務器可以做到的唯一方法(這很有意義)。

暫無
暫無

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

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