关于gridview 自定列不能读取数值的问题
在gridview1,自定义textbox列,在输入数值后,通过按钮保存其数值,但通过Dim t As TextBox = CType(GridView1.Rows(i).FindControl("txtOnePrice"), TextBox)获取的数值,都是空值,不知道什么原因,从昨晚折腾到现在了,请老鸟指点,感激不尽!!!
<%@ Page Language="vb" EnableViewState="true" AutoEventWireup="false" MasterPageFile="~/Site1.Master" CodeBehind="wydc.aspx.vb" Inherits="Web.wydc"
title="在线订餐" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<asp:Panel ID="Panel2" runat="server" Visible="False">
<asp:Calendar ID="Calendar1" runat="server" BackColor="#FFFFCC"
BorderColor="#FFCC66" BorderWidth="1px" DayNameFormat="Shortest"
Font-Names="Verdana" Font-Size="8pt" ForeColor="#663399" Height="200px"
ShowGridLines="True" Width="220px">
<SelectedDayStyle BackColor="#CCCCFF" Font-Bold="True" />
<SelectorStyle BackColor="#FFCC66" />
<TodayDayStyle BackColor="#FFCC66" ForeColor="White" />
<OtherMonthDayStyle ForeColor="#CC9966" />
<NextPrevStyle Font-Size="9pt" ForeColor="#FFFFCC" />
<DayHeaderStyle BackColor="#FFCC66" Font-Bold="True" Height="1px" />
<TitleStyle BackColor="#990000" Font-Bold="True" Font-Size="9pt"
ForeColor="#FFFFCC" />
</asp:Calendar>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
BackColor="White" BorderColor="#999999"
BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Vertical">
<RowStyle BackColor="#EEEEEE" ForeColor="Black" />
<Columns>
<asp:BoundField DataField="餐次名称" FooterText="预定餐次" HeaderText="预定餐次" />
<asp:BoundField DataField="开始时间" FooterText="开始时间" HeaderText="开始时间" />
<asp:BoundField DataField="结束时间" FooterText="结束时间" HeaderText="结束时间" />
<asp:BoundField DataField="订餐份数" FooterText="已订份数" HeaderText="已订份数" />
<asp:TemplateField FooterText="订餐份数" HeaderText="订餐份数">
<ItemTemplate>
<asp:TextBox ID="txtOnePrice" runat="server" Height="21px" Width="75px"
></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:ButtonField FooterText="订餐" HeaderText="订餐" Text="订餐" />
</Columns>
<FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="Gainsboro" />
</asp:GridView>
<asp:Button ID="Button2" runat="server" Text="保存订餐信息" />
</asp:Panel>
</asp:Content>
以下是vb代码
Public Partial Class wydc
Inherits System.Web.UI.Page
Public dtcc As New DataTable
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Session("loginuserbh") <> "" Then
Panel2.Visible = True
Dim xfs As New Service1 ' 这是个webservice
dtcc = xfs.select_canci(Session("loginuserbh"), IIf(Format(Calendar1.SelectedDate, "yyyy-MM-dd") = "0001-01-01", Date.Now, Format(Calendar1.SelectedDate, "yyyy-MM-dd")))
GridView1.DataSource = dtcc
GridView1.DataBind()
End If
End Sub
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button2.Click
For i = 0 To dtcc.Rows.Count - 1
dtcc.Rows(i).Item("编号") = Session("loginuserbh")
dtcc.Rows(i).Item("姓名") = Session("loginuserxm")
dtcc.Rows(i).Item("订餐日期") = IIf(Format(Calendar1.SelectedDate, "yyyy-MM-dd") = "0001-01-01", Date.Now, Format(Calendar1.SelectedDate, "yyyy-MM-dd"))
Dim t As TextBox = CType(GridView1.Rows(i).FindControl("txtOnePrice"), TextBox)
dtcc.Rows(i).Item("订餐份数") = t.Text '这里一直显示的是空值
Next
Dim xfs As New Service1
xfs.dc(dtcc)
End Sub
Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
Dim index As Integer = Convert.ToInt32(e.CommandArgument)
Dim t As TextBox = CType(GridView1.Rows(index).FindControl("txtOnePrice"), TextBox)
dtcc.Rows(index).Item("订餐份数") = t.Text
End Sub
End Class --------------------编程问答-------------------- 自定义列是你那样的吗?
参考:
--------------------编程问答-------------------- 对不起,看错了,你那样是对的。。。。 --------------------编程问答-------------------- 是因为数据绑定的原因么,那一列没绑定,还有在textbox_TextChanged事件里里面没有任何反应 --------------------编程问答--------------------
<asp:TemplateField HeaderText="编号">
<ItemTemplate>
<asp:TextBox ID="txtOnePrice" runat="server" Height="21px" Width="75px" />
</ItemTemplate>
</asp:TemplateField>
运行后的页面代码,看看textbox在不在页面里 --------------------编程问答-------------------- 怎么看? --------------------编程问答-------------------- 先跟踪一下看看FindControl有没有找到哪个textbox。 --------------------编程问答-------------------- 问题解决了,在Page_Load时数据又被重新加载了
补充:.NET技术 , ASP.NET