各位大大您好,我在gridview的最後一行另外添加了textbox,在點選儲存button時,我用gridview.rows(i).findcontrol("textbox")抓不到textbox,請問如何抓到textbox?
設計如下:
<asp:GridView ID="xxxGridView"... DataKeyNames="...">
<PagerSettings ... Mode="NumericFirstLast" />
<PagerStyle ... HorizontalAlign="Center" />
<Columns runat="server">
<asp:TemplateField HeaderText="xxx" ItemStyle-HorizontalAlign="Left" >
<HeaderStyle Width="90px" />
<ItemTemplate>
<asp:Label ID="TLabxxx" runat="server" Text='<%#Bind("...") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="xxx">
<HeaderStyle Width="200px" />
<ItemTemplate>
<asp:RadioButtonList ID="rdbtnLt1" runat="server" RepeatDirection="Horizontal">
<asp:ListItem>xx</asp:ListItem>
<asp:ListItem>xxx</asp:ListItem>
<asp:ListItem>xxxx</asp:ListItem>
</asp:RadioButtonList>
<asp:HiddenField ID="Hidxxx" runat="server" Value='<%# Bind("...") %>' />
</ItemTemplate>
</asp:TemplateField>
...
<asp:CommandField ... />
</Columns>
<HeaderStyle CssClass="..." />
...
<FooterStyle CssClass="..." />
</asp:GridView>
<asp:Button ID="butsave" runat="server" Text="儲存" />
程式碼如下
設定xxxGridView
Protected Sub setxxxGridView()
...
Dim gvr As GridViewRow = New GridViewRow(0, -1, DataControlRowType.DataRow, DataControlRowState.Normal)
Dim cell1 As TableCell = New TableCell()
Dim cell2 As TableCell = New TableCell()
Dim tb1 As TextBox = New TextBox()
tb1.ID = "textboxnotep"
tb1.Text = Hidpchknote.Value
tb1.TextMode = TextBoxMode.MultiLine
tb1.Width = 300
tb1.Height = 50
Dim cell3 As TableCell = New TableCell()
Dim tb2 As TextBox = New TextBox()
tb2.ID = "textboxnotec"
tb2.Text = Hidcchknote.Value
tb2.TextMode = TextBoxMode.MultiLine
tb2.Width = 300
tb2.Height = 50
cell1.Text = "備註"
cell2.Controls.Add(tb1)
cell3.Controls.Add(tb2)
gvr.Controls.Add(cell1)
gvr.Controls.Add(cell2)
gvr.Controls.Add(cell3)
gvr.CssClass = "GridView_B_RowStyle"
gvr.Style("text-align") = "left"
xxxGridView.Controls(0).Controls.AddAt(xxxGridView.Rows.Count + 2, gvr)
...
End Sub
設定xxxGridView的欄位存進資料庫,其中textbox抓不到
Protected Sub butsave_click(sender As Object, e As EventArgs) Handles butsave.Click
savexxxGridView(startid)
End Sub
Protected Sub savexxxGridView(ByRef startid As String)
...
If Session("CompID") = "1" Then
For m As Integer = 0 To xxxGridView.Rows.Count - 1
Using cn As New SqlConnection(ConnectString)
Dim dt As New DataTable
Dim textboxnotep As TextBox = xxxGridView.Rows(m).FindControl("textboxnotep")
'''系統執行發生錯誤:並未將物件參考設定為物件的執行個體
'''意思就是抓不到textbox
Dim Hidchktype As HiddenField = xxxGridView.Rows(m).FindControl("Hidchktype")
Dim HidTypeID As HiddenField = xxxGridView.Rows(m).FindControl("HidTypeID")
If textboxnotep.Text <> "" Then
sql2 = "Select chktype from DailyCheckNote where 1=1"
SqlDA2 = New SqlDataAdapter(sql2, cn)
If startid <> "" Then
sql2 += " and startid= @startid" SqlDA2.SelectCommand.Parameters.Add("@startid", SqlDbType.NVarChar) SqlDA2.SelectCommand.Parameters("@startid").Value = startid
End If
SqlDA2.SelectCommand.CommandText = sql2
SqlDS2 = New DataSet 'DataSet資料集物件實體化
SqlDA2.Fill(SqlDS2) '填充DataSet資料集
SqlDA2.Fill(dt)
SqlDA2.Dispose()
If Hidchktype.Value <> "" Then
Hidtype = Hidchktype.Value
For h As Integer = 0 To dt.Rows.Count - 1
teststype = dt.Rows(h)(0).ToString()
If HidTypeID.Value = teststype Then
pusernoteupdate(startid, textboxnotep)
End If
Next h
ElseIf Hidchktype.Value = "" Then
pusernoteinsert(startid, cktype, HidTypeID, textboxnotep)
Hidchktype.Value = HidTypeID.Value
End If
Else
End If
End Using
Next
For n As Integer = 0 To xxxGridView.Rows.Count - 1
Using cn As New SqlConnection(ConnectString)
Dim dt As New DataTable
Dim textboxnotec As TextBox = xxxGridView.Rows(n).FindControl("textboxnotec")
'''系統執行發生錯誤:並未將物件參考設定為物件的執行個體
'''意思就是抓不到textbox
Dim Hidchktype As HiddenField = xxxGridView.Rows(n).FindControl("Hidchktype")
Dim HidTypeID As HiddenField = xxxGridView.Rows(n).FindControl("HidTypeID")
If textboxnotec.Text <> "" Then
sql2 = "Select chktype from DailyCheckNote where 1=1"
SqlDA2 = New SqlDataAdapter(sql2, cn)
If startid <> "" Then
sql2 += " and startid= @startid"
SqlDA2.SelectCommand.Parameters.Add("@startid", SqlDbType.NVarChar)
SqlDA2.SelectCommand.Parameters("@startid").Value = startid
End If
SqlDA2.SelectCommand.CommandText = sql2
SqlDS2 = New DataSet 'DataSet資料集物件實體化
SqlDA2.Fill(SqlDS2) '填充DataSet資料集
SqlDA2.Fill(dt)
SqlDA2.Dispose()
If Hidchktype.Value <> "" Then
Hidtype = Hidchktype.Value
For h As Integer = 0 To dt.Rows.Count - 1
teststype = dt.Rows(h)(0).ToString()
If HidTypeID.Value = teststype Then
cusernoteupdate(startid, textboxnotec)
End If
Next h
ElseIf Hidchktype.Value = "" Then
cusernoteinsert(startid, cktype, HidTypeID, textboxnotec)
Hidchktype.Value = HidTypeID.Value
End If
Else
End If
End Using
Next
End If
end sub
請問各位大大如何抓到textbox,謝謝各位大大!!
有更簡單的作法
就是GridView資料繫結的DataTable裡多加一個欄位
然後只要把你要放TextBox的位置搬動一下就好了
不需要的資料列的TextBox 記得設隱藏, 應該就可以做到你要的效果
已解決 將TextBox放在外面 在insert到gridview內即可
設計如下
<asp:GridView ID="xxxGridView"... DataKeyNames="...">
<PagerSettings ... Mode="NumericFirstLast" />
<PagerStyle ... HorizontalAlign="Center" />
<Columns runat="server">
<asp:TemplateField HeaderText="xxx" ItemStyle-HorizontalAlign="Left" >
<HeaderStyle Width="90px" />
<ItemTemplate>
<asp:Label ID="TLabxxx" runat="server" Text='<%#Bind("...") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="xxx">
<HeaderStyle Width="200px" />
<ItemTemplate>
<asp:RadioButtonList ID="rdbtnLt1" runat="server" RepeatDirection="Horizontal">
<asp:ListItem>xx</asp:ListItem>
<asp:ListItem>xxx</asp:ListItem>
<asp:ListItem>xxxx</asp:ListItem>
</asp:RadioButtonList>
<asp:HiddenField ID="Hidxxx" runat="server" Value='<%# Bind("...") %>' />
</ItemTemplate>
</asp:TemplateField>
...
<asp:CommandField ... />
</Columns>
<HeaderStyle CssClass="..." />
...
<FooterStyle CssClass="..." />
</asp:GridView>
<asp:panel ID="ptextboxnotep" runat="server"><asp:TextBox ID="textboxnotep" runat="server" TextMode="MultiLine" Width="300" Height="50"></asp:TextBox></asp:panel>
<asp:panel ID="ctextboxnotec" runat="server"> <asp:TextBox ID="textboxnotec" runat="server" TextMode="MultiLine" Width="300" Height="50"></asp:TextBox></asp:panel>
程式碼如下:
設定xxxGridView
Private Sub SetxxxGridView()
...
Dim gvr As GridViewRow = New GridViewRow(0, -1, DataControlRowType.DataRow, DataControlRowState.Normal)
Dim cell1 As TableCell = New TableCell()
Dim cell2 As TableCell = New TableCell()
Dim cell3 As TableCell = New TableCell()
cell2.Controls.Add(ptextboxnotep)
cell3.Controls.Add(ctextboxnotec)
gvr.Controls.Add(cell1)
gvr.Controls.Add(cell2)
gvr.Controls.Add(cell3)
gvr.CssClass = "GridView_B_RowStyle"
gvr.Style("text-align") = "left"
...
End Sub
設定xxxGridView的欄位存進資料庫
Public Sub savexxxGridView()
...
If Session("CompID") = "1" Then
For m As Integer = 0 To xxxGridView.Rows.Count - 1
Using cn As New SqlConnection(ConnectString)
Dim dt As New DataTable
Dim HidTypeID As HiddenField = xxxGridView.Rows(m).FindControl("HidTypeID")
If textboxnotep.Text <> "" Then
sql2 = "Select chktype from DailyCheckNote where 1=1"
SqlDA2 = New SqlDataAdapter(sql2, cn)
If startid <> "" Then
sql2 += " and startid= @startid"
SqlDA2.SelectCommand.Parameters.Add("@startid", SqlDbType.NVarChar)
SqlDA2.SelectCommand.Parameters("@startid").Value = startid
End If
SqlDA2.SelectCommand.CommandText = sql2
SqlDS2 = New DataSet 'DataSet資料集物件實體化
SqlDA2.Fill(SqlDS2) '填充DataSet資料集
SqlDA2.Fill(dt)
SqlDA2.Dispose()
If Hidchktype.Value <> "" Then
Hidtype = Hidchktype.Value
For h As Integer = 0 To dt.Rows.Count - 1
teststype = dt.Rows(h)(0).ToString()
If HidTypeID.Value = teststype Then
pusernoteupdate(startid, textboxnotep)
End If
Next h
ElseIf Hidchktype.Value = "" Then
pusernoteinsert(startid, cktype, HidTypeID, textboxnotep)
Hidchktype.Value = HidTypeID.Value
End If
Else
End If
End Using
Next
For n As Integer = 0 To xxxGridView.Rows.Count - 1
Using cn As New SqlConnection(ConnectString)
Dim dt As New DataTable
Dim HidTypeID As HiddenField = xxxGridView.Rows(n).FindControl("HidTypeID")
If textboxnotec.Text <> "" Then
sql2 = "Select chktype from DailyCheckNote where 1=1"
SqlDA2 = New SqlDataAdapter(sql2, cn)
If startid <> "" Then
sql2 += " and startid= @startid"
SqlDA2.SelectCommand.Parameters.Add("@startid", SqlDbType.NVarChar)
SqlDA2.SelectCommand.Parameters("@startid").Value = startid
End If
SqlDA2.SelectCommand.CommandText = sql2
SqlDS2 = New DataSet 'DataSet資料集物件實體化
SqlDA2.Fill(SqlDS2) '填充DataSet資料集
SqlDA2.Fill(dt)
SqlDA2.Dispose()
If Hidchktype.Value <> "" Then
Hidtype = Hidchktype.Value
For h As Integer = 0 To dt.Rows.Count - 1
teststype = dt.Rows(h)(0).ToString()
If HidTypeID.Value = teststype Then
cusernoteupdate(startid, textboxnotec)
End If
Next h
ElseIf Hidchktype.Value = "" Then
cusernoteinsert(startid, cktype, HidTypeID, textboxnotec)
Hidchktype.Value = HidTypeID.Value
End If
Else
End If
End Using
Next
End If
...
End Sub
謝謝player大大
很久沒寫WebForm了,
我印象中如果是動態新增的,
你要在Page_Init事件當中動態新增同樣Id的控制項,
到了Page_Load的流程之後,
你就可以抓得到那個控制項了,
p.s.
當你按下按鈕觸發PostBack,
會先執行Page_Init(建動態控制項之前),
再執行Page_Load(這時候再建動態控制項就太晚了),
最後才執行Click事件。
不確定行不行 我目前沒試驗出來 謝謝
修正 這也是正確答案。一定要寫Page_Init才能在sub savexxxGridView()內使用textbox。謝謝小魚大大。
程式碼如下:
Private Sub page_Init(sender As Object, e As EventArgs) Handles Me.Init
ConnectString =ConfigurationManager.ConnectionStrings("xxxx").ConnectionString
tmode = Request("xxode")
startid = Request("xxartid")
Using SqlCN As New SqlConnection(ConnectString) SqlCN.Open()
SetxxxGridView(SqlCN, tmode, startid)
End Using
End Sub