1

I am new to Vb.net. I am using gridview and binding it a resultset from database. I am trying to use asp button and associate a codebehind function with it.

But since the rows are generating dynamically, ids of button are incrementing accordingly.

Here is the code -

'<asp:GridView ID="grdProjects" runat="server" CssClass="q_acontent" Width="990px"
AutoGenerateColumns="false" CellPadding="2" ClientIDMode="static" ViewStateMode="Enabled">
<asp:TemplateField HeaderText="Document Name">
<ItemTemplate>
<asp:Button ID="Button3" runat="server" Text='<%# (Convert.ToString(DataBinder.Eval(Container.DataItem, "Data_text")))%>'/>
</ItemTemplate>
<ItemStyle Width="5%" HorizontalAlign="Center" CssClass="conatact_phone  breakword" />
</asp:TemplateField>
</asp:GridView>'

Please help me how to associate codebehind function and pass its corresponding button text to it.

1 Answer 1

1

Use the OnCommand event of button controls and Set the CommandArgument property of the button to your DataItem value as:

<ItemTemplate>
<asp:Button ID="Button3" runat="server" 
 Text='<%# (Convert.ToString(DataBinder.Eval(Container.DataItem,"Data_text")))%>'
 OnCommand="Button3_Command" 
 CommandArgument='<%# (Convert.ToString(DataBinder.Eval(Container.DataItem,
                  "Data_text")))%>'
        />
</ItemTemplate>

OnCommand event in your code behind file:

Public Sub Button3_Command(sender As Object, e As CommandEventArgs)
    Dim _dataText As String = e.CommandArgument.ToString()

End Sub
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.