none
Gridview dynamique avec bouton d'action RRS feed

  • Question

  •  

    Bonjour

     

    Je souhaite créé un gridview dynamique, rempli par ma page vb, avec un champs ItemTemplate qui comprends un bouton d'action.

     

    Pour le moment, j'arrive a créer mon tableau avec mon bouton par contre quand je clique sur le bouton j'arrive a afficher le le databind dans un mesgbox par contre, je n'arrive pas à l'afficher dans un textbox.

     

    ci-joint mon code.

     

    Page ASPX

     <%@ Page Language="VB" AutoEventWireup="true" CodeFile="Default.aspx.vb" Inherits="_Default" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
            <aspTongue TiedcriptManager ID="ScriptManager1" runat="server" />
            <div>
                <asp:GridView ID="TableGridView" runat="server" AutoGenerateColumns="false" >
                </asp:GridView>
            </div>
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        </form>
    </body>
    </html>

     

    Page ASPX.VB

    Partial Class _Default

    Inherits System.Web.UI.Page

    Implements ITemplate

     

     

    Public Shared Table As New DataTable()

    Private ParameterArray As New ArrayList()

    Public Shared Group As String

    Private ItemType As ListItemType

    Private FieldName As String

    Private InfoType As String

    Public sButton As String

     

     

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load

    CreateTemplatedGridView("1")

    End Sub

    Function CreateTemplatedGridView(ByVal ID As String) As GridView

    'Dim TableGridView As New GridView()

    ' fill the table which is to bound to the GridView

    PopulateDataTable(ID)

    TableGridView.AutoGenerateColumns = False

    ' add templated fields to the GridView

    Dim BtnTmpField As New TemplateField()

    BtnTmpField.ItemTemplate = New _Default(ListItemType.Item, "IdInContent", "Command")

    BtnTmpField.HeaderTemplate = New _Default(ListItemType.Header, "...", "header")

    Dim ItemTmpField As New TemplateField()

    Dim ItemTmpField2 As New TemplateField()

    ' create HeaderTemplate

    ItemTmpField.HeaderTemplate = New _Default(ListItemType.Header, "Name", "header")

    ItemTmpField2.HeaderTemplate = New _Default(ListItemType.Header, "Comment", "header")

    ' create ItemTemplate

    ItemTmpField.ItemTemplate = New _Default(ListItemType.Item, Table.Columns(1).ColumnName, "content")

    ItemTmpField2.ItemTemplate = New _Default(ListItemType.Item, Table.Columns(3).ColumnName, "content")

    ' then add to the GridView

    TableGridView.Columns.Add(BtnTmpField)

    TableGridView.Columns.Add(ItemTmpField)

    TableGridView.Columns.Add(ItemTmpField2)

    ' bind and display the data

    TableGridView.DataSource = Table

    TableGridView.DataBind()

    Dim resultat As GridView

    resultat = TableGridView

    Return resultat

    End Function

    Public Sub New()

    ItemType = Nothing

    FieldName = Nothing

    InfoType = Nothing

    End Sub

    Public Sub New(ByVal item_type As ListItemType, ByVal field_name As String, ByVal info_type As String)

    ItemType = item_type

    FieldName = field_name

    InfoType = info_type

    End Sub

    Public Sub InstantiateIn(ByVal Container As System.Web.UI.Control)

    Implements System.Web.UI.ITemplate.InstantiateIn

    Select Case ItemType

    Case ListItemType.Header

    Dim header_ltrl As New Literal()

    header_ltrl.Text = "<b>" + FieldName + "</b>"

    Container.Controls.Add(header_ltrl)

    Exit Select

    Case ListItemType.Item

    Select Case InfoType

    Case "Command"

    Dim edit_button As New Button()

    edit_button.ID = "edit_button"

    edit_button.Text = "Execute"

    AddHandler edit_button.DataBinding, AddressOf OnDataBinding

    AddHandler edit_button.Click, AddressOf tb1_EditClik

    Container.Controls.Add(edit_button)

    Exit Select

    Case Else

    Dim field_lbl As New Label()

    field_lbl.ID = FieldName

    field_lbl.Text = [String].Empty

    AddHandler field_lbl.DataBinding, AddressOf OnDataBinding

    Container.Controls.Add(field_lbl)

    Exit Select

    End Select

    Exit Select

    Case ListItemType.EditItem

    Dim field_txtbox As New TextBox()

    field_txtbox.ID = FieldName

    field_txtbox.Text = [String].Empty

    AddHandler field_txtbox.DataBinding, AddressOf OnDataBinding

    Container.Controls.Add(field_txtbox)

     

     

    Exit Select

    End Select

    End Sub

    Public Sub OnDataBinding(ByVal sender As Object, ByVal e As EventArgs)

    Dim bound_value_obj As Object = Nothing

    Dim ctrl As Control = DirectCast(sender, Control)

    Dim data_item_container As IDataItemContainer = DirectCast(ctrl.NamingContainer, IDataItemContainer)

    bound_value_obj = DataBinder.Eval(data_item_container.DataItem, FieldName)

    Select Case ItemType

    Case ListItemType.Item

    Select Case InfoType

    Case "Command"

    Dim btn_ltrl As Button = DirectCast(sender, Button)

    btn_ltrl.CommandArgument = bound_value_obj.ToString()

    Exit Select

    Case Else

    Dim field_ltrl As Label = DirectCast(sender, Label)

    field_ltrl.Text = bound_value_obj.ToString()

    Exit Select

    End Select

    End Select

    End Sub

     

     

    Public Sub tb1_EditClik(ByVal sender As System.Object, ByVal e As System.EventArgs)

    Dim myButton As Button = CType(sender, Button)

    sButton = myButton.CommandArgument

    msgbox=(TextBox1.Text )

    TextBox1.Text = "fgdfgf"

    End Sub

     

     

     

    Public Sub PopulateDataTable(ByVal ID As String)

    Table = New DataTable()

    TableGridView.Columns.Clear()

     

    Dim Connection As SqlConnection = New System.Data.SqlClient.SqlConnection("Data Source=frillssql08; Integrated Security=SSPI;" & "Initial Catalog=test")

    Dim requete As String = "Select * from InContent Where ContentID = " + ID + " and Access = 1"

    Dim adapter As New SqlDataAdapter(requete, Connection)

    adapter.Fill(Table)

    End Sub

     

    End Class

     

    Merci de votre aide

     

    David

    lundi 22 septembre 2008 07:23