Pradeep Chavan
Pradeep Chavan

Reputation: 11

Bind label text inside gridview

I saw few grid view examples where data is bound to grid view from a sql or other database. The question is -

<ItemTemplate> 
    <asp:Label ID="label1"  runat="server" Text='<%#Bind("name") %>'>  
    </asp:Label>  
</ItemTemplate>  

How is Text='<%#Bind("name")%>' working? From where does the label gets the text?

I am using mysql I have a drop down list of tables, and a button. Whenever the user selects any table from ddl, and clicks on the button, I will bind the selected table with the grid.

I have enable autogenerating=true for edit and delete buttons.

I will write code for that, but whenever the user selects a different table will the grid show edit and delete buttons? and what about the Bind("value") ? will it change for every table?

Might be a silly question but please help!

Upvotes: 1

Views: 15214

Answers (2)

Netricity
Netricity

Reputation: 2738

The #Bind("name") command will insert the value of the column named name from whichever table you're binding to the GridView. Therefore, each of your tables would need a column named name for this label to be populated. Also, #Bind should be used for both displaying and updating data. If you only need to display data, #Eval("name") is a better choice, as this is read-only.

Upvotes: 2

Able
Able

Reputation: 3912

you have to use the #Eval, for example,

            <EditItemTemplate>
        <asp:Label ID="lbleditusr" runat="server" Text='<%#Eval("Username") %>'/>
        </EditItemTemplate>

Upvotes: 1

Related Questions