DataBind DataSource Result to ASP Labels - c#

I have an AccessDataSource TestSummaryADS. I can easily view the results in a GridView or DropDownList by setting its DataSourceID property.
How do I bind a value from the results TestSummaryADS to the text of a label?
I'm just trying to populate labels on my page with results from the DB entry.
C# or VB.NET answers okay.

If you only have a single record, use a FormView
<asp:FormView ID="FormViewTestSummary" runat="server"
DataSourceID="TestSummaryADS"
DefaultMode="Edit">
<EditItemTemplate>
<fieldset>
<asp:TextBox ID="txtMyText" Text='<%# Bind("ProjectNumber") %>' runat="server" />

Use Repeater control that contains Label controls to display the values you want. Bind your DataSource control to the repeater.
Also you can use TemplateColumn with GridView and add your labels into this template column.
But IMO, Repeater control is simpler to customize your view by templates.

Related

Check Box in ASP.NET page is not working correctly

I am using Grid View and inside grid view template defined for checkbox. I have used this criteria on various pages. But on some asp pages check box is not true as i have checked them it will always return property Checked = false.
<ItemTemplate>
<asp:CheckBox ID="chkBox_ID" runat="server" Checked='<%# Convert.ToBoolean( Convert.ToInt32(DataBinder.Eval(Container.DataItem, "COLUMN_NAME"))) %>' />
</ItemTemplate>
Template is defined above.
I am using foreach loop to get rows then cell and then FindControl() method to find the checkbox.
This is code behind how i access it.
CheckBox chkRow = (row.Cells[GetColumnIndexByName(Grv_h, "Authorize")].FindControl("chkBox_ID") as CheckBox);

How to set a default template in a ListView control

I have a ListView control with an ItemTemplate, and an EditItemTemplate. By default, the ItemTemplate is rendered; how can I change that? More clearly, on Page_Load, I would like my EditItemTemplate to be the selected template.
<asp:ListView ID="lvExample" runat="server" SelectMethod="GetStuff" UpdateMethod="UpdateStuff">
<EditItemTemplate>
...
</EditItemTemplate>
<EditItemTemplate>
...
</EditItemTemplate>
</asp:ListView>
You can use this approach.
(1) Write your Edit logic/Edit Template in your inside you item template.
(2) Then you can put your ItemTemplate content into another template.
since the default load template is ItemTemplate you get your edit template load first since the content of your itemteplate is your edit template

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!
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>

Dynamically assigning a property in a usercontrol in an ASP.NET repeater control

I currently have a repeater control and inside the itemtemplate I have a usercontrol. This usercontrol renders correctly, but I am trying to assign a dataitem to a property in the repeater control.
<asp:Repeater ID="Repeater1" DataSourceID="EntityDataSource" runat="server">
<ItemTemplate>
<uc1:Request ID="Request1" runat="server" RequestId='<%# Eval("RequestId") %>' />
</ItemTemplate>
RequestId is just an Int32. It just doesn't assign it.
I can put the eval outside of the usercontrol just in the itemtemplate and it correctly outputs the right id.
If I remove the whole eval and just type a number in then it works fine.
Any help appreciated.
[UPDATE] : Issue Solved
I was using an EntityDataSource and this automatically binded to the repeater. It printed out all the information from the database on the screen without any codebehind. But when I put in the code behind Repeater1.DataBind(); it then started to work.
I don't know why, but hey it's solved. It now successfully passes the value through. I imagine it has something to do with the page lifecycle.
If you just bind with repeater collection of int, you need use this:
<uc1:Request ID="Request1" runat="server" RequestId='<%# Container.DataItem %>' />
And don't forget to call DataBind() for repeater or for Page where there is a repeater control.
Are you missing a ' at the end?
change following:
RequestId='<%# Eval("RequestId") %> />
to
RequestId='<%# Eval("RequestId") %>' />
You can implement this by using Repeater control's ItemDataBound event, so that you can set the dynamic property for the control.

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