ASP.NET Event Handler for label in Datalist - asp.net

I'm working on an ASP.NET page, and have a DataList with a non-visible label, ID, inside of it. The Datalist is populated by Query A.
I'm trying to add a handler in my VB code where it will run Query B, populating a different label in the Item Template, after ID is changed. I figure I need to go somewhere in the DataList, but poking through Intellisense and a google search weren't successful. Does anyone know how I get to that label? Sorry if this is a dumb question, and thanks for the help.
Edit: I see how I can access the datalist items while inside the function, but how do I do it for only one part of the DataList control in the Event Handler? All the options I'm seeing are related to full events involving the Data List, not a single label changing. Thanks.
Edit 2:
I figured I'd add some code, to better explain exactly the exact issue I'm having.
<asp:DataList ID="DataList1" runat="server" DataSourceID="Omitted,Ilikemyjob">
<ItemTemplate>
<asp:Label ID="FromLabel" runat="server" Font-Size="Small" Text='<%# Eval("IncdntDate") %>'></asp:Label><br />
<asp:Label ID="ToLabel" runat="server" Font-Size="Small" Text='<%# Eval("Roadway") %>'></asp:Label><br />
<asp:Label ID = "lblCrossroad" runat ="server" Font-Size = "Small" Text = '<%# Eval("Crossroad") %>'></asp:Label><br />
<asp:Label ID = "lblRdwyID" runat ="server" Font-Size="Small" Visible = "false" Text = '<%# Eval ("RdwyID") %>'></asp:Label>
<asp:Label ID = "DistanceLabel" runat ="server" Font-Size = "Small" Text = '<%# Eval("RptTime") %>'></asp:Label><br />
<asp:Label ID = "lblTTime" runat ="server" Font-Size = "small" visible ="false"></asp:Label>
<hr />
</ItemTemplate>
</asp:DataList><br />
There's the DataList thing I'm doing, and I have a different query that I want to store the value of to lblTTime if lblRdwyID is set to a certain range of values, thought it will not always be set to a value.
I'm trying to set up an event that will trigger when lblRdwyID is set, that will launch the other query and set the value of that templated items lblTTime and make it visible. Is there a good way to do this? I tried messing around with DataList1's events, but couldn't get anything to do the trick.

If your label has its visibility set to false, it won't render on the screen (as opposed to having a div block with visibility set to hidden), and so I wager that is why you can't actually find the control after the binding has occurred.
Have you tried using the DataKey property to store the ID instead? It is easily accessible at the row level and you can store additional data if needs be.

Assuming you have the row, you use:
row.FindControl("mylabelid")

Related

Nested CheckBoxList using DataSet and Repeaters

I'm trying to wire up some CBL's using a repeater and dataset. The dataset contains 2 tables with the same schema and a (one, single) relation (in SQL land think of it as a self join).
When the control is rendered if I set my DataSource = the relationship, I am able to get the children elements to show; so I know the model is good, although misplaced (on purpose to test the model - see code below).
The problem is:
I am having difficulty getting the parent elements to show up at all
Not all parents have children, and the ones that don't still need to show up
Am I approaching this in the right frame of mind? i.e. Am I missing something fundamental? Implementation outside of CBL(plain text) works fine per this article
<asp:Repeater ID="ParentRepeater" runat="server">
<ItemTemplate>
<asp:CheckBoxList ID="ParentCBL" runat="server"
DataSource='<%# DataBinder.Eval(Container.DataItem,"Joined") %>'
DataTextField="TextProperty"
DataValueField="ValueProperty">
</asp:CheckBoxList>
<asp:Repeater ID="ChildRepeater" runat="server">
<ItemTemplate>
<asp:CheckBoxList ID="ChildCBL" runat="server"
DataSource='<%# DataBinder.Eval(Container.DataItem, "Joined") %>'
DataTextField="TextProperty"
DataValueField="ValueProperty">
</asp:CheckBoxList>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
Page Load is nothing spectacular
DataSet ds = Foo.foo();
ParentRepeater.DataSource = ds.Tables["Parent"];
ParentRepeater.DataBind();
In your code you don't bind ChildRepeater
If you want just change plain text to Checkbox in according to https://support.microsoft.com/en-us/kb/306154
then you don't need to use CheckBoxList. But you should only use single checkbox inside repeater.

Bind a Label Font Size which is in an ASP.NET Gridview from a DataBase

I want to bind a label's font-size from a database. The label is in a TemplateField. I have a column in my database with the name "FontSize" which contains data such as "small", "large", or "12px".
I used the below code to do this, but an error occurs. I think I might need to change my "FontSize" data format in the database or change the Font-Size='<%# Eval("FontSize") %>' line in the below code. How can I do this?
<asp:TemplateField>
<ItemTemplate>
<div>
<asp:Label ID="Label3"
runat="server"
Font-Bold="True"
Text='<%# Eval("PubText") %>'
Font-Size='<%# Eval("FontSize") %>'
ForeColor='<%# System.Drawing.Color.FromName(Eval("Color").ToString()) %>'></asp:Label>
</div>
</ItemTemplate>
</asp:TemplateField>
Binding a font size is kind of confusing because you can give it an explicit string value such as "12px" or "Smaller" like so:
Font-Size="12px"
But when you want to bind a value, it expects a FontUnit. Fortunately, FontUnit has a few methods that we can use to create the FontUnit we need from other datatypes. Since it looks like your data is strings, you probably need the FontUnit.Parse method:
Font-Size='<%# FontUnit.Parse(Eval("FontSize").ToString()) %>'
There is also a FontUnit.Point method that takes an integer. So you could use that if your data happened to be something like 10, 15, 20, etc.
Font-Size='<%# FontUnit.Point(Convert.ToInt32(Eval("FontSize"))) %>'

Setting Value of Bind

In my code I have this bind set up to a textbox
<asp:TextBox ID="CompanyTextBox" runat="server" Text='<%# Bind("Company") %>' CssClass="txtbox" />
Is there a way a way to access the "Company" value and change it programmatically, or do I simply change the value of the textbox and It will update accordingly?
Check links below:
http://mrbool.com/two-way-data-binding-course-asp-net-4-and-visual-studio-2010-part-46/22435
http://www.ccopus.com/blog/article.asp?id=24
You can see the same question here

Getting text out of textarea from a ASP.NET GridView update

i'm loosing my mind. Using ASP.NET in a GridView, amongst other controls, I have the following:
<asp:TemplateField HeaderText="Intention">
<EditItemTemplate>
<asp:TextBox ID="IntentionInfo" Enabled="true" TextMode="MultiLine" Wrap="true" runat="server" />
</EditItemTemplate>
<ItemTemplate>
<asp:TextBox ID="IntentionInfo" Enabled="false" TextMode="MultiLine" runat="server" />
</ItemTemplate>
</asp:TemplateField>
I would like to take the value out of this textarea and save in a database. However, server side, I try to pull the value out, like such:
string txt = (TextBox)DonationResultsTable.Rows[e.RowIndex].Cells[6].Controls[1].Text;
... but I keep getting the value that was SENT to the Client.
I wrote this javascript and I can see the values change in the DOM, but still the server keeps taking the old value.
$("textarea").change(function()
{
var txt = $(this).val();
$(this).html(txt).text(txt);
});
So my guess was ViewState, but I disabled it for those controls, like this:
<asp:TextBox ID="IntentionInfo" ViewStateMode="Disabled" Enabled="false" TextMode="MultiLine" runat="server" />
Still nothing! Any ideas?
One option could be to use a hidden field and update it on text changed for the text area. You could do this with jQuery like this:
$("textarea[id$=tbTest]").change(function () {
$("input[id$=hdnVal]").val($("textarea[id$=tbTest]").val());
});
Then on the server side, you can retrieve the hidden field's value and save it to your database.

How do I data bind a drop down list in a gridview from a database table using VB?

So in this gridview, there is a column for status and I want to have a drop down list with Pass, Pending, Fail appear when the edit button is clicked. These values are already in a table, so I need to somehow bind from this table to each ddl for every row.
Here is the column from the gridview. As you can see, I would like to just have a label showing when not in edit mode, and a ddl when the edit button is pressed
<asp:TemplateField HeaderText="During Production Status" SortExpression="DuringProductionStatus">
<EditItemTemplate>
<asp:DropDownList ID="ddlStatus" runat="server" datavaluefield="Name"
datatextfield="Name" DataSource="*What goes here?*"> />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblStatus" runat="server"
Text='I don't understand how to get this from the ddl' />
</ItemTemplate>
</asp:TemplateField>
For clarity's sake, my table is named Status, and the database is named DirectImport
There's a few steps to go through here - none of them are particularly difficult, but they can be a bit fiddly (IMHO). The good news is once you've got this working once, it gets easier to do it again!
I'm assuming you've got a <asp:*DataSource> control on your page - my preference is for an ObjectDataSource but I don't think it matters, I think a SqlDataSource works equally well. I've never tried doing this with GridView.DataSource = MyDataSet in code-behind, so I don't know whether that would work or not, but my assumption is that it wouldn't as you wouldn't get the proper two-way binding that you want. This data source feeds your grid with your main data. The key point here is that your database query must return both the Status text field and the Status id.
So your gridview will now look something like:
<asp:objectdatasource runat="server" id="MainDataSource" ... />
<asp:gridview runat="server" id="MyGridView" DataSourceID="MainDataSource">
<Columns>
<asp:TemplateField HeaderText="During Production Status" SortExpression="DuringProductionStatus">
<ItemTemplate>
<asp:Label ID="lblStatus" runat="server"
Text="<%# Bind('Status') %>" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:gridview>
The Text="<%# Bind('Status') %>" is the bit you're missing to get the status text into the grid.
Now add a second data source into your markup that reads in the set of values from the Status table.
<asp:objectdatasource runat="server" id="StatusObjectDataSource" ... />
And add the EditItemTemplate into the GridView, which is bound to the Status DataSource.
<EditItemTemplate>
<asp:DropDownList ID="ddlStatus" runat="server" datavaluefield="StatusID"
datatextfield="Status" DataSourceID="StatusObjectDataSource"
SelectedValue="<%# Bind('StatusId') %>" />
</EditItemTemplate>
The SelectedValue="<%# Bind('StatusId') %>" is what connects up the two datasets so that when you flip a row into Edit mode, the dropdownlist has the correct item already selected, and when you then save it you've got the Status ID to put into your database.
And you're done.
I have used the RowDataBound event. Here is a small code snippet. HTH
you would have an ItemTemplate in your aspx/ascx
<asp:TemplateField HeaderText="Column Headings">
<ItemTemplate>
<asp:DropDownList ID="ddlName" runat="server" Width="150"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
and in your code behind, you will have
protected void grdDataMap_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddl = (DropDownList)e.Row.FindControl("ddlName");
ddl.DataSource = someList;//the source of your dropdown
ddl.DataBind();
}
}
so when you bind your grid with grdDataMap.Databind (assuming your grid id is grdDataMap), row databound event will be called for each row (including header/footer, and thats the reason you check RowType)
so you can probably decide what controls/columns to hide/show/bind inside this row databound event
In the winforms world I pull my objects from the DB into a List(Of Whatever) and use the list as the datasource.
This also lets me add extra "convenience" fields in the object so that I can populate it with stuff from other tables.
I don't know asp.net at all so if you can do something similar, it might help.
A really quick solution is to create a custom web control for the status dropdown. The control will always contain the same data. When it renders you populate the datasource. When it gets added to the gridview, the data will be in the drop down. Hope that helps!

Resources