Bind label text inside gridview - c#

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!

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.

you have to use the #Eval, for example,
<EditItemTemplate>
<asp:Label ID="lbleditusr" runat="server" Text='<%#Eval("Username") %>'/>
</EditItemTemplate>

Related

Deleting Selected ListViewItems from the Database

I have a ListView that I populate from the database. The ListView has a Checkbox on each row which is a built-in property of ListView.
I have been trying to get the text from the ID column next to the Checkbox which I will use in deleting the respective rows from the database but it all fails. I thought I could get the ListView.CheckedIndices and use them in return to point to the SelectedText of each index.
However all fails. More specifically i wanted to use the OnItemDeleting Event of list view but the ListView I have does not seem to have such event.
Is there any way i can achieve this?
I have had this problem before. What I did was this below:
<asp:ListView ID="xyz" runat="server">
<ItemTemplate>
<asp:LinkButton runat="server" ID="blah" OnClick="blah_Click"
CommandArgument="<%# myObject.Id %>" />
</ItemTemplate>
</asp:ListView>
I got the passed id through the command argument.Then I deleted the selected item and refreshed the data for listView. I am sure there must be a cleaner way of doing it.
Let me know how it goes.

Hyperlink in Gridview which is autogenerated in C#

I have a Gridview which is autogenerated by fetching data from various tables.
Now my requirement is i need to make my first column as hyperlink so that if it is clicked then it has to navigate to another page with hyperlinked value.
In the second page i have Lable this label should show the hyperlink value and based on that the data will be populated on the second page.
For example:
I have a gridview with 10 columns..My first column is Emp Id.
If that id is clicked it has to take me to second page and the label control must get this id value and based on that id the rest of the info like emp name emp DOB should be filled.
i am using C# as my code behind.
Can anyone help me to proceed..
Awaiting ur reply
You need the <asp:HyperLinkField />
<asp:GridView>
<Columns>
<asp:HyperLinkField HeaderText="Id" DataTextField="YourID" DataNavigateUrlFields="YourID"
DataNavigateUrlFormatString="SecondPage.aspx?Id={0}" />
</Columns>
</asp:GridView>
You might need to remove the AutoGenerateColumns="true" property and type the fields yourself, selecting only the columns you want to show - using <asp:BoundFied DataField="ColumnName" />
In case you want to pass multiple values in the querystring, separate the fields with comma
DataNavigateUrlFields="YourID, SecondField"
and your format string would be
DataNavigateUrlFormatString="SecondPage.aspx?Id={0}&param2={1}"
Other links
HyperLinkField.DataNavigateUrlFormatString Property
How to pass multiple values in HyperlinkField
You can use a TemplateField column in your GridView:
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink ID="linkToDetails" runat="server" NavigateUrl='Details.aspx?empId=<%# Eval("empId") %>' Text='<%# Eval("empId") %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
On the Details.aspx page you should get the empId from the QueryString and get the details from the database.

Gridview textbox on Page_load enable edit

using Visual.Web.Developer.2010.Express;
using SQL.Server.Management.Studio.2008.R2;
N00b here,
I've got the gridview to look the way I want it to (a textbox inside of the ItemTemplate). The Textbox's class has some client-sideJS that enables a save button (an asp:LinkButton set to look like a Jquery UI save icon) to become visible after the Textbox's .keypress event fires..
Now for my question..I've looked everywhere, but I can't get how to have gridview put the Sql server db content in that textbox on Page_load (one textbox + <br /> for each row). I'm only printing one collumn from the Sql server db into the Gridview.. Also, how would I bind the asp:LinkButton save button to gridview's save event? If there is a more effecient way to do this? If you have some insight for me, please give me your opinion/!
My .aspx code
<asp:TemplateField >
<ItemTemplate>
<asp:TextBox ID="TextBox1" class="hexen" runat="server" DataField="TbValue" SortExpression="TbValue">
</asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:FluxConnectionString %>"
SelectCommand="SELECT [TbValue] FROM [InvestigateValues]">
</asp:SqlDataSource>
Thanks in advance!
Change your text box to
<asp:TextBox ID="TextBox1" class="hexen" runat="server" text='<%#Bind("TbValue")%>' />
This will enable two way databinding.
Here is an article to get you started: http://www.devx.com/DevX/Article/35058.
The grid view and SqlDataSource expose Insert, Update and Delete event/methods. These are on a row level, not grid level.
The way I would approach your problem would be to have an onclick event for your link button that iterates through the gridview, get the data from each text box and then perform the appropriate data base action in the code behind.

Multiple DataTable columns stacked horizontally in one GridView Column

How can multiple columns from a DataTable be stacked into one column of a GridView ?
Is there a better asp control for performing such an operation?
Lets assume you are binding that value to a label, then it will simply be:
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Eval("FirstName") + Eval("LastName")%>'></asp:Label>
</ItemTemplate>
You could iterate over the columns and rows on the server, pushing the items into any control that will display this one column (list, one column HTML table etc.)

How can i bind data to a dropdown in the gridview which is an EditItemTemplate

I declared my template as follows
<EditItemTemplate>
<asp:DropDownList ID="ddlYear" runat="server" DataSource='<%#GetYears() %>' DataTextField="year" DataValueField="year"></asp:DropDownList>
</EditItemTemplate>
I need to bind the data from the function i used GetYears() i don't know how to function it can any one help me
I need the data for example name to be loaded in dropdown when i click on Edit of gridview is it the correct way or is there any best way to do this
in the code behind you can find dropdown using findcontrol method on the click of edit link and can bind easily.
You could create an objectDatasource on the page and then set the datasourceid to that objectDataSource. Then you can create your POCO class
Look at this link for more info on objectdatasource
http://quickstarts.asp.net/QuickStartv20/aspnet/doc/ctrlref/data/objectdatasource.aspx

Categories

Resources