Is it possible to perform a SQL SELECT Statement that will select all rows, then output the rows to a label, which is created dynamically?
In the system I'm writing (pretty new to c#) I am performing a SQL SELECT from a view where a records ID can appear multiple times, but with different associated values, i.e.
ID - VALUE
1 - A
1 - B
1 - C
2 - A
2 - B
3 - A
What I want to do then is output each result to a its own label, but i would have no idea until the SELECT is ran how many labels I would need so I'd need code to draw them dynamically? Using the above examples A would return 3 labels, but B would only return 2.
If that makes any sense? E.G
foreach (result in sql)
{
label.Text = result
}
Thanks
You can use a repeater to do this.
https://msdn.microsoft.com/en-us/library/zzx23804(v=vs.85).aspx
<asp:Repeater ID="labelRepeater" runat="server"
DataSourceID="labelDataSource">
<ItemTemplate>
<asp:Label runat="server" ID="Label1"
text='<%# Eval("Value") %>' />
</ItemTemplate>
</asp:Repeater>
<asp:SqlDataSource
ConnectionString=
"<%$ _fill_in_your_string %>"
ID="labelDataSource" runat="server"
SelectCommand="SELECT ID, Value from myTable">
</asp:SqlDataSource>
Look here also: possible duplicate? How to add Repeater control dynamically from code behind?
Related
I have created a bulk edit gridview using Itemtemplate for each column. Label for non-edit mode, Textbox for edit mode. But this works only if the structure of gridview is known(Defining templates in asp).
<asp:TemplateField HeaderText="Name" ConvertEmptyStringToNull="True">
<ItemTemplate>
<asp:Label ID="lblName" Visible='<%# !(bool) IsInEditMode %>' runat="server" Text='<%# Eval("name") %>' />
<asp:TextBox ID="txtName" ControlStyle-CssClass="wide" Visible='<%# IsInEditMode %>'
runat="server" Text='<%# Eval("name") %>' />
</ItemTemplate>
</asp:TemplateField>
Now, what I am trying to achieve is when the user selects a dropdown value, the sql query is fired and it returns result. And this result is shown in gridview. But the problem arises because the number of columns in the result for each selected value from dropdown may vary. And i want to make the gridview as editable(or read-only). This requires two templates to be defined for each column.
So i want to know how this can be done dynamically i.e defining templates depending on number of columns returned by sql.
First off, you could trigger the query and following code with this event.
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
//insert code
}
For the issue of different templates, I might suggest using two GridViews. I've done this where I have two separate sets of results. If they choose a dropdownItem, it hides GridView1 and shows GridView2 (while querying GridView2.SqlDataSource).
Now are you querying different SQL Tables based on dropdown? Or are they querying from the same place?
Is it possible to programatically insert C# Inline Expressions as the values for ASP.NET Controls in your server-side code?
I'm currently using a DataList to display a lot of data in a table. I want to be able to dynamically change the columns of that table, so I need to be able to edit what controls are in its ItemTemplate.
However, in addition to editing the controls in the ItemTemplate, I need to be able to alter the value that is binded to each control in the template, because I want to dynamically change what is being displayed.
So what I currently have is a static table that doesn't change:
<asp:DataList ID="dataList" runat="server" OnSelectedIndexChanged="dataList_OnSelectedIndexChanged" DataSourceID="peopleData">
<ItemTemplate>
<table>
<tr>
<td>
<asp:LinkButton ID="NameLink" OnClientClick="onPageUpdate()" CommandName="Select" Text='<%# Eval(this.Name) %>' ForeColor='<%# Eval("this.NameColor") %>' runat=Server" />
</td>
<td>
<asp:Label Text='<%# Eval("this.Values[\"Age\"]") %>' ForeColor='<%# Eval("this.ValueColors[\"Age\"]") %>' runat="Server">
</td>
// OTHER COLUMNS WITH DIFFERENT DATA
</tr>
</table>
</ItemTemplate>
</asp:DataList>
// OBJECT DATA SOURCE CODE
I know how to dynamically add Controls to the ASPX web page. However, I don't know how to add the inline expressions.
I've tried doing the following but it doesn't work because of the type mismatches:
Label label = new Label();
label.Text = "<%# Eval(\"this.Values[\\\"Age\\\"]\") %>";
label.ForeColor = "<%# Eval(\"this.ValueColors[\\\"Age\\\"]\") %>";
Is there a way of achieving this or doing something similar?
My only other option that I can think of right now is to scrap using the DataList and ItemTemplate and just generate each row myself.. That's a lot more coding versus just using the ItemTemplate.
Not sure if this would help you, but look at http://msdn.microsoft.com/en-us/library/system.web.ui.databinder(v=vs.100).aspx
It is showing using of the hand-written exposure of properties. If your property were always call "magic" and you return the appropriate value for magic within your code would that get you what you need?
I am using the following technique to dynamically update a textbox -
Code behind
Lat.DataSource = ws.returnLAT(place);
Lat.DataBind();
aspx code
<asp:DataList runat="server" ID="Lat">
<ItemTemplate>
<td>
<asp:Label for="latBox" ID="Label5" runat="server" Text="Latitude"></asp:Label>
<br />
<asp:TextBox ID="latBox" runat="server" Text='<%# Container.DataItem.ToString() %>'></asp:TextBox>
</td>
</ItemTemplate>
</asp:DataList>
One debugging, DataSource is equal to 99, however when presented in the web page, that value is split into two separate text boxes each with a single 9 in them instead of the desired single textbox with 99 in it.
How can I resolve this?
I think what's happening here is it's (for whatever reason) treating 99 as 2 separate records. You shouldn't really be passing a single value as a DataSource it should be some form of collection.
For example, if you changed your binding code to:
Lat.DataSource = new List<int>(1) { ws.returnLat(place) };
Does the problem go away?
As you know when you have a lot of elements in a database table and you also have a relationship between this table and another (let's say many-to-many) , when you are trying to insert a new item in Dynamic Data, for the foreign key option you have a lot of checkboxes you can check.
For example, if you have Category and Products and you want to insert a new Category you will have something like this:
Product1 Product2 Product3 Product4
Product5 Product6 ...
When you have few products it looks good, but when you have over 50 000 it doesn't.
How can I replace them with a grid view ( where I load product entities ) having a checkbox before every product name. If I check that checkbox it means I want to add that product to this category (that I'm creating in this moment ).
Hope you understand what I am trying to say.
I'm thinking to make a custom field ( or a custom page with custom field ) but I don't know how to make that grid view.
Thank you
How about you make a Gridview:
<asp:GridView runat="server" ID="gvMyGrid">
<Columns>
<asp:BoundField DataField="MyField" HeaderText="HeaderForMyField />
... You have a few of these
<asp:TemplateField HeaderText="Products">
<ItemTemplate>
<asp:Repeater runat="server" id="rptListOfProducts" OnItemDataBound="ProductsDataBound">
<ItemTemplate>
<asp:HiddenField id="hfProductId" runat="server"
Value='<%# DataBinder.Eval(Container.DataItem, "ProductId") %>'/>
<asp:CheckBox id="cbProduct" runat="Server"
Text='<%# DataBinder.Eval(Container.DataItem, "ProductName") %>'
Checked='<%# DataBinder.Eval(Container.DataItem, "IsThisProductSelected") %>' /> //Or some kind of flag that is true or false
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</Columns>
</asp:GridView>
Then in your C# code you can something like this to find out what the user changed.
foreach (Control c in rptMonitors.Items)
{
var productId = int.Parse(((HiddenField)c.FindControl("hfProductId")).Value);
//And get the check box too, generate a list of ProductIds, and if they're checked or not
}
Not sure if this is what you're looking for, the description was a little vague
Edit: Oh and if you're expecting loads of entries in the Repeater, you could wrap it in an AjaxControlToolkit Collapsible Panel, so it doesn't spam up your screen.
I have a database table TravelRequest that contains, amongst other things, the fields SignOffName and SignOffDate. I have a table adapter TravelRequestTable based on this table. I have a DetailsView control which uses this via an ObjectDataSource. Should be all fairly standard stuff.
What I want to do is add a property called SignOffNameDate to the table adapter that combines the two fields and be able to bind to it in the DetailsView control. I want to do it programmatically rather than adding another column in the SQL because dealing with null values is tricky and depends on some other business rules.
This is what I tried:
public partial class TravelRequestDS
{
public partial class TravelRequestRow
{
public string SignOffNameDate
{
get { return CombineNameDate(SignOffName, SignOffDate); }
}
}
}
This property works fine when I access it programmatically, but when I try bind to it in my aspx page:
<asp:DetailsView ID="DetailsView_TravelRequest" runat="server" AutoGenerateRows="False"
DataKeyNames="TravelRequestID" DataSourceID="ObjectDataSource_TravelRequest">
<Fields>
<asp:BoundField DataField="SignOffNameDate"
HeaderText="Sign Off" />
...
I get an System.Web.HttpException exception, "A field or property with the name 'SignOffNameDate' was not found on the selected data source."
Any ideas how I can do this? Is there an attribute I need to add to the property?
If your objective is to display the combined result in a single non-editable field you can do it like this:
<asp:DetailsView ID="DetailsView_TravelRequest" runat="server" AutoGenerateRows="False"
DataKeyNames="TravelRequestID" DataSourceID="ObjectDataSource_TravelRequest">
<Fields>
<asp:TemplateField HeaderText="Sign Off" SortExpression="SignOffDate">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("SignOffName") %>'></asp:Label>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("SignOffDate") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
...
If you don't want to change your table structure, change the default method that loads data into your adapter (usually called Fill), most likely you are doing a Select * From [TravelRequest] or selecting the individual fields, so what you could do is change your TableAdapter query select statement to something like
Select [YourCol1],[YourCol2], SignOffNameDate as Null From [TravelRequest]
Modifying the default query, and selecting SignOffNameDate as null will give you access to set this value