Asp.net 将链接按钮动态添加到gridview。不开枪。
我动态地向gridview中的每个单元格添加一个链接按钮。添加按钮可以工作,但偶数处理程序的触发却不能。我需要linkbutton来调用函数并传递一些数据进行处理。我的代码如下。我从这个网站上找到了解决方案,使我走到了这一步。 此时,gridview加载带有蓝色按钮的单元格。当您单击它们时,它们会返回纯文本,并且不会调用任何函数Asp.net 将链接按钮动态添加到gridview。不开枪。,asp.net,vb.net,gridview,postback,linkbutton,Asp.net,Vb.net,Gridview,Postback,Linkbutton,我动态地向gridview中的每个单元格添加一个链接按钮。添加按钮可以工作,但偶数处理程序的触发却不能。我需要linkbutton来调用函数并传递一些数据进行处理。我的代码如下。我从这个网站上找到了解决方案,使我走到了这一步。 此时,gridview加载带有蓝色按钮的单元格。当您单击它们时,它们会返回纯文本,并且不会调用任何函数 Private Sub gv_datasource_options_RowDataBound(sender As Object, e As System.Web.UI.
Private Sub gv_datasource_options_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gv_datasource_options.RowDataBound
Try
If e.Row.RowType = DataControlRowType.DataRow Then
For Each c As TableCell In e.Row.Cells
If Len(c.Text) > 0 And c.Text <> " " Then
Dim v_lb As New LinkButton()
v_lb.Text = c.Text
AddHandler v_lb.Click, AddressOf add_datasource
v_lb.Attributes.Add("AutoPostback", "True")
v_lb.Attributes.Add("runat", "Server")
v_lb.Attributes.Add("AutoEventWireup", "True")
v_lb.CommandName = "NumClick"
v_lb.CommandArgument = e.Row.Cells(0).ToString & "|" & gv_datasource_options.HeaderRow.Cells(e.Row.Cells.GetCellIndex(c)).Text
c.Controls.Add(v_lb)
Dim sm As ScriptManager = ScriptManager.GetCurrent(Me)
sm.RegisterAsyncPostBackControl(v_lb)
End If
Next
End If
Catch ex As Exception
cl_Error.cl_Erorr.LogError(ex, txt_userid.Value, ex.ToString)
End Try
End Sub
Private Sub add_datasource(sender As Object, e As CommandEventArgs)
Try
hf_datasource_id.Value = Left(e.CommandArgument.ToString(), (Len(e.CommandArgument.ToString) - InStr(e.CommandArgument.ToString, "|")))
hf_datasource_column.Value = Left(e.CommandArgument.ToString(), (Len(e.CommandArgument.ToString) - InStr(e.CommandArgument.ToString, "|")))
hf_datasource_tableid.Value = tv_content.SelectedValue
p_datasource.Visible = False
Catch ex As Exception
cl_Error.cl_Erorr.LogError(ex, txt_userid.Value, ex.ToString)
End Try
End Sub
Private Sub gv_datasource_options_RowDataBound(发送方作为对象,e作为System.Web.UI.WebControls.GridViewRowEventArgs)处理gv_datasource_options.RowDataBound
尝试
如果e.Row.RowType=DataControlRowType.DataRow,则
对于e.Row.Cells中的每个c作为表格单元格
如果Len(c.Text)>0且c.Text为“”,则
尺寸v_lb为新链接按钮()
v_lb.Text=c.Text
AddHandler v_lb.Click,add_数据源的地址
v_lb.Attributes.Add(“自动回发”、“True”)
v_lb.Attributes.Add(“runat”、“Server”)
v_lb.Attributes.Add(“AutoEventWireup”、“True”)
v_lb.CommandName=“NumClick”
v_lb.CommandArgument=e.Row.Cells(0).ToString&“|”&gv_datasource_options.HeaderRow.Cells(e.Row.Cells.GetCellIndex(c)).Text
c、 控件。添加(v_lb)
Dim sm作为ScriptManager=ScriptManager.GetCurrent(Me)
sm.RegisterAsyncPostBackControl(v_lb)
如果结束
下一个
如果结束
特例
cl_Error.cl_Erorr.LogError(ex,txt_userid.Value,ex.ToString)
结束尝试
端接头
私有子添加_数据源(发送方作为对象,e作为CommandEventArgs)
尝试
hf_datasource_id.Value=Left(e.CommandArgument.ToString(),(Len(e.CommandArgument.ToString)-InStr(e.CommandArgument.ToString,“|”))
hf_datasource_column.Value=Left(e.CommandArgument.ToString(),(Len(e.CommandArgument.ToString)-InStr(e.CommandArgument.ToString,“|”))
hf\u数据源\u tableid.Value=tv\u content.SelectedValue
p_datasource.Visible=False
特例
cl_Error.cl_Erorr.LogError(ex,txt_userid.Value,ex.ToString)
结束尝试
端接头
而不是将事件(“添加数据源”)添加到Linkbutton,尝试使用GridView的RowCommand事件。您肯定能够在Gridview的Row命令事件上获取linkbutton冒泡的事件(以及commandname和commandarguments)我不确定是否理解。这是否允许我将具有唯一参数的alink按钮添加到每个单元格?是的,它将。在Gridview_RowCommand事件中,您将获得每个CommandArgument的唯一值(您为所有linkbuttons指定的唯一值)。