尝试检测网格视图的哪一行处于编辑模式,然后在选中复选框字段时调整文本框
本文关键字:然后 模式 调整 字段 复选框 编辑 文本 于编辑 视图 网格 检测 | 更新日期: 2023-09-27 18:31:08
我一直在研究几种不同的方法来实现这一目标。当前尝试使用 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来处理它。
基本上,我用模板字段和复选框替换了复选框字段。这使我可以在不处于编辑模式时保持外观相同,并且 asp.net 为我启用工作。这允许我比较 ID。然后我使用带有CheckedChange的AutoPostBack来检测帖子之间对复选框的更改,这是服务器可以做到这一点的唯一方法(这是有意义的)。