<asp:Content ID="ctnMain" ContentPlaceHolderID="cphMain" runat="server">
<asp:GridView ID="grvCart" runat="server" AutoGenerateColumns="False" HorizontalAlign="Center"
Width="400px" ShowFooter="True" OnDataBound="grvCart_DataBound" OnRowDataBound="grvCart_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="產品">
<ItemTemplate>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr valign="top">
<td style="width: 80px;">
<asp:Image ImageUrl=' <%# Eval("product_image") %>' ID="imgProduct" runat="server" /> </td>
<td rowspan="2">
<asp:Literal Text=' <%# Eval("product_name") %>' ID="lblProductName" runat="server"> </asp:Literal>
</td>
</tr>
<tr valign="top">
<td>
<asp:Literal ID="lblProductCode" runat="server"> </asp:Literal> </td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="數量">
<ItemStyle HorizontalAlign="Center" Width="60px" />
<ItemTemplate>
<asp:TextBox Text=' <%# Eval("product_quantity") %>' ID="txtQuantity" runat="server" MaxLength="5" Width="30px"> </asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="單價">
<ItemStyle HorizontalAlign="Right" Width="80px" />
<ItemTemplate>
<asp:Literal Text=' <%# Eval("product_unit_price") %>' ID="lblUnitPrice" runat="server"> </asp:Literal>
</ItemTemplate>
<FooterTemplate>
總金額︰
</FooterTemplate>
<FooterStyle Font-Bold="True" HorizontalAlign="Right" />
</asp:TemplateField>
<asp:TemplateField HeaderText="小計">
<ItemStyle HorizontalAlign="Right" Width="80px" />
<ItemTemplate>
<asp:Literal ID="lblSubtotal" runat="server"> </asp:Literal>
</ItemTemplate>
<FooterTemplate>
<asp:Literal ID="lblAmountTotal" runat="server"> </asp:Literal>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="刪除">
<ItemStyle HorizontalAlign="Center" Width="30px" />
<ItemTemplate>
<asp:CheckBox ID="chkDelete" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
購物籃內並沒有產品。
</EmptyDataTemplate>
<RowStyle VerticalAlign="Top" />
</asp:GridView>
<div style="text-align: right;">
<asp:Button ID="cmdRefresh" runat="server" CausesValidation="False" Text="更新購物籃" OnClick="cmdRefresh_Click" />
<asp:Button ID="cmdNext" runat="server" CausesValidation="False" Text="下一步" OnClick="cmdNext_Click" /> </div>
</asp:Content>
前台代码!( button 不是在gridview 里面。)gridview 包含三个控件:button , checkbox , textbox .
我想在button_click()事件里获取 textbox 和 checkbox 控件并修改它们的值。 protected void cmdRefresh_Click(object sender, EventArgs e)
{
DataTable DT = (DataTable)Session["shopping_cart"];
TextBox T = new TextBox();
CheckBox C = new CheckBox();
foreach (DataRow DW in DT.Rows)
{
foreach (GridViewRow row in grvCart.Rows)
{ if (row.RowType == DataControlRowType.DataRow)
{
// 一下三行代码都有问题:(获取不到控件)
//T = this.grvCart.Rows[i].FindControl("lblSubtotal") as TextBox;
T = (TextBox)row.FindControl("lblSubtotal");
if (T.Text != null)
{
<asp:GridView ID="grvCart" runat="server" AutoGenerateColumns="False" HorizontalAlign="Center"
Width="400px" ShowFooter="True" OnDataBound="grvCart_DataBound" OnRowDataBound="grvCart_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="產品">
<ItemTemplate>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr valign="top">
<td style="width: 80px;">
<asp:Image ImageUrl=' <%# Eval("product_image") %>' ID="imgProduct" runat="server" /> </td>
<td rowspan="2">
<asp:Literal Text=' <%# Eval("product_name") %>' ID="lblProductName" runat="server"> </asp:Literal>
</td>
</tr>
<tr valign="top">
<td>
<asp:Literal ID="lblProductCode" runat="server"> </asp:Literal> </td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="數量">
<ItemStyle HorizontalAlign="Center" Width="60px" />
<ItemTemplate>
<asp:TextBox Text=' <%# Eval("product_quantity") %>' ID="txtQuantity" runat="server" MaxLength="5" Width="30px"> </asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="單價">
<ItemStyle HorizontalAlign="Right" Width="80px" />
<ItemTemplate>
<asp:Literal Text=' <%# Eval("product_unit_price") %>' ID="lblUnitPrice" runat="server"> </asp:Literal>
</ItemTemplate>
<FooterTemplate>
總金額︰
</FooterTemplate>
<FooterStyle Font-Bold="True" HorizontalAlign="Right" />
</asp:TemplateField>
<asp:TemplateField HeaderText="小計">
<ItemStyle HorizontalAlign="Right" Width="80px" />
<ItemTemplate>
<asp:Literal ID="lblSubtotal" runat="server"> </asp:Literal>
</ItemTemplate>
<FooterTemplate>
<asp:Literal ID="lblAmountTotal" runat="server"> </asp:Literal>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="刪除">
<ItemStyle HorizontalAlign="Center" Width="30px" />
<ItemTemplate>
<asp:CheckBox ID="chkDelete" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
購物籃內並沒有產品。
</EmptyDataTemplate>
<RowStyle VerticalAlign="Top" />
</asp:GridView>
<div style="text-align: right;">
<asp:Button ID="cmdRefresh" runat="server" CausesValidation="False" Text="更新購物籃" OnClick="cmdRefresh_Click" />
<asp:Button ID="cmdNext" runat="server" CausesValidation="False" Text="下一步" OnClick="cmdNext_Click" /> </div>
</asp:Content>
前台代码!( button 不是在gridview 里面。)gridview 包含三个控件:button , checkbox , textbox .
我想在button_click()事件里获取 textbox 和 checkbox 控件并修改它们的值。 protected void cmdRefresh_Click(object sender, EventArgs e)
{
DataTable DT = (DataTable)Session["shopping_cart"];
TextBox T = new TextBox();
CheckBox C = new CheckBox();
foreach (DataRow DW in DT.Rows)
{
foreach (GridViewRow row in grvCart.Rows)
{ if (row.RowType == DataControlRowType.DataRow)
{
// 一下三行代码都有问题:(获取不到控件)
//T = this.grvCart.Rows[i].FindControl("lblSubtotal") as TextBox;
T = (TextBox)row.FindControl("lblSubtotal");
if (T.Text != null)
{
解决方案 »
- 在asp.net的datalist控件中如何取出商品表中的商品编号,并显示在datalist控件之外的label中?
- ASP.NET页面中调用winform界面(求详细步骤)
- 网站乱码请求解决
- 为什么我每次点击树形结构上的节点就会触发"Session_Start"事件啊?
- 还是GridView与Js的问题,多谢了!
- 修改了.cs文件后,如何让其生效?
- 在VS2005运行环境下连接Access是正常的,但是发布后直接用ISS连接,就出错
- 获取DataGrid中某行某单元格值的问题,请教?!
- 高分求vs.net 2003 中文企业版下载
- 一个关于AD的问题!!!???
- asp.net 2008 textbox只能输入数字??
- Asp.net 连接局域网sql2000数据库
依然没找到textbox ,checkbox 子控件。
protected void cmdRefresh_Click(object sender, EventArgs e)
{
DataTable DT = (DataTable)Session["shopping_cart"];
TextBox T ;
CheckBox C ;
GridViewRow GRV = (sender as Button).NamingContainer as GridViewRow;
if (GRV != null)
{
T = (TextBox)GRV.FindControl("lblSubtotal");
if (T != null)
{
foreach (DataRow DW in DT.Rows)
{
if ( GRV.RowType == DataControlRowType.DataRow)
{
if (FormatFunc.StrIsInt(T.Text))
{
DW["product_quantity"] = Convert.ToInt32(T.Text);
}红色地方判断为false ,直接跳出if()判断。
{
T = (((TextBox)row.Cells[?].Controls[1]).Text);
}
{
foreach(GridViewRow gvr in grvCart.Rows)
{
if(gvr.RowType == DataControlRowType.DataRow)
{
TextBox T = gvr.FindControl("txtQuantity") as TextBox;
if(T != null)
{
//to do...
}
}
}
}
真无语了,我最后的回复你没看啊……
C# codeprotectedvoid cmdRefresh_Click(object sender, EventArgs e)
{foreach(GridViewRow gvrin grvCart.Rows)
{if(gvr.RowType== DataControlRowType.DataRow)
{
TextBox T= gvr.FindControl("txtQuantity")as TextBox;if(T!=null)
{//to do... }
}
}
}
[/Quote]oj 兄弟,这个我也试过了。 T为null。
example:
(TextBox)(gv.Rows[int.Parse(e.CommandArgument.ToString())].Cells[2].FindControl("txbTest"));
T 依然为 null ! oj 兄。
{
DataTable DT = (DataTable)Session["shopping_cart"];
CheckBox C ; foreach (GridViewRow GRV in grvCart.Rows)
{
if (GRV.RowType == DataControlRowType.DataRow)
{
TextBox T = GRV.FindControl("lblSubtotal") as TextBox;
if (T != null)
{
foreach (DataRow DW in DT.Rows)
{ if (FormatFunc.StrIsInt(T.Text))
{
DW["product_quantity"] = Convert.ToInt32(T.Text);
}
C = (CheckBox)GRV.FindControl("checkboxid");
if (C.Checked)
{
Response.Write("aaa");
}现在的代码....
我还奇怪你怎么老说不行不行,原来类型就错了……
Literal T = GRV.FindControl("lblSubtotal") as Literal;
请各位高手批评我,特别是oj 兄,狠狠的骂....
无奈新手总是沉不住气细审代码!我知道错了。
特别感谢 ojlovecd 和 AderLee 二位!
//T = this.grvCart.Rows[i].FindControl("lblSubtotal") as TextBox;
T = (TextBox)row.FindControl("lblSubtotal");
if (T.Text != null)
{
这样查找GridView的中的控件.这应该这样Page.Master.FindControl("ContentPlaceHolder1").grvCart.Rows[i].FindControl("lblSubtotal") as TextBox;