Plz帮助我解决这个问题 [英] Plz help me in solving this prob
问题描述
大家好,
我的asp.net页面中有很多动态生成的GridView。在所有GridView中,所有单元格都是链接按钮,它们也是动态生成的。我希望得到选定的单元格/链接按钮对应的列索引。我该怎么做?
注意 - 我没有GridView名称,因为它是在循环中动态生成的。代码如下
Hi all,
I have n number of dynamically generated GridViews in my asp.net page. In all the GridView all the cells are linkbuttons which also dynamically generated. And I want to get selected cells/ linkbuttons corresponding column index. How do i do it?
Note- I dont have GridView name as it is dynamically generated in loop. code as below
Protected Sub Btncalculate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Btncalculate.Click
Call GetFinal()
End Sub
Public Sub GetFinal()
For count As Integer = 0 To dtValue.Rows.Count - 1
Dim GrdView1 As New GridView()
'DataTable filling Code goes here
GrdView1.DataSource = dt1
AddHandler GrdView1.SelectedIndexChanged, AddressOf GrdView1_SelectedIndexChanged
AddHandler GrdView1.RowDataBound, AddressOf GrdView1_RowDataBound
AddHandler GrdView1.RowCommand, AddressOf GrdView1_RowCommand
GrdView1.DataBind()
end sub
Public Sub GrdView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
' All cell to LinkButton code goes here
AddHandler lnkShow.Click, AddressOf LinkButton1_Click
end sub
推荐答案
这是一个选项....
创建一个字典对象来存储名称......
Here's one option....
Create a dictionary object to store the names...
' create a dictionary object to store header titles
Private headerNames As Dictionary(Of Int32, String) = Nothing
...并在RowType为Header时设置名称。然后,当您到达DataRow时,您可以使用单元格索引来提取标题行为数据绑定时存储的标题名称。
... and set the names when the RowType is Header. Then when you get to the DataRow you can use the cells index to pull the header name that was stored when the header row was databound.
Protected Sub GridView1_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
If e.Row.RowType = DataControlRowType.Header Then
' initialize dictionary object when header is databound
headerNames = New Dictionary(Of Int32, String)
' iterate header cells and capture each column's index and name and store in the dictionary object
For i As Int32 = 0 To e.Row.Cells.Count - 1
headerNames.Add(i, e.Row.Cells(i).Text)
Next
End If
If e.Row.RowType = DataControlRowType.DataRow Then
For i As Int32 = 0 To e.Row.Cells.Count - 1
Dim cell As TableCell = e.Row.Cells(i)
' get header text from dictionary using column index
Dim headText As String = headerNames(i).ToString()
' code to build link
'
Next
End If
End Sub
这篇关于Plz帮助我解决这个问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!