在网格中,我有以下列
<Columns>
<asp:TemplateField HeaderText="select to pay">
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" OnCheckedChanged="chkSelect_OnCheckedChanged" AutoPostBack="true" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Reference" HeaderText="Invoice" HeaderStyle-HorizontalAlign="Center"
ItemStyle-HorizontalAlign="Center">
</asp:BoundField>
<asp:BoundField DataField="ChargedDate" HeaderText="Date of charge" HeaderStyle-HorizontalAlign="Center"
ItemStyle-HorizontalAlign="Center" DataFormatString="{0:MM-dd-yyyy}">
</asp:BoundField>
<asp:BoundField DataField="Amount" HeaderText="Amount" HeaderStyle- HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center">
</asp:BoundField>
<asp:TemplateField HeaderText="Amount applied">
<ItemTemplate>
<asp:TextBox ID="txtPayAmount" Width="80px" runat="server" AutoPostBack="true" />
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
</Columns>
请告诉我,如果选中一个复选框,则在网格中,金额值应显示在同一行的文本框中。如果我选中了多个复选框,则值应显示在相关复选框/行的文本框中。总和应显示在网格下方的文本框中。我应该在“OnSelect_CheckedChanged”事件下写什么?
答案 0 :(得分:1)
请尝试使用以下代码段。
ASPX
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" DataKeyNames="TotalAmount" ShowFooter="true">
<Columns>
<asp:TemplateField HeaderText="select to pay">
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" OnCheckedChanged="chkSelect_OnCheckedChanged"
AutoPostBack="true" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Amount">
<ItemTemplate>
<%# Eval("TotalAmount", "{0:#,##0.00}")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Amount applied">
<ItemTemplate>
<asp:TextBox ID="txtPayAmount" Width="80px" runat="server" />
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TextBox1" Width="80px" runat="server" />
</FooterTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
</Columns>
</asp:GridView>
ASPX.CS
public partial class Forum : System.Web.UI.Page
{
protected void Page_Init(object sender, System.EventArgs e)
{
GridView gv = new GridView();
gv.ID = "GridView1";
gv.AutoGenerateColumns = false;
gv.ShowFooter = true;
gv.DataKeyNames = new string[] { "TotalAmount" };
TemplateField tf = new TemplateField();
tf.ItemTemplate = new MyCustomTemplate();
gv.Columns.Add(tf);
BoundField bf = new BoundField();
bf.DataField = "TotalAmount";
gv.Columns.Add(bf);
tf = new TemplateField();
tf.ItemTemplate = new MyCustomTextTemplate();
tf.FooterTemplate = new MyCustomFooterTextTemplate();
gv.Columns.Add(tf);
this.form1.Controls.Add(gv);
}
protected void Page_Load(object sender, System.EventArgs e)
{
GridView gv = form1.FindControl("GridView1") as GridView;
if (!IsPostBack)
{
dynamic data = new[] {
new { ID = 1, Name = "Name1",TotalAmount= 10},
new { ID = 2, Name = "Name2",TotalAmount= 20},
new { ID = 3, Name = "Name3",TotalAmount= 30},
new { ID = 4, Name = "Name4",TotalAmount= 40},
new { ID = 5, Name = "Name5",TotalAmount= 50}
};
gv.DataSource = data;
gv.DataBind();
}
}
protected void Page_PreRender(object sender, System.EventArgs e)
{
}
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
dynamic data = new[] {
new { ID = 1, Name ="Name_1"},
new { ID = 2, Name = "Name_2"},
new { ID = 3, Name = "Name_3"},
new { ID = 4, Name = "Name_4"},
new { ID = 5, Name = "Name_5"}
};
}
}
public class MyCustomTemplate : ITemplate
{
public void InstantiateIn(System.Web.UI.Control container)
{
CheckBox cb = new CheckBox();
cb.ID = "chkSelect";
cb.AutoPostBack = true;
cb.CheckedChanged += new EventHandler(cb_CheckedChanged);
container.Controls.Add(cb);
}
protected void cb_CheckedChanged(object sender, EventArgs e)
{
GridViewRow row = (sender as CheckBox).NamingContainer as GridViewRow;
TextBox txtPayAmount = row.FindControl("txtPayAmount") as TextBox;
if ((sender as CheckBox).Checked)
{
txtPayAmount.Text = Convert.ToString((row.Parent.Parent as GridView).DataKeys[row.RowIndex]["TotalAmount"]);
}
else
{
txtPayAmount.Text = string.Empty;
}
int TotalAmount = 0;
foreach (GridViewRow rowv in (row.Parent.Parent as GridView).Rows)
{
CheckBox chk = rowv.FindControl("chkSelect") as CheckBox;
if (chk.Checked)
{
TotalAmount += Convert.ToInt32((row.Parent.Parent as GridView).DataKeys[rowv.RowIndex]["TotalAmount"]);
}
}
GridViewRow rowf = (row.Parent.Parent as GridView).FooterRow;
((TextBox)rowf.FindControl("TextBox1")).Text = TotalAmount.ToString();
}
}
public class MyCustomTextTemplate : ITemplate
{
public void InstantiateIn(System.Web.UI.Control container)
{
TextBox cb = new TextBox();
cb.ID = "txtPayAmount";
container.Controls.Add(cb);
}
}
public class MyCustomFooterTextTemplate : ITemplate
{
public void InstantiateIn(System.Web.UI.Control container)
{
TextBox cb = new TextBox();
cb.ID = "TextBox1";
container.Controls.Add(cb);
}
}
答案 1 :(得分:0)
你的意思是网格中有一行有一个复选框和一个文本框列?每行包含一个复选框和代码和文本框中提到的其他列值?请附上图片/截图。
答案 2 :(得分:0)
我想用javascript来做,请试试这个。
$(function () {
$('input:checkbox').click(function (e) {
calculateSum(5); // sum of 5th column
});
function calculateSum(colidx) {
total = 0.0;
$("tr:has(:checkbox:checked) td:nth-child(" + colidx + ")").each(function () {
var Tot = ($(this).text());
Tot = parseFloat(Tot.replace(/,/g, ''));
total += Tot;
});
$("#<%=txtTotalAmt.ClientID %>").val(total).toFixed(2);
}
});
答案 3 :(得分:0)
在OnCheckedChanged事件中它应该是
protected void chkSelect_OnCheckedChanged(object sender, EventArgs e)
{
String czTemp = String.Empty;
GridViewRow gr = (GridViewRow)((CheckBox)sender).Parent.Parent;
GridViewRow footer = grdDues.FooterRow;
TextBox txtAmount = (TextBox)gr.Cells[3].FindControl("txtPayAmount");
TextBox txtAmountPaid = (TextBox)footer.FindControl("txtAmountPaid");
double dbTotalAmt = Convert.ToDouble(txtAmountPaid.Text.Replace("$","").ToString());
if( ((CheckBox)sender).Checked )
{
txtAmount.Enabled = true;
czTemp = grdDues.Rows[gr.RowIndex].Cells[3].Text.ToString();
txtAmount.Text = czTemp.ToString();
dbTotalAmt = dbTotalAmt + Convert.ToDouble(czTemp);
}
else
{
txtAmount.Enabled = false;
czTemp = grdDues.Rows[gr.RowIndex].Cells[3].Text.ToString();
dbTotalAmt = dbTotalAmt - Convert.ToDouble(czTemp);
txtAmount.Text = "0.00";
}
txtAmountPaid.Text = dbTotalAmt.ToString("C");}