datagrid中的一个绑定列,在绑定时,判断要绑定的字段是否为空,如为空,添加一个linkbutton控件,当用户点击该控件时,引发后台处理事件,问题是,后台处理事件怎样和这个动态添加的lindbutton联系上?代码如下:
    Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemDataBound
        If Not e.Item.ItemType = ListItemType.Header Then
            If e.Item.Cells(3).Text = " " Then
                Dim lbutton As LinkButton = New LinkButton()
                lbutton.Text = "抢占"
                lbutton.CommandArgument = e.Item.Cells(5).Text
                e.Item.Cells(3).Controls.Add(lbutton)
            End If
        End If    End SubPublic Sub lbutton_click(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs)
        Dim f As CommandEventArgs
        Dim conn As OleDbConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Password="""";User ID=Admin;Data Source=c:/inetpub/wwwroot/rexian/shujuku/rexian.mdb;Mode=Share Deny None")
        conn.Open()
        Dim cmd_string As String = "update jilubiao set qzr='" + Session("dlr") + "' where id=" + f.CommandArgument
        Dim cmd As OleDbCommand = New OleDbCommand(cmd_string, conn)
        cmd.ExecuteNonQuery()
        conn.Close()
        DataGrid1.DataBind()
    End Sub

解决方案 »

  1.   

    解决例子:
    <script>
    Sub ItemsGrid_Command(sender As Object, e As DataGridCommandEventArgs)         Select (CType(e.CommandSource, LinkButton)).CommandName            Case "Delete"
                   DeleteItem(e)            ' Add other cases here, if there are multiple ButtonColumns in 
                ' the DataGrid control.            Case Else
                   ' Do nothing.         End Select      End Sub
    </script><form runat="server">      <h3>DataGrid Editing Example</h3>
     
          <asp:DataGrid id="ItemsGrid"
               BorderColor="black"
               BorderWidth="1"
               CellPadding="3"
               OnItemCommand="ItemsGrid_Command"
               AutoGenerateColumns="false"
               runat="server">         <HeaderStyle BackColor="#aaaadd">
             </HeaderStyle>
     
             <Columns>            <asp:EditCommandColumn
                     EditText="Edit"
                     CancelText="Cancel"
                     UpdateText="Update" 
                     HeaderText="Edit item">               <ItemStyle Wrap="False">
                   </ItemStyle>               <HeaderStyle Wrap="False">
                   </HeaderStyle>            </asp:EditCommandColumn>            <asp:ButtonColumn 
                     HeaderText="Delete item" 
                     ButtonType="LinkButton" 
                     Text="Delete" 
                     CommandName="Delete"/>  
     
                <asp:BoundColumn HeaderText="Item" 
                     ReadOnly="True" 
                     DataField="Item"/>
     
                <asp:BoundColumn HeaderText="Quantity" 
                     DataField="Qty"/>
     
                <asp:BoundColumn HeaderText="Price"
                     DataField="Price"
                     DataFormatString="{0:c}"/>
     
             </Columns>
     
          </asp:DataGrid>   </form>
      

  2.   

    说点思路哈:不要这样动态生成linkbutton.
    而是这列绑定linkbutton,然后不符合条件的visible=false
    linkbutton要设置commandname值然后在itemcommand时间中根据commandname进行处理
      

  3.   

    lbutton.Attributes.Add("onclick","sdsdfsdfsd")不知道这样可以不.
      

  4.   

    谢谢各位,现将根椐各位的建议,操作的结果回复如下:
    1、xifan930朋友的方法行不通,可能是lbutton.attributes.add方法增加的属性都是用于呈现的属性,也就是说在生成的HTML文档中要呈现的属性
    2、zhouyqzhouyq朋友的方法,无法判断字段qzr是否为空呀,因为绑定LINKBUTTON控件后,数据库中的字段怎样表示出来
    3、yyst朋友说的不是动态生成控件